//using NPOI.XWPF.UserModel;
namespace OASystem.API.OAMethodLib.NPOI
{
///
/// word / excel
///
public class NPOIHelper
{
//#region Word
/////
///// 输出模板docx文档(使用字典)
/////
///// docx文件路径
///// 输出文件路径
///// 字典数据源
//public static void Export(string tempFilePath, string outPath, Dictionary 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 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);
// }
//}
/////
///// 输出模板docx文档(使用反射)
/////
///// docx文件路径
///// 输出文件路径
///// 对象数据源
//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
}
}