NPOI已出现一段时间了,目前版本2.0 Beta 2 [v2.0.5],网上关于NPOI操作xlsx文章较多,而关于docx的几乎没有,尽管NPOI对于Word还不稳定,经过一阵捣鼓后终于实现了表的简单操作:创建表、创建行、创建单元,单元行和列的合并。
环境:vs2010,netframework4
具体代码:
private void button1_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
XWPFDocument m_Docx = new XWPFDocument();
m_Docx = CreatDocxTable();
m_Docx.Write(ms);
ms.Flush();
SaveToFile(ms,"d:\\test.docx");
}
protected XWPFDocument CreatDocxTable()
{
XWPFDocument m_Docx = new XWPFDocument();
XWPFParagraph p0 = m_Docx.CreateParagraph();
XWPFRun r0 = p0.CreateRun();
r0.SetText("DOCX表");
XWPFTable table = m_Docx.CreateTable(1, 3);//创建一行3列表
table.GetRow(0).GetCell(0).SetText("111");
table.GetRow(0).GetCell(1).SetText("222");
table.GetRow(0).GetCell(2).SetText("333");
XWPFTableRow m_Row = table.CreateRow();//创建一行
m_Row = table.CreateRow();//创建一行
m_Row.GetCell(0).SetText("211");
//合并单元格
m_Row = table.InsertNewTableRow(0);//表头插入一行
XWPFTableCell cell = m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P
CT_Tc cttc = cell.GetCTTc();
CT_TcPr ctPr = cttc.AddNewTcPr();
ctPr.gridSpan.val = "3";//合并3列
cttc.GetPList()[0].AddNewPPr().AddNewJc().val= ST_Jc.center;
cttc.GetPList()[0].AddNewR().AddNewT().Value = "abc";
XWPFTableRow td3 = table.InsertNewTableRow(table.Rows.Count - 1);//插入行
cell = td3.CreateCell();
cttc = cell.GetCTTc();
ctPr = cttc.AddNewTcPr();
ctPr.gridSpan.val = "3";
cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[0].AddNewR().AddNewT().Value = "qqq";
//表增加行,合并列
CT_Row 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.gridSpan.val = "3";
cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[0].AddNewR().AddNewT().Value = "sss";
//表未增加行,合并2列,合并2行
//1行
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.gridSpan.val = "2";
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";
cell = m_Row.CreateCell();
cell.SetText("ddd");
//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.gridSpan.val = "2";
ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行
cell = m_Row.CreateCell();
cell.SetText("kkk");
3行
//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.gridSpan.val = "2";
//ctPr.AddNewVMerge().val = ST_Merge.@continue;
//cell = m_Row.CreateCell();
//cell.SetText("hhh");
return m_Docx;
}
static void SaveToFile(MemoryStream ms, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
data = null;
}
}
上面代码所创建的表见图
转载于:https://www.cnblogs.com/sunbobohu/p/4914999.html