Net Core DocXCore 實現word模板導出

  • 2019 年 10 月 3 日
  • 筆記

實際工作中,往往有這樣的需求,需要導出word,還有各種各樣的樣式,於是有了word模板導出。

實現以下幾個需求:

1、表單導出

2、表格導出

3、表單表格混合導出

4、實際用例測試

解決方案:

實現是基於NET Core 2.1 ,搜索了各個開源項目最終基於DocX這個開源庫,當初實現時發現DocX作者並沒有發布Core的版本,最後在Nuget搜索到DocXCore這個包,但是沒有GitHub搜索到這個庫。

上面還遇到一個坑爹的問題,系統在win運行沒問題,一部署到centos導出就掛了,根據錯誤研究發現裡面居然要獲取當前登錄的用戶資訊,win系統沒有問題,centos報錯,於是去掉獲取系統用戶這塊,居然沒有源碼。

一怒之下,反編譯了DocXCore包,移除了獲取登錄系統程式碼,最終win和centos都導出正常。

奉上源碼地址:https://github.com/deeround/DocXCore

1、表單導出

模板

 

程式碼

 1     public class FormTest   2     {   3         public static void Test()   4         {   5             Console.WriteLine($"表單");   6             Stopwatch sw = new Stopwatch();   7             Dictionary<string, object> data = new Dictionary<string, object>()   8             {   9                 { "xmmc","測試姓名測試姓名111"},  10                 { "sqje","1417.4"},  11                 { "xmdw","部落格園Deeround"},  12                 { "glfs","自行管理方式"},  13                 { "xmgk","部落格園Deeround來函申請辦理 應急搶險治理工程項目竣工結(決)算,該項目已完工並通過項目初步驗收,現擬按程式採取政府購買服務方式開展評審"},  14                 { "psyj",""},  15                 { "gzyq", @"(一)對建設程式進行評審,包括可行性研究報告、初步設計等批准文件的程式性審查。  16 (二)對建設規模、建設標準、可研執行情況等進行評審。  17 (三)對工程投資進行評審,包括工程計量、定額選用、材料價格及費用標準等的評審。  18 (四)對設施設備資進行評審,包括設施設備型號、規格、數量及價格的評審。  19 "},  20                 { "wcsx","1. 收到委託書後在10天內報送評審方案,評審完成後需提交評審報告紙質件7份及電子文檔。"},  21                 { "ywcs","伯爵二元"},  22                 { "lxr","千年  12345678"},  23             };  24  25             sw.Start();  26             string root = System.AppDomain.CurrentDomain.BaseDirectory;  27             WordHelper.Export(root + Path.Combine("Templates", "temp_form.docx"), root + "temp_form_out.docx", data);  28             sw.Stop();  29             var time = sw.ElapsedMilliseconds;  30             Console.WriteLine($"耗時:{time}毫秒");  31         }  32     }

最終效果

 2、表格導出

模板

 

 

 程式碼

 1     public class TableListTest   2     {   3         public static void Test(int count = 10)   4         {   5             Console.WriteLine($"表格");   6             Stopwatch sw = new Stopwatch();   7             IList<Dictionary<string, object>> data = new List<Dictionary<string, object>>();   8             for (int i = 0; i < count; i++)   9             {  10                 Dictionary<string, object> d = new Dictionary<string, object>()  11                 {  12                     { "xm","測試"+i.ToString()},  13                     { "nl",i},  14                     { "xb",""}  15                 };  16                 data.Add(d);  17             }  18  19             Dictionary<string, object> data1 = new Dictionary<string, object>();  20             data1.Add("list", data);  21             sw.Start();  22             string root = System.AppDomain.CurrentDomain.BaseDirectory;  23             WordHelper.Export(root + Path.Combine("Templates", "temp_table_list.docx"), root + "temp_table_list_out.docx", data1);  24             sw.Stop();  25             var time = sw.ElapsedMilliseconds;  26             Console.WriteLine($"耗時:{time}毫秒");  27         }  28     }

 

最終效果

 

 

 3、表單表格混合導出

模板

 

 

 程式碼

 1 public class FormTableTest   2     {   3         public static void Test()   4         {   5             Console.WriteLine($"表單表格混合");   6             Stopwatch sw = new Stopwatch();   7             Dictionary<string, object> data = new Dictionary<string, object>()   8             {   9                 { "xmmc","測試姓名測試姓名111"},  10                 { "sqje","1417.4"},  11                 { "xmdw","部落格園Deeround"},  12                 { "glfs","自行管理方式"},  13                 { "xmgk","部落格園Deeround來函申請辦理 應急搶險治理工程項目竣工結(決)算,該項目已完工並通過項目初步驗收,現擬按程式採取政府購買服務方式開展評審"},  14                 { "psyj",""},  15                 { "gzyq", @"(一)對建設程式進行評審,包括可行性研究報告、初步設計等批准文件的程式性審查。  16 (二)對建設規模、建設標準、可研執行情況等進行評審。  17 (三)對工程投資進行評審,包括工程計量、定額選用、材料價格及費用標準等的評審。  18 (四)對設施設備資進行評審,包括設施設備型號、規格、數量及價格的評審。  19 "},  20                 { "wcsx","1. 收到委託書後在10天內報送評審方案,評審完成後需提交評審報告紙質件7份及電子文檔。"},  21                 { "ywcs","測試處"},  22                 { "lxr","李  123456"},  23             };  24             //明細數據  25             IList<Dictionary<string, object>> mx = new List<Dictionary<string, object>>();  26             for (int i = 0; i < 10; i++)  27             {  28                 mx.Add(new Dictionary<string, object>() {  29                     { "a",i},  30                     { "b","項目概況表項目概況表項目概況表項目概況表項目概況表"},  31                     { "c","評審中"},  32                 });  33             }  34             data.Add("mx", mx);  35             sw.Start();  36             string root = System.AppDomain.CurrentDomain.BaseDirectory;  37             WordHelper.Export(root + Path.Combine("Templates", "temp_form_table.docx"), root + "temp_form_table_out.docx", data);  38             sw.Stop();  39             var time = sw.ElapsedMilliseconds;  40             Console.WriteLine($"耗時:{time}毫秒");  41         }  42     }

最終效果

4、實例

請看源碼

 

簡單說明:

採用字元串模板方式替換形式,之前也用過其他方式設置參數,多多少少會遇到些坑,還不如自定義字元串靈活。

#:普通表單關鍵字使用#包裹

$:表格關鍵字使用$包裹,裡面使用.分割

 

源碼下載:

 DocXCore源碼地址: https://github.com/deeround/DocXCore

上面demo源碼:https://files.cnblogs.com/files/deeround/WordExportDemo.zip   或者   https://github.com/deeround/mydemo