控制word页内边距
//创建document对象
var doc = new XWPFDocument();
doc.Document.body.sectPr = new CT_SectPr();
CT_SectPr m_SectPr = doc.Document.body.sectPr;
m_SectPr.pgSz.h = (ulong)16838;
m_SectPr.pgSz.w = (ulong)11906;
m_SectPr.pgMar.left = (ulong)800;//左边距
m_SectPr.pgMar.right = (ulong)800;//右边距
m_SectPr.pgMar.top = "850";//上边距
m_SectPr.pgMar.bottom = "850";//下边距
创建段落
var paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER; //字体居中
var run = paragraph.CreateRun();
run.IsBold = true;
run.SetText(contend);
run.FontSize = 28;
run.SetFontFamily("黑体", FontCharRange.None); //设置黑体
控制段落与其他元素的上下距离
paragraph.SpacingBeforeLines = 20;//上方距离
paragraph.SpacingAfterLines = 20;//下方距离
换页(本页未满直接写下一页)
paragraph = doc.CreateParagraph();
paragraph.CreateRun().AddBreak(BreakType.PAGE);
创建表格
var table = doc.CreateTable(行数, 列数);
table.Width = 5000;
控制表格中列宽(这里需要注意,只设置一行的列宽一旦插入文字就会使设置的列宽失效,所以要把每一个单元格都要设置上)
for (int r = 0; r < 8; r++)
{
SetCellWith(table.GetRow(r).GetCell(0), "600");
SetCellWith(table.GetRow(r).GetCell(1), "1500");
SetCellWith(table.GetRow(r).GetCell(2), "1500");
SetCellWith(table.GetRow(r).GetCell(3), "600");
}
设置表格中单元格竖直居中
var rowcell = table.GetRow(r).GetCell(c);
rowcell.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
设置单元格内容
//table中的文字格式设置
var para = new CT_P();
var pCell = new XWPFParagraph(para, table.Body);
pCell.Alignment = paragraphAlignment; //字体居中
pCell.VerticalAlignment = TextAlignment.CENTER; //字体居中
var r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = 11;
r1c1.SetFontFamily("宋体", FontCharRange.None); //设置雅黑字体
pCell.SpacingAfterLines = 40;
pCell.SpacingBeforeLines = 40;
//放入单元格
table.GetRow(4).GetCell(1).SetParagraph(pCell);
表格中换行(这个版本,网上其他的方法我都试了不好用最后参考的poi)
var run= paragraph.CreateRun();
run.SetText(contends[i]);
run.FontSize = 11;
run.SetFontFamily("宋体", FontCharRange.None);
run.AddBreak(BreakType.TEXTWRAPPING);//换行
合并单元格
table.GetRow(rowIndex).MergeCells(fromCol, toCol);//合并列
//合并行
public void MYMergeRows(XWPFTable table, int fromRow, int toRow, int colIndex)
{
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
{
XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(colIndex);
rowcell.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
CT_Tc cttc = rowcell.GetCTTc();
if (cttc.tcPr == null)
{
cttc.AddNewTcPr();
}
if (rowIndex == fromRow)
{
// The first merged cell is set with RESTART merge value
rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.restart;
}
else
{
// Cells which join (merge) the first one, are set with CONTINUE
rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.@continue;
}
}
}
字号字典
控制word页内边距
//创建document对象
var doc = new XWPFDocument();
doc.Document.body.sectPr = new CT_SectPr();
CT_SectPr m_SectPr = doc.Document.body.sectPr;
m_SectPr.pgSz.h = (ulong)16838;
m_SectPr.pgSz.w = (ulong)11906;
m_SectPr.pgMar.left = (ulong)800;//左边距
m_SectPr.pgMar.right = (ulong)800;//右边距
m_SectPr.pgMar.top = "850";//上边距
m_SectPr.pgMar.bottom = "850";//下边距
创建段落
var paragraph = doc.CreateParagraph();
paragraph.Alignment = ParagraphAlignment.CENTER; //字体居中
var run = paragraph.CreateRun();
run.IsBold = true;
run.SetText(contend);
run.FontSize = 28;
run.SetFontFamily("黑体", FontCharRange.None); //设置黑体
控制段落与其他元素的上下距离
paragraph.SpacingBeforeLines = 20;//上方距离
paragraph.SpacingAfterLines = 20;//下方距离
换页(本页未满直接写下一页)
paragraph = doc.CreateParagraph();
paragraph.CreateRun().AddBreak(BreakType.PAGE);
创建表格
var table = doc.CreateTable(行数, 列数);
table.Width = 5000;
控制表格中列宽(这里需要注意,只设置一行的列宽一旦插入文字就会使设置的列宽失效,所以要把每一个单元格都要设置上)
for (int r = 0; r < 8; r++)
{
SetCellWith(table.GetRow(r).GetCell(0), "600");
SetCellWith(table.GetRow(r).GetCell(1), "1500");
SetCellWith(table.GetRow(r).GetCell(2), "1500");
SetCellWith(table.GetRow(r).GetCell(3), "600");
}
设置表格中单元格竖直居中
var rowcell = table.GetRow(r).GetCell(c);
rowcell.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
设置单元格内容
//table中的文字格式设置
var para = new CT_P();
var pCell = new XWPFParagraph(para, table.Body);
pCell.Alignment = paragraphAlignment; //字体居中
pCell.VerticalAlignment = TextAlignment.CENTER; //字体居中
var r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = 11;
r1c1.SetFontFamily("宋体", FontCharRange.None); //设置雅黑字体
pCell.SpacingAfterLines = 40;
pCell.SpacingBeforeLines = 40;
//放入单元格
table.GetRow(4).GetCell(1).SetParagraph(pCell);
表格中换行(这个版本,网上其他的方法我都试了不好用最后参考的poi)
var run= paragraph.CreateRun();
run.SetText(contends[i]);
run.FontSize = 11;
run.SetFontFamily("宋体", FontCharRange.None);
run.AddBreak(BreakType.TEXTWRAPPING);//换行
合并单元格
table.GetRow(rowIndex).MergeCells(fromCol, toCol);//合并列
//合并行
public void MYMergeRows(XWPFTable table, int fromRow, int toRow, int colIndex)
{
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
{
XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(colIndex);
rowcell.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
CT_Tc cttc = rowcell.GetCTTc();
if (cttc.tcPr == null)
{
cttc.AddNewTcPr();
}
if (rowIndex == fromRow)
{
// The first merged cell is set with RESTART merge value
rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.restart;
}
else
{
// Cells which join (merge) the first one, are set with CONTINUE
rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.@continue;
}
}
}
字号字典