using NPOI.XWPF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Windows; using NPOI.OpenXmlFormats.Wordprocessing; using TextAlignment = NPOI.XWPF.UserModel.TextAlignment; namespace QuiltingCommon.Utilities { public class WordHelper { public void InsertTextToBookmark(XWPFDocument doc, string bookmark, string text) { //BLL.XmxxBLL XmxxBLL = new BLL.XmxxBLL(); //Model.Xmxx model = new Model.Xmxx(); //model = XmxxBLL.GetModel(20); //string text = para.ParagraphText; //var runs = para.Runs; //string styleid = para.Style; //for (int i = 0; i < runs.Count; i++) //{ // var run = runs[i]; // text = run.ToString(); // Type t = model.GetType(); // PropertyInfo[] pi = t.GetProperties(); // foreach (PropertyInfo p in pi) // { // if (text.Contains("{$xmxx." + p.Name + "}")) // { // text = text.Replace("{$xmxx." + p.Name + "}", TM.Common.StringHelper.ToString(p.GetValue(model, null))); // } // } // runs[i].SetText(text, 0); //} } /// /// 创建行 /// /// /// private XWPFTableRow CreateRow(XWPFTable table) { CT_Row m_NewRow = new CT_Row(); XWPFTableRow m_row = new XWPFTableRow(m_NewRow, table); m_row.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)426; table.AddRow(m_row); return m_row; } /// /// 创建列 /// /// /// private XWPFTableCell CreateCell(XWPFTableRow row) { XWPFTableCell cell = row.CreateCell(); CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctpr = cttc.AddNewTcPr(); cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//水平居中 ctpr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中 ctpr.tcW = new CT_TblWidth(); ctpr.tcW.w = "1200";//默认列宽 ctpr.tcW.type = ST_TblWidth.dxa; return cell; } /// /// 合并单元格 /// /// /// private void SetColSpan(XWPFTableCell cell, int Span) { CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctpr = cttc.AddNewTcPr(); ctpr.gridSpan = new CT_DecimalNumber(); ctpr.gridSpan.val = Span.ToString();//合并单元格 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//水平居中 ctpr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中 } /// /// 设置列宽 /// /// 表格 /// 列号。从0开始编号 /// 列宽 private void SetColWith(XWPFTable table, int ColIndex, int Width) { CT_TcPr m_pr = table.GetRow(0).GetCell(ColIndex).GetCTTc().AddNewTcPr(); m_pr.tcW = new CT_TblWidth(); m_pr.tcW.w = Width.ToString(); m_pr.tcW.type = ST_TblWidth.dxa; } /// /// 设置字体格式 /// /// /// /// /// public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText) { //table中的文字格式设置 CT_P para = new CT_P(); XWPFParagraph pCell = new XWPFParagraph(para, table.Body); pCell.SetAlignment(ParagraphAlignment.CENTER);//字体居中 pCell.SetVerticalAlignment(TextAlignment.CENTER);//字体居中 XWPFRun r1c1 = pCell.CreateRun(); r1c1.SetText(setText); r1c1.SetFontSize(12); r1c1.SetFontFamily("华文楷体");//设置雅黑字体 return pCell; } /// /// 设置单元格格式 /// /// doc对象 /// 表格对象 /// 要填充的文字 /// 文字对齐方式 /// rows行的高度 /// public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align, int textPos) { CT_P para = new CT_P(); XWPFParagraph pCell = new XWPFParagraph(para, table.Body); pCell.SetAlignment(align);//字体居中 XWPFRun r1c1 = pCell.CreateRun(); r1c1.SetText(setText); r1c1.SetFontSize(12); r1c1.SetFontFamily("华文楷体");//设置雅黑字体 r1c1.SetTextPosition(textPos);//设置高度 return pCell; } } }