aspose.words .net 导出word表
程序员文章站
2022-04-09 08:45:32
...
using Aspose.Words;
public void ExportWord()
{
string filePath = Server.MapPath("~/Template.doc");
string filePath1 = Server.MapPath("~/Template1.doc");
//预先生成数据
List<Student> studentData = new List<Student>();
for (int i = 0; i < 100; i++)
{
studentData.Add(new Student()
{
name = "学生" + i.ToString("D3"),
schoolName = "某某中学",
num = i,
score = i
});
}
//加载word模板。
Aspose.Words.Document doc = new Aspose.Words.Document(filePath);
Aspose.Words.DocumentBuilder docWriter = new Aspose.Words.DocumentBuilder(doc);
double[] colWidth = new double[] { 45, 60, 33, 55 };
string[] colName = new string[] { "编号", "姓名", "分数", "学校" };
int pageSize = 0;
for (int i = 0, j = studentData.Count; i < j; i++)
{
if (pageSize == 0)
{
//word页刚开始,一个表格的开始,要插入一个表头
docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
docWriter.StartTable();
AsposeCreateCell(docWriter, colWidth[0], colName[0]);
AsposeCreateCell(docWriter, colWidth[1], colName[1]);
AsposeCreateCell(docWriter, colWidth[2], colName[2]);
AsposeCreateCell(docWriter, colWidth[3], colName[3]);
docWriter.EndRow();
}
else if (pageSize == 30)//经过测算,每页word中可以放置30行
{
//结束第一个表格,插入分栏符号,并开始另一个表格
docWriter.EndTable();
docWriter.InsertBreak(Aspose.Words.BreakType.ColumnBreak);
docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
docWriter.StartTable();
AsposeCreateCell(docWriter, colWidth[0], colName[0]);
AsposeCreateCell(docWriter, colWidth[1], colName[1]);
AsposeCreateCell(docWriter, colWidth[2], colName[2]);
AsposeCreateCell(docWriter, colWidth[3], colName[3]);
docWriter.EndRow();
}
else if (pageSize == 60)//word分栏为2栏,那么一页word可以放60行数据
{
//一页word完毕,关闭表格,并绘制下一页的表头。
docWriter.EndTable();
docWriter.InsertBreak(Aspose.Words.BreakType.PageBreak);
docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
docWriter.StartTable();
AsposeCreateCell(docWriter, colWidth[0], colName[0]);
AsposeCreateCell(docWriter, colWidth[1], colName[1]);
AsposeCreateCell(docWriter, colWidth[2], colName[2]);
AsposeCreateCell(docWriter, colWidth[3], colName[3]);
docWriter.EndRow();
pageSize = 0;
}
pageSize++;
//创建内容
AsposeCreateCell(docWriter, colWidth[0], studentData[i].num.ToString());
AsposeCreateCell(docWriter, colWidth[1], studentData[i].name);
AsposeCreateCell(docWriter, colWidth[2], studentData[i].score.ToString());
AsposeCreateCell(docWriter, colWidth[3], studentData[i].schoolName);
docWriter.EndRow();
}//end for
//保存文件
doc.Save(filePath1, Aspose.Words.SaveFormat.Doc);
}
public void AsposeCreateCell(Aspose.Words.DocumentBuilder builder, double width, string text)
{
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.Width = width;//单元格的宽度
builder.CellFormat.LeftPadding = 3;//单元格的左内边距
builder.CellFormat.RightPadding = 3;//单元格的右内边距
builder.RowFormat.Height = 20;//行高
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
builder.Write(text);
}
public void MergeCell()
{
string filePath = Server.MapPath("~/Template.doc");
string filePath1 = Server.MapPath("~/Template1.doc");
Aspose.Words.Document doc = new Aspose.Words.Document(filePath);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
//水平合并
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
//垂直合并
//builder.CellFormat.HorizontalMerge= Aspose.Words.Tables.CellMerge.First;
builder.Write("Text in merged cells.");
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
//builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.Previous;
builder.Write("Text in one cell");
builder.EndRow();
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
// 此单元格垂直合并到单元格上方,并应为空.
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous;
//builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
builder.InsertCell();
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
builder.Write("Text in another cell");
builder.EndRow();
doc.Save(filePath1, Aspose.Words.SaveFormat.Doc);
}
上一篇: PHP判断是否为手机客户端的方法
下一篇: PHP实现递归删除目录的通用方法