Aspose.Words是一款功能強大的word文檔處理控件,在不須要安裝word的條件下,可進行word的建立,修改,轉換等操做。html
Aspose.Words能夠簡單使用該產品提供的DocumentBuilder類庫進行Word表格的插入。app
DocumentBuilder.StartTable 開始構建一個新的表格
DocumentBuilder.InsertCell 插入新的行和單元格到表格
DocumentBuilder.Writeln 爲當前單元格寫入文本
DocumentBuilder.EndRow用於指示結束當前行,而且開始新的一行
DocumentBuilder.EndTable 表示表格構建完成ide
下面的代碼,展現瞭如何插入一個簡單無格式的表格到word:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);ui
// We call this method to start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");this
// Build the second cell
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content.");
// Call the following method to end the row and start a new row.
builder.EndRow();component
// Build the first cell of the second row.
builder.InsertCell();
builder.Write("Row 2, Cell 1 Content");orm
// Build the second cell.
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content.");
builder.EndRow();htm
// Signal that we have finished building the table.
builder.EndTable();ci
// Save the document to disk.
doc.Save(MyDir + "DocumentBuilder.CreateSimpleTable Out.doc");文檔
下面代碼展現了,如何使用代碼插入格式化的表格到word:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
// Make the header row.
builder.InsertCell();
// Set the left indent for the table. Table wide formatting must be applied after
// at least one row is present in the table.
table.LeftIndent = 20.0;
// Set height and define the height rule for the header row.
builder.RowFormat.Height = 40.0;
builder.RowFormat.HeightRule = HeightRule.AtLeast;
// Some special features for the header row.
builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(198, 217, 241);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 16;
builder.Font.Name = "Arial";
builder.Font.Bold = true;
builder.CellFormat.Width = 100.0;
builder.Write("Header Row,\n Cell 1");
// We don't need to specify the width of this cell because it's inherited from the previous cell.
builder.InsertCell();
builder.Write("Header Row,\n Cell 2");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Header Row,\n Cell 3");
builder.EndRow();
// Set features for the other rows and cells.
builder.CellFormat.Shading.BackgroundPatternColor = Color.White;
builder.CellFormat.Width = 100.0;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
// Reset height and define a different height rule for table body
builder.RowFormat.Height = 30.0;
builder.RowFormat.HeightRule = HeightRule.Auto;
builder.InsertCell();
// Reset font formatting.
builder.Font.Size = 12;
builder.Font.Bold = false;
// Build the other cells.
builder.Write("Row 1, Cell 1 Content");
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Row 1, Cell 3 Content");
builder.EndRow();
builder.InsertCell();
builder.CellFormat.Width = 100.0;
builder.Write("Row 2, Cell 1 Content");
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content");
builder.InsertCell();
builder.CellFormat.Width = 200.0;
builder.Write("Row 2, Cell 3 Content.");
builder.EndRow();
builder.EndTable();
doc.Save(MyDir + "DocumentBuilder.CreateFormattedTable Out.doc");
試用版下載:http://www.componentcn.com/html/wbbjkj_281_3926.html
聯繫方式:846631466