using IronWord;
using IronWord.Models;
// Create table
Table table = new Table(5, 3);
// Apply styling
table.Zebra = new ZebraColor("FFFFFF", "dddddd");
// Populate table
table[0, 0] = new TableCell(new Text("Number"));
table[0, 1] = new TableCell(new Text("First Name"));
table[0, 2] = new TableCell(new Text("Last Name"));
for (int i = 1; i < table.Rows.Count; i++)
{
table[i, 0].AddChild(new Text($"{i}"));
table[i, 1].AddChild(new Text($"---"));
table[i, 2].AddChild(new Text($"---"));
}
// Create new Word document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx");
Imports IronWord
Imports IronWord.Models
' Create table
Private table As New Table(5, 3)
' Apply styling
table.Zebra = New ZebraColor("FFFFFF", "dddddd")
' Populate table
table(0, 0) = New TableCell(New Text("Number"))
table(0, 1) = New TableCell(New Text("First Name"))
table(0, 2) = New TableCell(New Text("Last Name"))
For i As Integer = 1 To table.Rows.Count - 1
table(i, 0).AddChild(New Text($"{i}"))
table(i, 1).AddChild(New Text($"---"))
table(i, 2).AddChild(New Text($"---"))
Next i
' Create new Word document from the table
Dim doc As New WordDocument(table)
' Export Word document
doc.SaveAs("Document.docx")
要使用IronWord添加表格,首先通過提供行和列的數量來實例化表格。根據需求自定義整個表格的樣式。可以通過訪問表格中的每個單元格。[行,列] 格式。有了這個功能,您可以根據需要為每個單元格設置樣式,並添加多個文檔元素,如文本運行、段落、圖片和形狀到單元格中。一旦正確設置了表格對象,就可以用來創建新的 Word 文檔。