[原创]开源Word读写组件DocX,通过word模板,导出用户简历使用示例
入门请看:
【原创翻译】开源Word读写组件DocX介绍与入门[资料已发送]
我也是通过看上面的入门的。
1.DocX通过word模板批量导出用户简历
由于Docx有两种方法可以自定义属性:1.1通过word模板文件(在word模板中定义好自定义属性) 1.2 用代码创建word模板,并同时用代码创建自定义属性。
1.1通过word模板文件(在word模板中定义好自定义属性),自己新建一个模板文件。
每个要替换的部分,都定义成自定义属性
域代码如下:TAge 就为自定义属性名称
代码如下:
private void CreateInvoice() { DocX g_document; try {
//导入模板 g_document = DocX.Load(Server.MapPath(@"moban\Translator.docx")); //查数据,遍历。 DataTable dt = sqldb.GetDataTable("select * from test"); if (dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { //把需要填充的数据,替换模板中的信息,并保存 g_document = CreateInvoiceFromTemplate(DocX.Load(Server.MapPath(@"moban\Translator.docx")),dr); g_document.SaveAs(Server.MapPath(@"translatorTemp\" + dr["name"].ToString() + ".docx")); } } } catch (FileNotFoundException) { //如模板不存在时,先创建模板,再执行上班操作 //g_document = CreateInvoiceTemplate(); //g_document.Save(); //CreateInvoice(); } }
下面代码为填充数据方法
private DocX CreateInvoiceFromTemplate(DocX template,DataRow dr) { //为自定义属性赋值,CustomerProperty(name,values),name就是我们刚刚在word中定义的名称。values就是要填充进去的内容 #region Set CustomProperty values template.AddCustomProperty(new CustomProperty("Translatorno", dr["translator_no"].ToString())); template.AddCustomProperty(new CustomProperty("TName", dr["name"].ToString())); template.AddCustomProperty(new CustomProperty("TAge", dr["age"].ToString())); template.AddCustomProperty(new CustomProperty("TSex", dr["sex"].ToString())); template.AddCustomProperty(new CustomProperty("TNationality", dr["nationality"].ToString())); #endregion return template; }
1.2 用代码创建word模板,并同时用代码为word模板创建自定义属性。
private static DocX CreateInvoiceTemplate() { // 创建一个文档 DocX document = DocX.Create(@"docs\InvoiceTemplate.docx"); //先创建了一个表格 Table layout_table = document.InsertTable(2, 2); layout_table.Design = TableDesign.TableNormal; layout_table.AutoFit = AutoFit.Window; // 定义格式 Formatting dark_formatting = new Formatting(); dark_formatting.Bold = true; dark_formatting.Size = 12; dark_formatting.FontColor = Color.FromArgb(31, 73, 125); // 定义格式 Formatting light_formatting = new Formatting(); light_formatting.Italic = true; light_formatting.Size = 11; light_formatting.FontColor = Color.FromArgb(79, 129, 189); #region Company Name //取表格的第一行第一列的第一段落 Paragraph upper_left_paragraph = layout_table.Rows[0].Cells[0].Paragraphs[0]; // 新建一个自定义属性。其对应word中的内容是Translatorno为自定义属性名称,translatorno,为我们自己的word里面的内容 CustomProperty company_name = new CustomProperty("Translatorno", "translatorno"); // 加入自定义属性 layout_table.Rows[0].Cells[0].Paragraphs[0].InsertDocProperty(company_name, f: dark_formatting);
return document; }
2.资源
开源网址:http://docx.codeplex.com/
写篇博客不容易,烦躁的心情都见鬼去吧。兄弟们,对你有帮助,不要吝啬鼠标哦。
可以去下载示例代码。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库