需要word动态绘制表格。在网上找了些资料,觉得aspose.words还是挺方便的。
把自己测试的代码贴出来有需要的可以看看
1 using Aspose.Words;
2
3 public void ExportWord()
4 {
5 string filePath = Server.MapPath("~/Template.doc");
6 string filePath1 = Server.MapPath("~/Template1.doc");
7 //预先生成数据
8 List<Student> studentData = new List<Student>();
9 for (int i = 0; i < 100; i++)
10 {
11 studentData.Add(new Student()
12 {
13 name = "学生" + i.ToString("D3"),
14 schoolName = "某某中学",
15 num = i,
16 score = i
17 });
18 }
19 //加载word模板。
20 Aspose.Words.Document doc = new Aspose.Words.Document(filePath);
21 Aspose.Words.DocumentBuilder docWriter = new Aspose.Words.DocumentBuilder(doc);
22
23 double[] colWidth = new double[] { 45, 60, 33, 55 };
24 string[] colName = new string[] { "编号", "姓名", "分数", "学校" };
25 int pageSize = 0;
26 for (int i = 0, j = studentData.Count; i < j; i++)
27 {
28 if (pageSize == 0)
29 {
30 //word页刚开始,一个表格的开始,要插入一个表头
31 docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
32 docWriter.StartTable();
33 AsposeCreateCell(docWriter, colWidth[0], colName[0]);
34 AsposeCreateCell(docWriter, colWidth[1], colName[1]);
35 AsposeCreateCell(docWriter, colWidth[2], colName[2]);
36 AsposeCreateCell(docWriter, colWidth[3], colName[3]);
37 docWriter.EndRow();
38 }
39 else if (pageSize == 30)//经过测算,每页word中可以放置30行
40 {
41 //结束第一个表格,插入分栏符号,并开始另一个表格
42 docWriter.EndTable();
43 docWriter.InsertBreak(Aspose.Words.BreakType.ColumnBreak);
44 docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
45 docWriter.StartTable();
46 AsposeCreateCell(docWriter, colWidth[0], colName[0]);
47 AsposeCreateCell(docWriter, colWidth[1], colName[1]);
48 AsposeCreateCell(docWriter, colWidth[2], colName[2]);
49 AsposeCreateCell(docWriter, colWidth[3], colName[3]);
50 docWriter.EndRow();
51 }
52 else if (pageSize == 60)//word分栏为2栏,那么一页word可以放60行数据
53 {
54 //一页word完毕,关闭表格,并绘制下一页的表头。
55 docWriter.EndTable();
56 docWriter.InsertBreak(Aspose.Words.BreakType.PageBreak);
57 docWriter.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
58 docWriter.StartTable();
59 AsposeCreateCell(docWriter, colWidth[0], colName[0]);
60 AsposeCreateCell(docWriter, colWidth[1], colName[1]);
61 AsposeCreateCell(docWriter, colWidth[2], colName[2]);
62 AsposeCreateCell(docWriter, colWidth[3], colName[3]);
63 docWriter.EndRow();
64 pageSize = 0;
65 }
66 pageSize++;
67 //创建内容
68 AsposeCreateCell(docWriter, colWidth[0], studentData[i].num.ToString());
69 AsposeCreateCell(docWriter, colWidth[1], studentData[i].name);
70 AsposeCreateCell(docWriter, colWidth[2], studentData[i].score.ToString());
71 AsposeCreateCell(docWriter, colWidth[3], studentData[i].schoolName);
72 docWriter.EndRow();
73 }//end for
74 //保存文件
75 doc.Save(filePath1, Aspose.Words.SaveFormat.Doc);
76
77
78 }
1 public void AsposeCreateCell(Aspose.Words.DocumentBuilder builder, double width, string text)
2 {
3
4 builder.InsertCell();
5 builder.CellFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
6 builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
7 builder.CellFormat.Width = width;//单元格的宽度
8 builder.CellFormat.LeftPadding = 3;//单元格的左内边距
9 builder.CellFormat.RightPadding = 3;//单元格的右内边距
10 builder.RowFormat.Height = 20;//行高
11 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
12 builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐
13 builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;//水平居中对齐
14 builder.Write(text);
15 }
合并单元格:
1 public void MergeCell()
2 {
3 string filePath = Server.MapPath("~/Template.doc");
4 string filePath1 = Server.MapPath("~/Template1.doc");
5 Aspose.Words.Document doc = new Aspose.Words.Document(filePath);
6 Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
7 builder.InsertCell();
8 builder.CellFormat.Borders.LineStyle = LineStyle.Single;
9 builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
10 //水平合并
11 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
12 //垂直合并
13 //builder.CellFormat.HorizontalMerge= Aspose.Words.Tables.CellMerge.First;
14 builder.Write("Text in merged cells.");
15
16 builder.InsertCell();
17 builder.CellFormat.Borders.LineStyle = LineStyle.Single;
18 builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
19 //builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.Previous;
20 builder.Write("Text in one cell");
21 builder.EndRow();
22
23 builder.InsertCell();
24 builder.CellFormat.Borders.LineStyle = LineStyle.Single;
25 builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
26 // 此单元格垂直合并到单元格上方,并应为空.
27 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous;
28 //builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
29
30 builder.InsertCell();
31 builder.CellFormat.Borders.LineStyle = LineStyle.Single;
32 builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
33 builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
34 builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
35 builder.Write("Text in another cell");
36 builder.EndRow();
37 doc.Save(filePath1, Aspose.Words.SaveFormat.Doc);
38 }