Record the table export Word with POI

Source: Internet
Author: User

It is also the code http://53873039oycg.iteye.com/blog/2152009 found on the internet, but the horizontal merge cell did not succeed. It's only a stupid way to build a lot of table.

public void filltable (xwpftable table) {
for (int rowIndex = 0; RowIndex < table.getnumberofrows (); rowindex++) {
Xwpftablerow row = Table.getrow (RowIndex);
Row.setheight (380);
for (int colindex = 0; Colindex < Row.gettablecells (). Size (); colindex++) {
Xwpftablecell cell = Row.getcell (Colindex);
if (rowIndex% 2 = = 0) {
Setcelltext (cell, "cell" + rowIndex + colindex + "", "d4dbed", 1000);
} else {
Setcelltext (cell, "cell" + rowIndex + colindex + "", "AEDE72", 1000);
}
}
}
}

public void Setcelltext (Xwpftablecell cell, string text, string bgcolor, int width) {
CTTc CTTc = CELL.GETCTTC ();
CTTCPR CELLPR = CTTC.ADDNEWTCPR ();
CELLPR.ADDNEWTCW (). SETW (biginteger.valueof (width));
Cell.setcolor (bgcolor);
CTTCPR CTPR = CTTC.ADDNEWTCPR ();
Ctshd ctshd = Ctpr.addnewshd ();
Ctshd.setfill (bgcolor);
Ctpr.addnewvalign (). Setval (Stverticaljc.center);
Cttc.getplist (). Get (0). ADDNEWPPR (). ADDNEWJC (). Setval (Stjc.center);
Cell.settext (text);
}

/**
* @Description: Merge across columns
*/
public void mergecellshorizontal (xwpftable table, int row, int fromcell, int tocell) {
for (int cellindex = FromCell; cellindex <= ToCell; cellindex++) {
Xwpftablecell cell = Table.getrow (row). Getcell (CellIndex);
if (CellIndex = = FromCell) {
The first merged cell is set with RESTART merge value
CELL.GETCTTC (). ADDNEWTCPR (). Addnewhmerge (). Setval (Stmerge.restart);
} else {
Cells which join (merge) The first one, is set with CONTINUE
CELL.GETCTTC (). ADDNEWTCPR (). Addnewhmerge (). Setval (Stmerge.continue);
}
}
}

/**
* @Description: Merge across rows
* @see http://stackoverflow.com/questions/24907541/row-span-with-xwpftable
*/
public void mergecellsvertically (xwpftable table, int col, int fromrow, int torow) {
for (int rowIndex = Fromrow; RowIndex <= torow; rowindex++) {
Xwpftablecell cell = Table.getrow (RowIndex). Getcell (COL);
if (RowIndex = = Fromrow) {
The first merged cell is set with RESTART merge value
CELL.GETCTTC (). ADDNEWTCPR (). Addnewvmerge (). Setval (Stmerge.restart);
} else {
Cells which join (merge) The first one, is set with CONTINUE
CELL.GETCTTC (). ADDNEWTCPR (). Addnewvmerge (). Setval (Stmerge.continue);
}
}
}

public void Settablewidth (xwpftable table,string width) {
Cttbl ttbl = Table.getcttbl ();
CTTBLPR tblPr = TTBL.GETTBLPR () = = null? TTBL.ADDNEWTBLPR (): Ttbl.gettblpr ();
Cttblwidth tblwidth = TBLPR.ISSETTBLW ()? TBLPR.GETTBLW (): TBLPR.ADDNEWTBLW ();
CTJC CTJC=TBLPR.ADDNEWJC ();
Ctjc.setval (STJc.Enum.forString ("center"));
TBLWIDTH.SETW (new BigInteger (width));
Tblwidth.settype (STTBLWIDTH.DXA);
}
public void FillTable2 (xwpftable table) {
for (int rowIndex = 0; RowIndex < table.getnumberofrows (); rowindex++) {
Xwpftablerow row = Table.getrow (RowIndex);
Row.setheight (380);
for (int colindex = 0; Colindex < Row.gettablecells (). Size (); colindex++) {
Xwpftablecell cell = Row.getcell (Colindex);
if (rowIndex% 2 = = 0) {
Setcelltext (cell, "cell" + rowIndex + colindex + "", "d4dbed", 1800);
} else {
Setcelltext (cell, "cell" + rowIndex + colindex + "", "AEDE72", 1800);
}
}
}
}
public void CreateTable (xwpfdocument doc,string str) {
xwpftable table = null;
Table = doc.createtable (1, 1);
Settablewidth (table, "9000");
Xwpftablerow row = null;
row = Table.getrow (0);
Row.setheight (380);
Xwpftablecell cell = null;
Cell = Row.getcell (0);
Setcelltext (cell, str, "CCCCCC", 9000);

}

@Override
public void Exportword (Servletoutputstream outputstream) {
Document corresponds to a Word application file
Xwpfdocument doc = new xwpfdocument ();

/*xwpftable table1 = doc.createtable (6, 8);
Settablewidth (table1, "8000");
Filltable (table1);
Mergecellsvertically (table1, 1, 1,4);
Mergecellsvertically (table1, 4, 2, 4);
Mergecellshorizontal (table1, 0, 3, 5);
Mergecellshorizontal (table1, 2, 2, 3);
Mergecellshorizontal (table1, 2, 6, 7); */

CreateTable (Doc, "Renyuanxinxi");

xwpftable table1 = doc.createtable (1, 6);
Xwpftablerow row0 = table1.getrow (0);
Row0.setheight (380);
Xwpftablecell cell = null;
Cell = Row0.getcell (0);
Setcelltext (cell, "Personnel number", "FFFFFF", 1500);
Cell = Row0.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 1500);
Cell = Row0.getcell (2);
Setcelltext (Cell, "name", "FFFFFF", 1500);
Cell = Row0.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 1500);
Cell = Row0.getcell (4);
Setcelltext (Cell, "Social Security Number", "FFFFFF", 1500);
Cell = Row0.getcell (5);
Setcelltext (Cell, "text", "FFFFFF", 1500);


CreateTable (Doc, "Jiancejihua");

xwpftable table2 = doc.createtable (2, 6);
Xwpftablerow row =null;
Row.setheight (380);
row = Table2.getrow (0);
Cell = Row.getcell (0);
Setcelltext (cell, "Place room", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 1500);
Cell = Row.getcell (2);
Setcelltext (Cell, "department", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 1500);
Cell = Row.getcell (4);
Setcelltext (cell, "unit", "FFFFFF", 1500);
Cell = Row.getcell (5);
Setcelltext (Cell, "text", "FFFFFF", 1500);
row = Table2.getrow (1);
Cell = Row.getcell (0);
Setcelltext (cell, "production unit", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 1500);
Cell = Row.getcell (2);
Setcelltext (cell, "interface person", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 1500);
Cell = Row.getcell (4);
Setcelltext (cell, "Monitoring plan Status", "FFFFFF", 1500);
Cell = Row.getcell (5);
Setcelltext (Cell, "text", "FFFFFF", 1500);

CreateTable (Doc, "exception description");
Xwpftable table3 = doc.createtable (4, 4);
row = Table3.getrow (0);
Row.setheight (380);
Cell = Row.getcell (0);
Setcelltext (cell, "type of monitoring", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Cell = Row.getcell (2);
Setcelltext (Cell, "Exception type", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 3000);
row = Table2.getrow (1);
Cell = Row.getcell (0);
Setcelltext (cell, "unit", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Cell = Row.getcell (2);
Setcelltext (Cell, "Time", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 3000);
row = Table2.getrow (2);
Cell = Row.getcell (0);
Setcelltext (Cell, "outlier", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Cell = Row.getcell (2);
Setcelltext (cell, "Outlier unit", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Row = Table2.getrow (3);
Cell = Row.getcell (0);
Setcelltext (cell, "initiator", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Cell = Row.getcell (2);
Setcelltext (Cell, "Initiation Time", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 3000);

CreateTable (Doc, "Radiation Protection Group Survey (500 characters)");
Xwpftable table4 = doc.createtable (1, 2);
row = Table4.getrow (0);
Row.setheight (900);
Cell = Row.getcell (0);
Setcelltext (cell, "survey description", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 7500);
Xwpftable table5 = doc.createtable (1, 4);
row = Table5.getrow (0);
Row.setheight (380);
Cell = Row.getcell (0);
Setcelltext (Cell, "investigator", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Cell = Row.getcell (2);
Setcelltext (cell, "date", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 3000);

CreateTable (doc, "Personal dose Group survey (500 characters)");
Xwpftable table6 = doc.createtable (1, 2);
row = Table6.getrow (0);
Row.setheight (900);
Cell = Row.getcell (0);
Setcelltext (cell, "survey description", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 7500);
Xwpftable Table7 = doc.createtable (1, 6);
row = Table7.getrow (0);
Row.setheight (380);
Cell = Row.getcell (0);
Setcelltext (cell, "nominal dosage determination Mode", "FFFFFF", 2000);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 2000);
Cell = Row.getcell (2);
Setcelltext (cell, "nominal dose/Final value", "FFFFFF", 2000);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 2000);
Cell = Row.getcell (4);
Setcelltext (cell, "nominal dose/final value unit", "FFFFFF", 2000);
Cell = Row.getcell (5);
Setcelltext (Cell, "text", "FFFFFF", 2000);
Xwpftable Table8 = doc.createtable (1, 4);
row = Table8.getrow (0);
Cell = Row.getcell (0);
Setcelltext (Cell, "investigator", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Cell = Row.getcell (2);
Setcelltext (cell, "date", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 3000);

CreateTable (Doc, "Conclusion and processing opinion (200 characters)");
Xwpftable table9 = doc.createtable (2, 2);
row = Table9.getrow (0);
Cell = Row.getcell (0);
Setcelltext (cell, "tracking", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "", "FFFFFF", 7500);
row = Table9.getrow (1);
Cell = Row.getcell (0);
Setcelltext (cell, "Conclusion", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "", "FFFFFF", 7500);
Xwpftable Table10 = doc.createtable (1, 4);
row = Table10.getrow (0);
Cell = Row.getcell (0);
Setcelltext (Cell, "auditor", "FFFFFF", 1500);
Cell = Row.getcell (1);
Setcelltext (Cell, "text", "FFFFFF", 3000);
Cell = Row.getcell (2);
Setcelltext (cell, "date", "FFFFFF", 1500);
Cell = Row.getcell (3);
Setcelltext (Cell, "text", "FFFFFF", 3000);

Xwpftable table11 = doc.createtable (1, 1);
Settablewidth (TABLE11, "9000");
row = Table11.getrow (0);
Row.setheight (380);
Cell = Row.getcell (0);
Setcelltext (Cell, "Submission of Radiation protection Group Survey submitted personal dose Group survey submitted for audit Audit Export Print Survey urine sample label", "FFFFFF", 9000);

try {
Doc.write (OutputStream);
Outputstream.flush ();
Outputstream.close ();
} catch (IOException e) {
E.printstacktrace ();
}

Add a table
/*xwpftable tab = docx.createtable (2,3);
Tab.setwidth (10000);
Add a row
Xwpftablerow row1 = Tab.createrow ();
Row1.setheight (3500);
Add columns
Xwpftablecell cell1 =null;
Cell1 = Row1.createcell ();
Cell1.settext ("name");
String hql= "Select t from tpdmisexceptionentity t where t.userid= ' 511021196307179130 '";
list<tpdmisexceptionentity> tpdmisexceptionentitylist = commondao.findbyquerystring (HQL);
Cell1 = Row1.createcell ();
Cell1.settext (Tpdmisexceptionentitylist.get (0). Getuname ());

Cell1 = Row1.createcell ();
Xwpfparagraph PIO =cell1.addparagraph ();
Xwpfrun RIO = Pio.createrun ();
Rio.setfontfamily ("The song Body");
Rio.setfontsize (8);
Rio.setbold (TRUE);
Rio.settext (Tpdmisexceptionentitylist.get (0). Getuname ());

Xwpfparagraph P1 = Docx.createparagraph ();
Xwpfrun r1 = P1.createrun ();
R1.settext ("Hello World"); */


/*try {
Doc.write (OutputStream);
Outputstream.flush ();
Outputstream.close ();
} catch (IOException e) {
E.printstacktrace ();
}*/

/*//Create a table with 5 rows and 5 columns
xwpftable table = doc.createtable (5, 5);
The 5 rows added here that were originally initialized are not available when obtained through the Gettablecells () method, but can be added via row.
Table.addnewcol (); Add a column to the table and turn it into 6 columns
Table.createrow (); Add a row to the table and turn it into 6 rows
list<xwpftablerow> rows = Table.getrows ();
Table Properties
CTTBLPR Tablepr = Table.getcttbl (). ADDNEWTBLPR ();
Table width
Cttblwidth width = TABLEPR.ADDNEWTBLW ();
WIDTH.SETW (biginteger.valueof (8000));
Xwpftablerow Row;
List<xwpftablecell> cells;
Xwpftablecell cell;
int rowsize = Rows.size ();
int cellsize;
for (int i = 0; i < rowsize; i++) {
row = Rows.get (i);
New cell
Row.addnewtablecell ();
Set the height of a row
Row.setheight (500);
Row properties
CTTRPR ROWPR = Row.getctrow (). ADDNEWTRPR ();
This is a way to get the new cell.
list<cttc> list = Row.getctrow (). Gettclist ();
Cells = Row.gettablecells ();
Cellsize = Cells.size ();
for (int j = 0; J < Cellsize; J + +) {
Cell = Cells.get (j);
if ((i + j)% 2 = = 0) {
Set the color of a cell
Cell.setcolor ("ff0000"); Red
} else {
Cell.setcolor ("0000ff"); Blue
}
Cell Properties
CTTCPR CELLPR = CELL.GETCTTC (). ADDNEWTCPR ();
Cellpr.addnewvalign (). Setval (Stverticaljc.center);
if (j = = 3) {
Set width
CELLPR.ADDNEWTCW (). SETW (biginteger.valueof (3000));
}
Cell.settext (i + "," + j);
}
}*/

Record the table export Word with POI

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.