首先安装NPOI控件:
Install-Package NPOI
代码:
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;
using System.IO;
using System.Windows; namespace NPOIDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
MemoryStream ms = new MemoryStream();
XWPFDocument m_Docx = new XWPFDocument();
m_Docx = CreatDocxTable();
m_Docx.Write(ms);
ms.Flush();
SaveToFile(ms, "d:\\test.docx");
} private XWPFDocument CreatDocxTable()
{
XWPFDocument m_Docx = new XWPFDocument();
XWPFParagraph p0 = m_Docx.CreateParagraph();
XWPFRun r0 = p0.CreateRun();
r0.SetText("DOCX表"); XWPFTable table = m_Docx.CreateTable(, );//创建一行3列表
table.GetRow().GetCell().SetText("");
table.GetRow().GetCell().SetText("");
table.GetRow().GetCell().SetText(""); XWPFTableRow m_Row = table.CreateRow();//创建一行
m_Row = table.CreateRow();//创建一行
m_Row.GetCell().SetText(""); //合并单元格
m_Row = table.InsertNewTableRow();//表头插入一行
XWPFTableCell cell = m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P
CT_Tc cttc = cell.GetCTTc();
CT_TcPr ctPr = cttc.AddNewTcPr();
ctPr.AddNewGridspan();
ctPr.gridSpan.val = "";//合并3列
cttc.GetPList()[].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[].AddNewR().AddNewT().Value = "abc"; XWPFTableRow td3 = table.InsertNewTableRow(table.Rows.Count - );//插入行
cell = td3.CreateCell();
cttc = cell.GetCTTc();
ctPr = cttc.AddNewTcPr();
ctPr.AddNewGridspan();
ctPr.gridSpan.val = "";//合并3列
cttc.GetPList()[].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[].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.AddNewGridspan();
ctPr.gridSpan.val = "";//合并3列
cttc.GetPList()[].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[].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.AddNewGridspan();
ctPr.gridSpan.val = "";//合并3列
ctPr.AddNewVMerge().val = ST_Merge.restart;//合并行
ctPr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
cttc.GetPList()[].AddNewPPr().AddNewJc().val = ST_Jc.center;
cttc.GetPList()[].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.AddNewGridspan();
ctPr.gridSpan.val = "";//合并3列
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;
} private void SaveToFile(MemoryStream ms, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray(); fs.Write(data, , data.Length);
fs.Flush();
data = null;
}
}
}
}