NPOIHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //using NPOI.XWPF.UserModel;
  2. namespace OASystem.API.OAMethodLib.NPOI
  3. {
  4. /// <summary>
  5. /// word / excel
  6. /// </summary>
  7. public class NPOIHelper
  8. {
  9. //#region Word
  10. ///// <summary>
  11. ///// 输出模板docx文档(使用字典)
  12. ///// </summary>
  13. ///// <param name="tempFilePath">docx文件路径</param>
  14. ///// <param name="outPath">输出文件路径</param>
  15. ///// <param name="data">字典数据源</param>
  16. //public static void Export(string tempFilePath, string outPath, Dictionary<string, string> data)
  17. //{
  18. // try
  19. // {
  20. // using (FileStream stream = File.OpenRead(tempFilePath ))
  21. // {
  22. // XWPFDocument doc = new XWPFDocument(stream);
  23. // //遍历段落
  24. // foreach (var para in doc.Paragraphs)
  25. // {
  26. // ReplaceKey(para, data);
  27. // }
  28. // //遍历表格
  29. // foreach (var table in doc.Tables)
  30. // {
  31. // foreach (var row in table.Rows)
  32. // {
  33. // foreach (var cell in row.GetTableCells())
  34. // {
  35. // foreach (var para in cell.Paragraphs)
  36. // {
  37. // ReplaceKey(para, data);
  38. // }
  39. // }
  40. // }
  41. // }
  42. // //写文件
  43. // FileStream outFile = new FileStream(outPath, FileMode.Create);
  44. // doc.Write(outFile);
  45. // outFile.Close();
  46. // }
  47. // }
  48. // catch (Exception ex)
  49. // {
  50. // throw;
  51. // }
  52. //}
  53. //private static void ReplaceKey(XWPFParagraph para, Dictionary<string, string> data)
  54. //{
  55. // string text = "";
  56. // foreach (var run in para.Runs)
  57. // {
  58. // text = run.ToString();
  59. // foreach (var key in data.Keys)
  60. // {
  61. // //$$模板中数据占位符为$KEY$
  62. // if (text.Contains($"${key}$"))
  63. // {
  64. // text = text.Replace($"${key}$", data[key]);
  65. // }
  66. // }
  67. // run.SetText(text, 0);
  68. // }
  69. //}
  70. ///// <summary>
  71. ///// 输出模板docx文档(使用反射)
  72. ///// </summary>
  73. ///// <param name="tempFilePath">docx文件路径</param>
  74. ///// <param name="outPath">输出文件路径</param>
  75. ///// <param name="data">对象数据源</param>
  76. //public static void ExportObjet(string tempFilePath, string outPath, object data)
  77. //{
  78. // using (FileStream stream = File.OpenRead(tempFilePath))
  79. // {
  80. // XWPFDocument doc = new XWPFDocument(stream);
  81. // //遍历段落
  82. // foreach (var para in doc.Paragraphs)
  83. // {
  84. // ReplaceKeyObjet(para, data);
  85. // }
  86. // //遍历表格
  87. // foreach (var table in doc.Tables)
  88. // {
  89. // foreach (var row in table.Rows)
  90. // {
  91. // foreach (var cell in row.GetTableCells())
  92. // {
  93. // foreach (var para in cell.Paragraphs)
  94. // {
  95. // ReplaceKeyObjet(para, data);
  96. // }
  97. // }
  98. // }
  99. // }
  100. // //写文件
  101. // FileStream outFile = new FileStream(outPath, FileMode.Create);
  102. // doc.Write(outFile);
  103. // outFile.Close();
  104. // }
  105. //}
  106. //private static void ReplaceKeyObjet(XWPFParagraph para, object model)
  107. //{
  108. // string text = "";
  109. // Type t = model.GetType();
  110. // PropertyInfo[] pi = t.GetProperties();
  111. // foreach (var run in para.Runs)
  112. // {
  113. // text = run.ToString();
  114. // foreach (PropertyInfo p in pi)
  115. // {
  116. // //$$模板中数据占位符为$KEY$
  117. // string key = $"${p.Name}$";
  118. // if (text.Contains(key))
  119. // {
  120. // try
  121. // {
  122. // text = text.Replace(key, p.GetValue(model, null).ToString());
  123. // }
  124. // catch (Exception ex)
  125. // {
  126. // //可能有空指针异常
  127. // text = text.Replace(key, "");
  128. // }
  129. // }
  130. // }
  131. // run.SetText(text, 0);
  132. // }
  133. //}
  134. //#endregion
  135. }
  136. }