Cách Thêm Dòng Vào Bảng Trong Tài Liệu MS Word Bằng C#

Cách Thêm Dòng Vào Bảng Trong Tài Liệu MS Word Bằng C#

Trong hướng dẫn này, bạn sẽ học cách thêm các hàng vào bảng trong tài liệu MS Word một cách lập trình bằng C#. Bạn sẽ thêm các hàng tại một chỉ mục xác định và chèn nhiều hàng trống ở cuối bảng.

Lợi ích của việc thêm hàng vào bảng trong tài liệu Word

  1. Quản lý nội dung động: - Dễ dàng chỉnh sửa các bảng hiện có để phù hợp với dữ liệu mới.
  2. Tính linh hoạt: - Điều chỉnh cấu trúc bảng một cách lập trình mà không cần chỉnh sửa thủ công.
  3. Tự động hoá: - Quản lý dữ liệu bảng một cách hiệu quả thông qua các script tự động.

Yêu cầu trước: Chuẩn bị môi trường

  1. Cài đặt Visual Studio hoặc bất kỳ IDE .NET nào.
  2. Đảm bảo thư viện Aspose.Words có sẵn qua NuGet.

Hướng Dẫn Từng Bước Để Thêm Dòng Vào Bảng Trong Word

Bước 1: Cài Đặt Thư Viện Aspose.Words

Cài đặt gói Aspose.Words bằng trình quản lý gói NuGet.

Install-Package Aspose.Words

Bước 2: Nhập các Namespace cần thiết

Bao gồm các namespace Aspose.Words và Aspose.Words.Tables trong dự án của bạn.

using Aspose.Words;
using Aspose.Words.Tables;

Bước 3: Mở tài liệu Word

Tải tài liệu MS Word hiện có.

Document MSWordDocument = new Document(@"MS Word.docx");

Bước 4: Truy cập Table

Lấy Table theo chỉ mục của nó từ tài liệu.

Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

Bước 5: Tạo hoặc sao chép một Row

Tạo một Row mới hoặc sao chép một Row hiện có từ Table.

Row row = new Row(MSWordDocument);

Bước 6: Thêm các ô vào hàng

Thêm các ô và văn bản vào hàng.

using Aspose.Words;
using Aspose.Words.Tables;

// Open MS Word Document
Document MSWordDocument = new Document(@"BigDocument.docx");

// Get the Table by index
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

// 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);

// 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);
}

// Save updated document
MSWordDocument.Save(@"output.docx");

Bước 7: Thêm các hàng vào cuối

Sử dụng RowCollection.Add để thêm các hàng vào Bảng.

tableToAddRowsTo.Rows.Add(row);

Bước 8: Chèn các hàng vào một chỉ mục cụ thể

Chèn các hàng vào một chỉ mục cụ thể bằng cách sử dụng RowCollection.Insert.

tableToAddRowsTo.Rows.Insert(1, row);

Bước 9: Lưu tài liệu đã cập nhật

Lưu lại tài liệu với các hàng đã được thêm.

MSWordDocument.Save(@"Added Rows to Table in MS Word.docx");

Mã ví dụ để thêm các hàng vào Bảng trong Word

Dưới đây là mã hoàn chỉnh để thêm các hàng vào một bảng:

using Aspose.Words;
using Aspose.Words.Tables;

// Open MS Word Document
Document MSWordDocument = new Document(@"BigDocument.docx");

// Get the Table by index
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

// 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);

// 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);
}

// Save updated document
MSWordDocument.Save(@"output.docx");

Kết luận

Hướng dẫn này đã trình bày cách thêm các hàng vào một bảng trong tài liệu MS Word bằng C#. Bằng cách thực hiện các bước này, bạn có thể quản lý và chỉnh sửa các bảng trong tài liệu Word một cách hiệu quả thông qua lập trình.

 Tiếng Việt