文檔半島外圍網上直營>>Aspose中文文檔>>將表格添加到 Word 文檔
                將表格添加到 Word 文檔
Aspose.Words是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
使用 Aspose.Words在 Aspose.Words 中,通常使用DocumentBuilder插入表格。使用其方法構建表格并將內容插入表格單元格,例如以下內容:
- 起始表
- 插入單元格
- 結束行
- EndTable
- 寫入
以下代碼示例展示了如何將表格添加到文檔中:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Start the table, then populate the first row with two cells.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1.");
builder.InsertCell();
builder.Write("Row 1, Cell 2.");
// Call the builder's "EndRow" method to start a new row.
builder.EndRow();
builder.InsertCell();
builder.Write("Row 2, Cell 1.");
builder.InsertCell();
builder.Write("Row 2, Cell 2.");
builder.EndTable();
// Save the document.
doc.Save("CreateTable.docx");
	點擊復制
使用 Open XML SDK
需要添加的命名空間:
	
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
點擊復制
	
WordProcessingML文檔的基本文檔結構由文檔和正文元素組成,后跟一個或多個塊級元素,例如代表段落的 p。一個段落包含一個或多個 r 元素。r 代表 run,它是具有一組通用屬性(例如格式設置)的文本區域。一次運行包含一個或多個 t 元素。t 元素包含文本范圍。 為了創建此結構,下面的代碼中使用了CreateWordprocessingDocument函數。通過使用AddTable函數,我們可以通過傳遞文件名和數據字符串將表格添加到word文檔中。
以下代碼示例展示了如何將表格添加到文檔中:
	
public void AddTableFeature()
{
string[,] data = {{"Mike", "Amy"}, {"Mary", "Albert"}};
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(ArtifactsDir + "Add Table - OpenXML.docx",
WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Create text in body - Create wordprocessing document"));
Table table = new Table();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}));
table.AppendChild(props);
for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
// Assume you want automatically sized columns.
tc.Append(new TableCellProperties(
new TableCellWidth {Type = TableWidthUnitValues.Auto}));
tr.Append(tc);
}
table.Append(tr);
}
mainPart.Document.Body.Append(table);
mainPart.Document.Save();
}
}
	點擊復制
	

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線