Aspose.Words是一款功能强大的word文档处理控件,在不需要安装word的条件下,可进行word的创建,修改,转换等操作。
Aspose.Words可以简单使用该产品提供的DocumentBuilder类库进行Word表格的插入。
DocumentBuilder.StartTable 开始构建一个新的表格
DocumentBuilder.InsertCell
插入新的行和单元格到表格
DocumentBuilder.Writeln
为当前单元格写入文本
DocumentBuilder.EndRow用于指示结束当前行,并且开始新的一行
DocumentBuilder.EndTable
表示表格构建完成
下面的代码,展示了如何插入一个简单无格式的表格到word:
Document doc = new
Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// We call this method to start building the
table.
builder.StartTable();
builder.InsertCell();
builder.Write("Row
1, Cell 1 Content.");
// 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();
// Build the first cell of the second
row.
builder.InsertCell();
builder.Write("Row 2, Cell 1 Content");
// Build the second cell.
builder.InsertCell();
builder.Write("Row 2,
Cell 2 Content.");
builder.EndRow();
// Signal that we have finished building the
table.
builder.EndTable();
// 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
Aspose.Words使用代码插入表格,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/component/p/3765715.html