模板创建Word【一】"/>
Aspose.Words模板创建Word【一】
概要:
最近在项目需求当中,用到了在线预览类似人员档案,导出word 或pdf文件等。下面就来记录一下通过Aspose.Words插件实现导出word文件。
说明:
本篇文章内容是记录通过Aspose.Words类库,利用word模板 实现静态的创建和填充word文档(即:通过Bookmark书签填充文档内容)。
第一步:制作word模板。
第二步:添加bookmark书签。
例子:给工号对应的单元格添加【GongHao】书签名。
第三部:创建控制台项目引用Aspose.Words破解版插件无水印广告
项目代码如下:
class Program
{static void Main(string[] args){string templatePath = @"C:\Users\JeterJing\Desktop\Aspose.Words\template\template.docx";string targetPath = @"C:\Users\JeterJing\Desktop\Aspose.Words\create-example\" + Guid.NewGuid().ToString() + ".docx";CreateDocumentByBookmark(templatePath, targetPath);Console.WriteLine("创建成功!请按照任意键结束。");Console.ReadKey(true);}/// <summary>/// 根据模板word创建指定文档/// </summary>/// <param name="templatePath"></param>/// <param name="targetPath"></param>private static void CreateDocumentByBookmark(string templatePath, string targetPath){//加载word模板文件Document doc = new Document(templatePath);DocumentBuilder builder = new DocumentBuilder(doc);//两种形式填充内容://第一种:// 书签名为【GongHao】如果不存在,则会有空引用异常。if (doc.Range.Bookmarks["GongHao"] != null){//光标选中签名为:【GongHao】区域Bookmark bookmark = doc.Range.Bookmarks["GongHao"];//写入内容bookmark.Text = "A1001";}//第二种:// 通过移动光标到指定的书签名,写入内容builder.MoveToBookmark("XingMing");builder.Write("张飞");doc.Save(targetPath, SaveFormat.Docx);}
}
第四步:运行项目结果。
第五步:查看文档生成情况。
总结:由于这种生成比较简单,因此Demo解决方案就不上传了,下一篇将介绍:动态添加表格中的数据。
更多推荐
Aspose.Words模板创建Word【一】
发布评论