NPOI创建DOCX常用操作1、 创建文档XWPFDocument m_Docx = new XWPFDocument();
2、 页面设置 //1‘=1440twip=25.4mm=72pt(磅point)=96px(像素pixel)
//1px(像素pixel)=0.75pt(磅point)
// A4:W=11906 twip=8.269''=210mm,h=16838twip=11.693''=297mm
//A5:W=8390 twip=5.827''=148mm,h=11906 twip=8.269''=210mm
//A6:W=5953 twip=4.134''=105mm,h=8390twip=5.827''=1148mm
//16k195mmX270mm:
//16k184mmX260mm:
//16k197mmX273mm:
CT_SectPr m_SectPr = newCT_SectPr();
//页面设置A4横向
m_SectPr.pgSz.w = (ulong)16838;
m_SectPr.pgSz.h = (ulong)11906;
m_Docx.Document.body.sectPr = m_SectPr;
3、 创建段落1) XWPFParagraph gp = m_Docx.CreateParagraph();
2) CT_Pm_p = m_Docx.Document.body.AddNewP();
m_p.AddNewPPr().AddNewJc().val = ST_Jc.center;//段落水平居中
XWPFParagraph gp = newXWPFParagraph(m_p, m_Docx); //创建XWPFParagraph
4、 段首行缩进gp.IndentationFirstLine=(int)100;
可以用一个函数计算
protected int Indentation(Stringfontname, int fontsize, int Indentationfonts, FontStylefs)
{
//字显示宽度,用于段首行缩进
//字号与fontsize关系
//初号(0号)=84,小初=72,1号=52,2号=44,小2=36,3号=32,小3=30,4号=28,
//小4=24,5号=21,小5=18,6号=15,小6=13,7号=11,8号=10
Graphicsm_tmpGr = this.CreateGraphics();
m_tmpGr.PageUnit = GraphicsUnit.Point;
SizeF size = m_tmpGr.MeasureString("好", new Font(fontname,fontsize * 0.75F, fs));
return (int)size.Width* Indentationfonts * 10;
}
gp.IndentationFirstLine= Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符
5、 行距设置//单倍为默认值(240twip)不需设置,1.5倍=240X1.5=360twip,2倍=240X2=480twipm_p.AddNewPPr().AddNewSpacing().line = "400";//行距固定20磅 m_p.AddNewPPr().AddNewSpacing().lineRule= ST_LineSpacingRule.exact;
6、 创建RUN 1) XWPFRun gr= gp.CreateRun();
gr.GetCTR().AddNewRPr().AddNewRFonts().ascii = "黑体";
gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑体";
gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia;
gr.GetCTR().AddNewRPr().AddNewSz().val = (ulong)44;//2号字体
gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)44;
gr.GetCTR().AddNewRPr().AddNewB().val = true;//加粗
gr.GetCTR().AddNewRPr().AddNewColor().val= "red";//字体颜色
gr.SetText("DOCX表");
2) CT_R= m_p.AddNewR();
7、 创建表1) 创建表有两种方法:a.方法1XWPFTabletable = m_Docx.CreateTable(1, 1);//创建1行1列表
CT_Tblm_CTTbl = m_Docx.Document.body.GetTblArray()[0];//获得文档第一张表
b.方法2
CT_Tblm_CTTbl = m_Docx.Document.body.AddNewTbl();
XWPFTabletable = new XWPFTable(m_CTTbl,m_Docx);//创建1行1列表
2) 表水平居中m_CTTbl.AddNewTblPr().jc = new CT_Jc();
m_CTTbl.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中
3) 表宽度m_CTTbl.AddNewTblPr().AddNewTblW().w = "2000";//表宽度
m_CTTbl.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa;
4) 表定位//若tblpXSpec、tblpX同时存在,则tblpXSpec优先tblpX;//若tblpYSpec、tblpY同时存在,则tblpYSpec优先tblpY;m_CTTblPr.tblpPr = new CT_TblPPr();//表定位
m_CTTblPr.tblpPr.tblpX = "4003";//表左上角坐标
m_CTTblPr.tblpPr.tblpY = "365";
//m_CTTblPr.tblpPr.tblpXSpec = ST_XAlign.center;// tblpXSpec优先tblpX
//m_CTTblPr.tblpPr.tblpYSpec = ST_YAlign.top;// tblpYSpec优先tblpY
m_CTTblPr.tblpPr.leftFromText = (ulong)180;
m_CTTblPr.tblpPr.rightFromText = (ulong)180;
m_CTTblPr.tblpPr.vertAnchor = ST_VAnchor.text;
m_CTTblPr.tblpPr.horzAnchor = ST_HAnchor.page;
5) 列宽设置//列宽设置
CT_TcPr m_Pr =table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr();
m_Pr.tcW = new CT_TblWidth();
m_Pr.tcW.w = "1500";//单元格宽
m_Pr.tcW.type = ST_TblWidth.dxa;
m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();
m_Pr.tcW = new CT_TblWidth();
m_Pr.tcW.w = "1000";//单元格宽
m_Pr.tcW.type = ST_TblWidth.dxa;
6) 创建行a. XWPFTableRow m_Row = table.CreateRow();//创建一行
b. XWPFTableRow m_Row = table.InsertNewTableRow(0);//表头插入一行
c. XWPFTableRow td3 = table.InsertNewTableRow(table.Rows.Count - 1);//插入行
d. CT_Row m_NewRow = new CT_Row();
XWPFTableRow m_Row = new XWPFTableRow(m_NewRow, table);
table.AddRow(m_Row);
7) 行高设置a. m_Row.GetCTRow().AddNewTrPr().AddNewTrHeight().val= (ulong)426;
b. m_NewRow.AddNewTrPr().AddNewTrHeight().val= (ulong)426;
8) 创建单元格a. XWPFTableCell cell = m_Row.CreateCell();//创建一单元格,创建单元格时就创建了一个CT_P
b. XWPFTableCell cell = m_Row.AddNewTableCell();//创建单元格时创建了一个CT_P
9) 单元格设置文字table.GetRow(0).GetCell(0).SetText("111");
10) 列合并//表增加行,合并列
CT_Row m_NewRow = new CT_Row();
XWPFTableRow m_Row = new XWPFTableRow(m_NewRow,table);
table.AddRow(m_Row);
XWPFTableCell cell = m_Row.CreateCell();
CT_Tc cttc = cell.GetCTTc();
CT_TcPr ctPr = cttc.AddNewTcPr();
ctPr.gridSpan = new CT_DecimalNumber();
ctPr.gridSpan.val = "3"; //合并3列
cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[0].AddNewR().AddNewT().Value = "sss";
11) 行合并//1行
CT_Row m_NewRow = new CT_Row();
XWPFTableRow m_Row = new XWPFTableRow(m_NewRow,table);
table.AddRow(m_Row);
XWPFTableCell cell = m_Row.CreateCell();
CT_Tc cttc = cell.GetCTTc();
CT_TcPr ctPr = cttc.AddNewTcPr();
ctPr.AddNewVMerge().val = ST_Merge.restart;//合并行
ctPr.AddNewVAlign().val = ST_VerticalJc.center;//垂直
cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[0].AddNewR().AddNewT().Value = "xxx";
//2行,多行合并类似
m_NewRow = new CT_Row();
m_Row = new XWPFTableRow(m_NewRow,table);
table.AddRow(m_Row);
cell = m_Row.CreateCell();
cttc = cell.GetCTTc();
ctPr = cttc.AddNewTcPr();
ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行
8、 插图1) 内联式插图(inline)此种插图方式对插入的图片位置不能灵活控制,只能通过段设置,对应word的嵌入型插图。宽和高数值换算:1cm=360000 EMUS(English Metric Unit)。 FileStream gfs = null;
gfs = new FileStream("f:\\pic\\1.jpg", FileMode.Open, FileAccess.Read);
m_p = m_Docx.Document.body.AddNewP();
m_p.AddNewPPr().AddNewJc().val = ST_Jc.center;//段落水平居中
gp = new XWPFParagraph(m_p,m_Docx);
gr = gp.CreateRun();
gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg",1000000, 1000000);
gfs.Close();
2) 锚式插图(anchor)此种插图方式对插入的图片位置能灵活控制,对应word的四周型、紧密型、穿越型等。图的左上角坐标及宽和高数值换算:1cm=360000 EMUS(English Metric Unit)。gfs = new FileStream("f:\\pic\\1.jpg", FileMode.Open, FileAccess.Read);
m_p = m_Docx.Document.body.AddNewP();
m_p.AddNewPPr().AddNewJc().val = ST_Jc.center;
gp = new XWPFParagraph(m_p,m_Docx);
gr = gp.CreateRun();
CT_Anchor an = newCT_Anchor();
//图片距正文上(distT)、下(distB)、左(distL)、右(distR)的距离。114300EMUS=3.1mm
an.distB = (uint)(0);
an.distL = 114300u;
an.distR = 114300U;
an.distT = 0U;
an.relativeHeight = 251658240u;
an.behindDoc = false; //"0",图与文字的上下关系
an.locked = false; //"0"
an.layoutInCell = true; //"1"
an.allowOverlap = true; //"1"
CT_Positive2D simplePos = new CT_Positive2D();
simplePos.x = (long)0;
simplePos.y = (long)0;
CT_EffectExtent effectExtent = new CT_EffectExtent();
effectExtent.b = 0L;
effectExtent.l = 0L;
effectExtent.r = 0L;
effectExtent.t = 0L;
//图左上角坐标
CT_PosH posH = newCT_PosH();
posH.relativeFrom = ST_RelFromH.column;
posH.posOffset = 4000000;//单位:EMUS,1CM=360000EMUS
CT_PosV posV = newCT_PosV();
posV.relativeFrom = ST_RelFromV.paragraph;
posV.posOffset = 200000;
a) 四周型CT_WrapSquare wrapSquare = new CT_WrapSquare();
wrapSquare.wrapText = ST_WrapText.bothSides;
gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg",1000000, 1000000,
posH, posV, wrapSquare,anchor,simplePos,effectExtent);
b) 紧密型CT_WrapTight wrapTight = new CT_WrapTight();
wrapTight.wrapText = ST_WrapText.bothSides;
wrapTight.wrapPolygon = new CT_WrapPath();
wrapTight.wrapPolygon.edited = false;
wrapTight.wrapPolygon.start = new CT_Positive2D();
wrapTight.wrapPolygon.start.x = 0;
wrapTight.wrapPolygon.start.y = 0;
CT_Positive2D lineTo = new CT_Positive2D();
wrapTight.wrapPolygon.lineTo = new List<CT_Positive2D>();
lineTo = new CT_Positive2D();
lineTo.x = 0;
lineTo.y = 21394;
wrapTight.wrapPolygon.lineTo.Add(lineTo);
lineTo = new CT_Positive2D();
lineTo.x = 21806;
lineTo.y = 21394;
wrapTight.wrapPolygon.lineTo.Add(lineTo);
lineTo = new CT_Positive2D();
lineTo.x = 21806;
lineTo.y = 0;
wrapTight.wrapPolygon.lineTo.Add(lineTo);
lineTo = new CT_Positive2D();
lineTo.x = 0;
lineTo.y = 0;
wrapTight.wrapPolygon.lineTo.Add(lineTo);
gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg",720000, 720000,
posH, posV, wrapTight, anchor, simplePos, effectExtent);
c) 穿越型CT_WrapThrough wrapThrough = new CT_WrapThrough();
wrapThrough.wrapText = ST_WrapText.bothSides;
wrapThrough.wrapPolygon = new CT_WrapPath();
wrapThrough.wrapPolygon.edited = false;
wrapThrough.wrapPolygon.start = new CT_Positive2D();
wrapThrough.wrapPolygon.start.x = 0;
wrapThrough.wrapPolygon.start.y = 0;
CT_Positive2D lineTo = new CT_Positive2D();
wrapThrough.wrapPolygon.lineTo = new List<CT_Positive2D>();
lineTo = new CT_Positive2D();
lineTo.x = 0;
lineTo.y = 21394;
wrapThrough.wrapPolygon.lineTo.Add(lineTo);
lineTo = new CT_Positive2D();
lineTo.x = 21806;
lineTo.y = 21394;
wrapThrough.wrapPolygon.lineTo.Add(lineTo);
lineTo = new CT_Positive2D();
lineTo.x = 21806;
lineTo.y = 0;
wrapThrough.wrapPolygon.lineTo.Add(lineTo);
lineTo = new CT_Positive2D();
lineTo.x = 0;
lineTo.y = 0;
wrapThrough.wrapPolygon.lineTo.Add(lineTo);
gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg",720000, 720000,
posH, posV, wrapThrough, anchor, simplePos, effectExtent);