概要
利用Aspose.words插件,给word文档表格动态批量添加行数据,这种方式和上一篇有点区别,
上一篇提到过,仔细想了想还是追加一篇学习记录吧,当写个总结了(注意:实现创建的word
文档,都是事先制作好word模板文件)。
回到正题:
第一步:制作word模板。
第二步:代码实现创建新的word文档。
class Program
{static void Main(string[] args){string templatePath = @"C:\Users\JeterJing\Desktop\Aspose.Words\template\template1.docx";string targetPath = @"C:\Users\JeterJing\Desktop\Aspose.Words\create-example\" + Guid.NewGuid().ToString() + ".docx";string message = string.Empty;FillWordTableRowData(templatePath, targetPath);Console.WriteLine("创建成功!请按照任意键结束。");Console.ReadKey(true);}/// <summary>/// 动态填充表格数据/// </summary>/// <param name="templatePath"></param>/// <param name="targetPath"></param>private static void FillWordTableRowData(string templatePath, string targetPath){Document doc = new Document(templatePath);DocumentBuilder builder = new DocumentBuilder(doc);//获取表格节点集合NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);//拿到第一个表格:tableindex=0Table table = tables[0] as Table;//拿到第三行的row模板:rowindex=2var roww = table.Rows[2];#region 动态添加表格中的数据for (int i = 0; i < 3; i++){#region 第一种方式填充数据//复制第3行var row = roww.Clone(true);//将复制的行插入当前行的上方table.Rows.Insert(2 + i, row);//光标移动到:第一个表格的,第3行的第1列的格子里,即:资质编号builder.MoveToCell(0, 2 + i, 0, 0);//给资质编号单元格填充内容builder.Write("A001");//给资质名称单元格填充内容builder.MoveToCell(0, 2 + i, 1, 0);builder.Write("临床技术资质");//给科室审核填充内容builder.MoveToCell(0, 2 + i, 2, 0);builder.Write("已审核");//给院级审核填充内容builder.MoveToCell(0, 2 + i, 3, 0);builder.Write("已审核");//给发放日期填充内容builder.MoveToCell(0, 2 + i, 4, 0);builder.Write(DateTime.Now.ToString("yyyy-MM-dd"));#endregion}#endregiondoc.Save(targetPath, SaveFormat.Docx);}
}
第三步:运行项目查看结果。
第四步:查看新生成的word情况。
总结:
这里两篇都提到了,动态word文档中批量填充表格中的行数据。
(1)代码方式有所不同。
(2)模板的样式也有区别。
Aspose.Words模板创建Word【二】