You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.4 KiB

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);
//}
}
/// <summary>
/// 创建行
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 创建列
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 合并单元格
/// </summary>
/// <param name="cell"></param>
/// <param name="Span"></param>
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;//垂直居中
}
/// <summary>
/// 设置列宽
/// </summary>
/// <param name="table">表格</param>
/// <param name="ColIndex">列号。从0开始编号</param>
/// <param name="Width">列宽</param>
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;
}
/// <summary>
/// 设置字体格式
/// </summary>
/// <param name="doc"></param>
/// <param name="table"></param>
/// <param name="setText"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 设置单元格格式
/// </summary>
/// <param name="doc">doc对象</param>
/// <param name="table">表格对象</param>
/// <param name="setText">要填充的文字</param>
/// <param name="align">文字对齐方式</param>
/// <param name="textPos">rows行的高度</param>
/// <returns></returns>
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;
}
}
}