Aspose.Words 随笔
//加载文档
Document doc = new Document(Server.MapPath("~/ExportTemplate/CLAAS/ServiceReport.doc"));
//保存
SetData(doc);
doc.Save(Page.Response, "C010.doc", ContentDisposition.Attachment, null);
doc.Clone();
private void SetData(Document doc)
{
DocumentBuilder builder = new DocumentBuilder(doc);
DataTable products = GetTable(); //数据源
builder.MoveToDocumentEnd();
// 下划线不设置
builder.Font.Underline=Underline.None;
//开始表格
Aspose.Words.Tables.Table table = builder.StartTable();
builder.RowFormat.Alignment = RowAlignment.Center;
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
List<string> listcolumn = new List<string>(products.Columns.Count);
for (var i = 0; i < products.Columns.Count; i++)
{
listcolumn.Add(products.Columns[i].ColumnName.Trim());
if (i == 0)
{
//添加表格
builder.InsertCell();
builder.Write("维修信息:");
//对其方式
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
}
else
{
builder.InsertCell();
builder.Write("");
builder.CellFormat.HorizontalMerge = CellMerge.None;
}
}
builder.EndRow();
//表格对其方式
table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.AutoFitToWindow);
#region##循环表头
foreach (string HeadName in listcolumn)
{
builder.InsertCell();
builder.Write(HeadName);
}
builder.EndRow();
#endregion
#region##循环数据
foreach (DataRow ro in products.Rows)
{
foreach (string HeadName in listcolumn)
{
builder.InsertCell();
builder.Write(ro[HeadName].ToString());
}
builder.EndRow();
}
#endregion
builder.EndTable();