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}

推荐阅读