[原創]開源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/

寫篇博客不容易,煩躁的心情都見鬼去吧。兄弟們,對你有幫助,不要吝嗇鼠標哦。

可以去下載示例代碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM