Npoi has been there for some time, the current version 2.0 Beta 2 [v2.0.5], online about Npoi operation xlsx articles more, and about docx almost no, although npoi for word is not stable, after a bit of tinkering finally realized the simple operation of the table: Create a table, create a row, Create a merge of cells, cell rows, and columns.
Environment: VS2010,NETFRAMEWORK4
Specific code:
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 table");
xwpftable table = m_docx.createtable (1, 3);//Create a row 3 list
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 ();//Create a line
M_row = table. CreateRow ();//Create a line
M_row.getcell (0). SetText ("211");
Merge cells
M_row = table. Insertnewtablerow (0);//table header insert Row
Xwpftablecell cell = M_row.createcell ();//Creates a cell and creates a ct_p when the cell is created
CT_TC CTTC = cell. GETCTTC ();
CT_TCPR CTPR = CTTC. ADDNEWTCPR ();
CtPr.gridSpan.val = "3";//Merge 3 columns
Cttc. Getplist () [0]. ADDNEWPPR (). ADDNEWJC (). val= St_jc.center;
Cttc. Getplist () [0]. ADDNEWR (). ADDNEWT (). Value = "ABC";
Xwpftablerow td3 = table. Insertnewtablerow (table. ROWS.COUNT-1);//Insert Row
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";
Table add rows, merge columns
Ct_row M_newrow = new Ct_row ();
M_row = new Xwpftablerow (m_newrow, table);
Table. AddRow (M_row); Must HAVE to!!!
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";
Table not adding rows, merging 2 columns, merging 2 rows
1 rows
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;//Merge rows
Ctpr.addnewvalign (). val = st_verticaljc.center;//Vertical 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 lines, multi-line merge similar
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 = [email protected];//merge Line
Cell = M_row.createcell ();
Cell. SetText ("KKK");
3 Rows
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 = [email protected];
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;
}
}
The table created by the above code is shown in figure
Npoi Creating Word