123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //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
- }
- }
|