Dalam tutorial ini, Anda akan belajar menambahkan baris ke tabel secara terprogram dalam dokumen MS Word menggunakan C#. Pertama-tama kita akan menambahkan tabel baris Word C# pada indeks yang ditentukan dalam kumpulan baris tabel dan kemudian beberapa baris tabel Word C# kosong akan ditambahkan ke akhir tabel.
Langkah-langkah Menambahkan Baris ke Tabel di Dokumen MS Word menggunakan C#
- Instal paket Aspose.Words for .NET di proyek Anda melalui NuGet
- Impor ruang nama Aspose.Words dan Aspose.Words.Tables
- Buka Dokumen MS Word menggunakan kode C#
- Dapatkan Table menurut indeks yang ingin Anda tambahi Baris
- Buat objek kelas Baris baru atau klon Baris yang ada dari Tabel
- Tambahkan beberapa Sel dengan Paragraf teks ke Baris di atas
- Gunakan metode RowCollection.Add untuk menambahkan Baris ke akhir koleksi Table.Rows
- Atau gunakan metode RowCollection.Insert untuk menyisipkan Baris pada indeks tertentu
- Simpan kembali dokumen MS Word dengan menambahkan Baris Tabel
Kode C# aplikasi .NET console berikut menggunakan Aspose Words menambahkan baris ke tabel yang ada di dokumen MS Word.
Kode untuk Menambahkan Baris ke Tabel di Dokumen MS Word menggunakan C#
using Aspose.Words; | |
using Aspose.Words.Tables; | |
namespace AddRowsToTableInMsWordDocumentUsingCsharp | |
{ | |
class Aspose_Words_Table_Add_Row | |
{ | |
static void Main(string[] args) | |
{ | |
License setupToAddRowsToTable = new License(); | |
setupToAddRowsToTable.SetLicense("path to license file.lic"); | |
// Open MS Word Document | |
Document MSWordDocument = new Document(@"MS Word.docx"); | |
// Get the Table by index | |
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0]; | |
#region C# Word table insert row | |
// Create a new Row class object | |
Row row = new Row(MSWordDocument); | |
// Add three Cells to Row's cells collection | |
for (int i = 0; i < 3; i++) | |
{ | |
Cell cell = new Cell(MSWordDocument); | |
cell.AppendChild(new Paragraph(MSWordDocument)); | |
cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i)); | |
row.Cells.Add(cell); | |
} | |
// Insert new Row after the first Row | |
tableToAddRowsTo.Rows.Insert(1, row); | |
#endregion | |
#region C# add rows to Word table | |
// Clone an existing Row from Table | |
Row cloneOfRow = (Row)tableToAddRowsTo.FirstRow.Clone(true); | |
// Remove all content from all Cells | |
foreach (Cell cell in cloneOfRow) | |
{ | |
cell.RemoveAllChildren(); | |
cell.EnsureMinimum(); | |
} | |
// Add multiple empty rows to the end of table | |
for (int i = 0; i < 10; i++) | |
{ | |
Row emptyRow = (Row)cloneOfRow.Clone(true); | |
tableToAddRowsTo.Rows.Add(emptyRow); | |
} | |
#endregion | |
MSWordDocument.Save(@"Added Rows to Table to MS Word.docx"); | |
} | |
} | |
} |
Contoh tabel C# Word menambahkan baris ini pada dasarnya pertama-tama perlu mendapatkan tabel yang ada dari dokumen MS Word menggunakan indeksnya. Tetapi jika tidak ada tabel dalam dokumen maka Anda bahkan dapat membuat kode membuat tabel di Word menggunakan C#. Kode pertama tabel C# Word menyisipkan baris pada indeks yang ditentukan dalam koleksi baris. Kemudian mengkloning baris tabel pertama, menghapus semua konten dari semua sel dan akhirnya C# ini menambahkan baris ke potongan kode tabel Word akan menambahkan beberapa baris kosong ke akhir tabel dalam dokumen MS Word.