Aspose.Words模板创建Word【一】

编程入门 行业动态 更新时间:2024-10-26 08:23:23

Aspose.Words<a href=https://www.elefans.com/category/jswz/34/1770549.html style=模板创建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【一】

本文发布于:2023-06-15 01:27:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/717669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   Aspose   Words   Word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!