|
@@ -0,0 +1,146 @@
|
|
|
+//using NPOI.XWPF.UserModel;
|
|
|
+
|
|
|
+namespace OASystem.API.OAMethodLib.NPOI
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// word / excel
|
|
|
+ /// </summary>
|
|
|
+ public class NPOIHelper
|
|
|
+ {
|
|
|
+ //#region Word
|
|
|
+ ///// <summary>
|
|
|
+ ///// 输出模板docx文档(使用字典)
|
|
|
+ ///// </summary>
|
|
|
+ ///// <param name="tempFilePath">docx文件路径</param>
|
|
|
+ ///// <param name="outPath">输出文件路径</param>
|
|
|
+ ///// <param name="data">字典数据源</param>
|
|
|
+ //public static void Export(string tempFilePath, string outPath, Dictionary<string, string> data)
|
|
|
+ //{
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // using (FileStream stream = File.OpenRead(tempFilePath ))
|
|
|
+ // {
|
|
|
+ // XWPFDocument doc = new XWPFDocument(stream);
|
|
|
+ // //遍历段落
|
|
|
+ // foreach (var para in doc.Paragraphs)
|
|
|
+ // {
|
|
|
+ // ReplaceKey(para, data);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // //遍历表格
|
|
|
+ // foreach (var table in doc.Tables)
|
|
|
+ // {
|
|
|
+ // foreach (var row in table.Rows)
|
|
|
+ // {
|
|
|
+ // foreach (var cell in row.GetTableCells())
|
|
|
+ // {
|
|
|
+ // foreach (var para in cell.Paragraphs)
|
|
|
+ // {
|
|
|
+ // ReplaceKey(para, data);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // //写文件
|
|
|
+ // FileStream outFile = new FileStream(outPath, FileMode.Create);
|
|
|
+ // doc.Write(outFile);
|
|
|
+ // outFile.Close();
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // catch (Exception ex)
|
|
|
+ // {
|
|
|
+
|
|
|
+ // throw;
|
|
|
+ // }
|
|
|
+
|
|
|
+ //}
|
|
|
+ //private static void ReplaceKey(XWPFParagraph para, Dictionary<string, string> data)
|
|
|
+ //{
|
|
|
+ // string text = "";
|
|
|
+ // foreach (var run in para.Runs)
|
|
|
+ // {
|
|
|
+ // text = run.ToString();
|
|
|
+ // foreach (var key in data.Keys)
|
|
|
+ // {
|
|
|
+ // //$$模板中数据占位符为$KEY$
|
|
|
+ // if (text.Contains($"${key}$"))
|
|
|
+ // {
|
|
|
+ // text = text.Replace($"${key}$", data[key]);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // run.SetText(text, 0);
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+
|
|
|
+ ///// <summary>
|
|
|
+ ///// 输出模板docx文档(使用反射)
|
|
|
+ ///// </summary>
|
|
|
+ ///// <param name="tempFilePath">docx文件路径</param>
|
|
|
+ ///// <param name="outPath">输出文件路径</param>
|
|
|
+ ///// <param name="data">对象数据源</param>
|
|
|
+ //public static void ExportObjet(string tempFilePath, string outPath, object data)
|
|
|
+ //{
|
|
|
+ // using (FileStream stream = File.OpenRead(tempFilePath))
|
|
|
+ // {
|
|
|
+ // XWPFDocument doc = new XWPFDocument(stream);
|
|
|
+ // //遍历段落
|
|
|
+ // foreach (var para in doc.Paragraphs)
|
|
|
+ // {
|
|
|
+ // ReplaceKeyObjet(para, data);
|
|
|
+ // }
|
|
|
+ // //遍历表格
|
|
|
+ // foreach (var table in doc.Tables)
|
|
|
+ // {
|
|
|
+ // foreach (var row in table.Rows)
|
|
|
+ // {
|
|
|
+ // foreach (var cell in row.GetTableCells())
|
|
|
+ // {
|
|
|
+ // foreach (var para in cell.Paragraphs)
|
|
|
+ // {
|
|
|
+ // ReplaceKeyObjet(para, data);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // //写文件
|
|
|
+ // FileStream outFile = new FileStream(outPath, FileMode.Create);
|
|
|
+ // doc.Write(outFile);
|
|
|
+ // outFile.Close();
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ //private static void ReplaceKeyObjet(XWPFParagraph para, object model)
|
|
|
+ //{
|
|
|
+ // string text = "";
|
|
|
+ // Type t = model.GetType();
|
|
|
+ // PropertyInfo[] pi = t.GetProperties();
|
|
|
+ // foreach (var run in para.Runs)
|
|
|
+ // {
|
|
|
+ // text = run.ToString();
|
|
|
+ // foreach (PropertyInfo p in pi)
|
|
|
+ // {
|
|
|
+ // //$$模板中数据占位符为$KEY$
|
|
|
+ // string key = $"${p.Name}$";
|
|
|
+ // if (text.Contains(key))
|
|
|
+ // {
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // text = text.Replace(key, p.GetValue(model, null).ToString());
|
|
|
+ // }
|
|
|
+ // catch (Exception ex)
|
|
|
+ // {
|
|
|
+ // //可能有空指针异常
|
|
|
+ // text = text.Replace(key, "");
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // run.SetText(text, 0);
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ //#endregion
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|