实际工作中,往往有这样的需求,需要导出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 
<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>源码地址: 
https://github.com/deeround/DocXCore <https://github.com/deeround/DocXCore>
上面demo源码:https://files.cnblogs.com/files/deeround/WordExportDemo.zip 
<https://files.cnblogs.com/files/deeround/WordExportDemo.zip>
 
热门工具 换一换