• 开源Word读写组件DocX,通过word模板,导出用户简历使用示例


    [原创]开源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/

    写篇博客不容易,烦躁的心情都见鬼去吧。兄弟们,对你有帮助,不要吝啬鼠标哦。

    可以去下载示例代码。

     
     
  • 相关阅读:
    P1144 最短路计数 题解 最短路应用题
    C++高精度加减乘除模板
    HDU3746 Teacher YYF 题解 KMP算法
    POJ3080 Blue Jeans 题解 KMP算法
    POJ2185 Milking Grid 题解 KMP算法
    POJ2752 Seek the Name, Seek the Fame 题解 KMP算法
    POJ2406 Power Strings 题解 KMP算法
    HDU2087 剪花布条 题解 KMP算法
    eclipse创建maven项目(详细)
    maven的作用及优势
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2933188.html
Copyright © 2020-2023  润新知