Hướng dẫn này liệt kê hai cách khác nhau để tạo bảng trong tài liệu Word bằng C#. Trước tiên, chúng ta sẽ tạo bảng trong Word bằng C# với sự trợ giúp của Aspose DocumentBuilder và sau đó tạo bảng Word bằng C# trực tiếp trong mô hình đối tượng tài liệu Word (DOM).
Các bước để tạo bảng trong tài liệu Word bằng C#
- Tham khảo gói Aspose.Words for .NET từ NuGet trong giải pháp
- Nhập các không gian tên Aspose.Words và Aspose.Words.Tables
- Tạo phiên bản lớp DocumentBuilder hoặc Table
- Sử dụng phương thức InsertCell hoặc Table.Rows.Cells.Add để thêm các ô vào hàng của bảng
- Sử dụng phương thức DocumentBuilder.Write hoặc Cell.AppendChild để chèn đoạn văn bản
- Tạo nhiều hàng trong bảng và hoàn thành tạo bảng trong văn bản Word
- Lưu dưới dạng tài liệu Word với bảng sang định dạng DOCX trên đĩa
Ví dụ mã Aspose DocumentBuilder sau đây có thể được sử dụng trong ứng dụng .NET để tạo bảng C# Word.
Mã để tạo bảng trong tài liệu Word bằng C#
using Aspose.Words; | |
using Aspose.Words.Tables; | |
namespace CreateTableInWordDocumentUsingCsharp | |
{ | |
class CreateTableWord | |
{ | |
static void Main(string[] args) | |
{ | |
// Set license before C# Word table creation | |
License setupToCreateTable = new License(); | |
setupToCreateTable.SetLicense(@"license file path.lic"); | |
// Document Builder object to create Table in Word document | |
DocumentBuilder toCreateTableInWord = new DocumentBuilder(); | |
// Mark the start of Word Table | |
Table tableInWord = toCreateTableInWord.StartTable(); | |
// Insert a new Row & then first Cell in Word Table | |
toCreateTableInWord.InsertCell(); | |
// Write text in Table Cell | |
toCreateTableInWord.Write("Table Row 1 and Cell 1"); | |
// Insert a new Cell in same Word Table Row | |
toCreateTableInWord.InsertCell(); | |
// Insert an Image in Word Table Cell | |
toCreateTableInWord.InsertImage("insert image in word table.jpg"); | |
// Mark the end of Table Row | |
toCreateTableInWord.EndRow(); | |
// Mark end of Word Table creation | |
toCreateTableInWord.EndTable(); | |
// Save Word DOCX document with Table on disk | |
toCreateTableInWord.Document.Save("c# word table created in.docx"); | |
} | |
} | |
} |
Về cơ bản, ứng dụng Word C# tạo bảng này cho phép bạn chèn một bảng vào tài liệu Word. Đầu tiên, nó thêm hai ô trong hàng đầu tiên của bảng và sau đó viết nội dung văn bản vào ô đầu tiên rồi đến thêm hình ảnh vào Word C# ô cuối cùng.
Mã để tạo bảng từ bằng C# (Lớp DOM)
using Aspose.Words; | |
using Aspose.Words.Tables; | |
namespace CreateWordTable | |
{ | |
class CreateTableInWord | |
{ | |
static void Main(string[] args) | |
{ | |
// Set license before creating Word table | |
License license = new License(); | |
license.SetLicense(@"license file path.lic"); | |
Document wordDocument = new Document(); // Create empty Word document | |
Table wordTable = new Table(wordDocument); // Create Table object | |
wordTable.Rows.Add(new Row(wordDocument)); // Create a Row inside Table | |
wordTable.FirstRow.Cells.Add(new Cell(wordDocument)); // Create a single Cell in Table Row | |
wordTable.FirstRow.FirstCell.AppendChild(new Paragraph(wordDocument)); // Add a Paragraph inside Cell | |
// Write text content inside Word Table Cell | |
wordTable.FirstRow.FirstCell.FirstParagraph.Runs.Add(new Run(wordDocument, "Text in Table Row 1 and Cell 1")); | |
// Insert Table at the end of Word Document | |
wordDocument.FirstSection.Body.InsertBefore(wordTable, wordDocument.FirstSection.Body.LastParagraph); | |
// Save Word document to DOCX format | |
wordDocument.Save("c# word table created in.docx"); | |
} | |
} | |
} |
Ví dụ về mã C# tạo bảng trong Aspose Word ở trên chỉ trình bày một cách tiếp cận khác để thêm phần tử bảng trong mô hình đối tượng tài liệu Word bằng cách sử dụng các lớp Bảng, Hàng, Ô và Đoạn văn.