需要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 }