Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

Writing a table without a Border to Word with Poi

$
0
0
Hello :-)

I will write a table to Word with the framework poi.
From another webside I found an example of this. But unfortunately I don't know how to remove the border of the table.

Did anyone have an idea?

Best regards,

louisa.


Code:

public static void main(String[] args) throws Exception {

                XWPFDocument document = new XWPFDocument();

                // Create a new table with 6 rows and 3 columns
                int nRows = 2;
                int nCols = 5;
                XWPFTable table = document.createTable(nRows, nCols);


                CTTblPr tablePr = table.getCTTbl().getTblPr();
                CTString styleTable = tablePr.addNewTblStyle();
                styleTable.setVal("meinStyle");

                // Get a list of the rows in the table
                List<XWPFTableRow> rows = table.getRows();
                int rowCt = 0;
                int colCt = 0;

                // ueber die Zeilen iterieren
                for (XWPFTableRow row : rows) {
                        CTTrPr rowProperties = row.getCtRow().addNewTrPr();
                        CTHeight rowHeight = rowProperties.addNewTrHeight();
                        rowHeight.setVal(BigInteger.valueOf(360));

                        // get the cells in this row
                        List<XWPFTableCell> cells = row.getTableCells();

                        // iteriere ueber die Zellen
                        for (XWPFTableCell cell : cells) {
                                CTTcPr cellPropertie = cell.getCTTc().addNewTcPr();
                                CTVerticalJc verticalCell = cellPropertie.addNewVAlign();
                                verticalCell.setVal(STVerticalJc.CENTER);

                                CTShd ctshd = cellPropertie.addNewShd();
                                ctshd.setColor("auto");
                                ctshd.setVal(STShd.CLEAR);
                                if (rowCt == 0) {
                                        // header row
                                        ctshd.setFill("A7BFDE");
                                } else if (rowCt % 2 == 0) {
                                        // even row
                                        ctshd.setFill("D3DFEE");
                                } else {
                                        // odd row
                                        ctshd.setFill("EDF2F8");
                                }

                                // get 1st paragraph in cell's paragraph list
                                XWPFParagraph para = cell.getParagraphs().get(0);

                                XWPFRun rh = para.createRun();
                                // style cell as desired

                                rh.setFontSize(5);
                                rh.setFontFamily("Courier");
                                para.setAlignment(ParagraphAlignment.CENTER);

                                if (rowCt == 0) {
                                        // header row
                                        rh.setText("header row, col " + colCt);
                                } else if (rowCt % 2 == 0) {
                                        // even row
                                        rh.setText("row " + rowCt + ", col " + colCt);
                                } else {
                                        // odd row
                                        rh.setText("row " + rowCt + ", col " + colCt);
                                }
                                colCt++;
                        } // for cell
                        colCt = 0;
                        rowCt++;
                } // for row

                // write the file

                FileOutputStream out = new FileOutputStream("D:/styledTable1.doc");
                document.write(out);
                out.close();
        }


Viewing all articles
Browse latest Browse all 120

Trending Articles