Npoi Creating Word

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.