Aspose.Words 操作 Word 畫 EChart 圖

使用 Aspose.Words 插件在 Word 畫 EChart 圖
使用此插件可以畫出豐富的 EChart 圖 , API 參考 https://reference.aspose.com/words/net/aspose.words.drawing.charts/charttype/
首先需要在 vs 中引入插件

Aspose.Words 操作 Word 畫 EChart 圖

文章插圖
代碼中添加引用
using Aspose.Words;using Aspose.Words.Drawing.Charts;using Aspose.Words.Tables;1、插入文字內容和表格
我們只需要獲取模板,并且創建一個文檔對象,將內容寫入進去即可
創建表格時,需要創建頭,規定每個單元格長度,寬度,將對應的數據庫字段對應上
Aspose.Words 操作 Word 畫 EChart 圖

文章插圖
Aspose.Words 操作 Word 畫 EChart 圖

文章插圖
1 DataTable dt = new DataTable(); 2 3//讀取 Word 模板 4Document doc = new Document("C:\\TestProject\\WebApplication1\\WebApplication1\\Word\\AsposeWord.doc"); 5 6//創建文檔對象 7DocumentBuilder builder = new DocumentBuilder(doc); 8 9#region 插入文字信息10//Word 中對應書簽的名稱11builder.MoveToBookmark("文章標題");1213//寫入內容14builder.Write("學會使用AsposeWord畫EChart圖");15#endregion1617#region 列表18builder.MoveToBookmark("學生信息列表");1920Aspose.Words.Tables.Table table3 = builder.StartTable();//開始畫Table21string[] columnheadersWD = { "序號", "姓名", "學科", "分數", "性別", "年齡" };22string[] columnnamesWD = { "ID", "TNAME", "DNAME", "SAL", "TSEX", "AGE" };23double[] colwidthsWD = { 50, 100, 100, 50, 50, 50 };2425dt = DBHelper.ExecuteDataTable("SELECT top 5 * FROM [testdb].[dbo].[TEACHER]");2627InsertTable(builder, table3, columnheadersWD, columnnamesWD, colwidthsWD, dt);2829//第三列的 第三行,第四行 合并30MergeCells(table3.Rows[3].Cells[2], table3.Rows[4].Cells[2]);31#endregion這里我們將畫表格的方法和合并單元格的方法封裝好了
Aspose.Words 操作 Word 畫 EChart 圖

文章插圖
Aspose.Words 操作 Word 畫 EChart 圖

文章插圖
1/// <summary>2/// 列表表格3/// </summary>4/// <param name="builder"></param>5/// <param name="table"></param>6/// <param name="columnheaders"></param>7/// <param name="columnnames"></param>8/// <param name="colwidths"></param>9/// <param name="statisTable"></param> 10public static void InsertTable(DocumentBuilder builder, Aspose.Words.Tables.Table table, string[] columnheaders, string[] columnnames, double[] colwidths, DataTable statisTable) 11{ 12var str = ""; 13 14#region 畫表頭 15for (int i = 0; i < columnheaders.Length; i++) 16{ 17str = columnheaders[i]; 18builder.InsertCell(); 19//Table單元格邊框線樣式 20builder.CellFormat.Borders.LineStyle = LineStyle.Single; 21//Table此單元格寬度 22builder.RowFormat.Height = 30; 23builder.CellFormat.Width = colwidths[i]; 24//此單元格中內容垂直對齊方式 25builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; 26builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; 27builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; 28//字體大小 29builder.Font.Size = 10; 30//是否加粗 31builder.Bold = false; 32//向此單元格中添加內容 33builder.Write(str); 34} 35builder.EndRow(); 36#endregion 37 38#region 畫數據 39for (int j = 0; j < statisTable.Rows.Count; j++) 40{ 41for (int i = 0; i < columnheaders.Length; i++) 42{ 43try 44{ 45str = statisTable.Rows[j][columnnames[i]].ToString(); 46} 47catch (Exception e) { str = ""; } 48builder.InsertCell(); 49//Table單元格邊框線樣式 50builder.CellFormat.Borders.LineStyle = LineStyle.Single; 51//Table此單元格寬度 52builder.RowFormat.Height = 30; 53builder.CellFormat.Width = colwidths[i]; 54 55//builder.CellFormat.Shading.ForegroundPatternColor = Color.Red;//設置單元格顏色 56 57//此單元格中內容垂直對齊方式 58builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center; 59builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None; 60builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None; 61//字體大小 62builder.Font.Size = 10; 63//是否加粗 64builder.Bold = false; 65//向此單元格中添加內容 66builder.Write(str); 67} 68builder.EndRow(); 69} 70#endregion 71 72//Table行結束 73builder.EndTable(); 74table.AutoFit(Aspose.Words.Tables.AutoFitBehavior.FixedColumnWidths); 75table.Alignment = Aspose.Words.Tables.TableAlignment.Center; 76} 77 78/// <summary> 79/// 合并單元格 80/// </summary> 81/// <param name="startCell"></param> 82/// <param name="endCell"></param> 83public static void MergeCells(Cell startCell, Cell endCell) 84{ 85Aspose.Words.Tables.Table parentTable = startCell.ParentRow.ParentTable; 86 87// Find the row and cell indices for the start and end cell. 88Point startCellPos = new Point(startCell.ParentRow.IndexOf(startCell), parentTable.IndexOf(startCell.ParentRow)); 89Point endCellPos = new Point(endCell.ParentRow.IndexOf(endCell), parentTable.IndexOf(endCell.ParentRow)); 90// Create the range of cells to be merged based off these indices. Inverse each index if the end cell if before the start cell. 91Rectangle mergeRange = new Rectangle(Math.Min(startCellPos.X, endCellPos.X), Math.Min(startCellPos.Y, endCellPos.Y), Math.Abs(endCellPos.X - startCellPos.X) + 1, 92Math.Abs(endCellPos.Y - startCellPos.Y) + 1); 93 94foreach (Row row in parentTable.Rows) 95{ 96foreach (Cell cell in row.Cells) 97{ 98Point currentPos = new Point(row.IndexOf(cell), parentTable.IndexOf(row)); 99100// Check if the current cell is inside our merge range then merge it.101if (mergeRange.Contains(currentPos))102{103if (currentPos.X == mergeRange.X)104cell.CellFormat.HorizontalMerge = CellMerge.First;105else106cell.CellFormat.HorizontalMerge = (CellMerge.Previous);107108if (currentPos.Y == mergeRange.Y)109cell.CellFormat.VerticalMerge = (CellMerge.First);110else111cell.CellFormat.VerticalMerge = (CellMerge.Previous);112}113}114}115}

推薦閱讀