Example usage for org.apache.poi.xwpf.usermodel XWPFDocument createTable

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument createTable

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFDocument createTable.

Prototype

public XWPFTable createTable(int rows, int cols) 

Source Link

Document

Create an empty table with a number of rows and cols specified

Usage

From source file:genrate_doc.java

void create_tt_word(String fname, String[][] tt) throws FileNotFoundException, IOException {
    XWPFDocument doc = new XWPFDocument();
    insert_title(doc);//from w  ww .  j  ava  2 s.  c  o m
    changeOrientation(doc, "landscape");

    XWPFTable table = doc.createTable(7, 10);

    table.setCellMargins(200, 200, 200, 200);

    XWPFTableRow row0 = table.getRow(0);
    XWPFTableCell r0_cell0 = row0.getCell(0);
    r0_cell0.setText("DAY/TIME");
    XWPFTableRow row1 = table.getRow(1);
    XWPFTableCell r1_cell0 = row1.getCell(0);
    r1_cell0.setText("MON");
    XWPFTableRow row2 = table.getRow(2);
    XWPFTableCell r2_cell0 = row2.getCell(0);
    r2_cell0.setText("TUE");
    XWPFTableRow row3 = table.getRow(3);
    XWPFTableCell r3_cell0 = row3.getCell(0);
    r3_cell0.setText("WED");
    XWPFTableRow row4 = table.getRow(4);
    XWPFTableCell r4_cell0 = row4.getCell(0);
    r4_cell0.setText("THR");
    XWPFTableRow row5 = table.getRow(5);
    XWPFTableCell r5_cell0 = row5.getCell(0);
    r5_cell0.setText("FRI");
    XWPFTableRow row6 = table.getRow(6);
    XWPFTableCell r6_cell0 = row6.getCell(0);
    r6_cell0.setText("SAT");

    XWPFTableCell r0_cell1 = row0.getCell(1);
    r0_cell1.setText("08:40-09:40");
    XWPFTableCell r0_cell2 = row0.getCell(2);
    r0_cell2.setText("09:40-10:40");
    XWPFTableCell r0_cell3 = row0.getCell(3);
    r0_cell3.setText("10:40-11:00");
    XWPFTableCell r0_cell4 = row0.getCell(4);
    r0_cell4.setText("11:00-12:00");
    XWPFTableCell r0_cell5 = row0.getCell(5);
    r0_cell5.setText("12:00-01:00");
    XWPFTableCell r0_cell6 = row0.getCell(6);
    r0_cell6.setText("01:00-01:40");
    XWPFTableCell r0_cell7 = row0.getCell(7);
    r0_cell7.setText("01:40-02:30");
    XWPFTableCell r0_cell8 = row0.getCell(8);
    r0_cell8.setText("02:30-03:20");
    XWPFTableCell r0_cell9 = row0.getCell(9);
    r0_cell9.setText("03:20-04:10");

    // writing the time table
    /* XWPFTableCell r1_cell1 = row1.getCell(1); 
    r1_cell1.setText(tt[0][0]);
    XWPFTableCell r1_cell2 = row1.getCell(2); 
    r1_cell2.setText(tt[0][1]);
    XWPFTableCell r1_cell3 = row1.getCell(3); 
    r1_cell3.setText(tt[0][2]);
            
    XWPFTableCell r1_cell4 = row1.getCell(4); 
    r1_cell4.setText("B");
            
    XWPFTableCell r1_cell5 = row1.getCell(5); 
    r1_cell5.setText(tt[0][3]);
    XWPFTableCell r1_cell6 = row1.getCell(6); 
    r1_cell6.setText(tt[0][4]);*/
    XWPFTableRow row;
    XWPFTableCell cell;
    for (int i = 0; i < 6; i++) {
        row = table.getRow(i + 1);
        for (int j = 0; j < 9; j++) {
            cell = row.getCell(j + 1);
            cell.setText(tt[i][j]);
        }
    }

    FileOutputStream output = new FileOutputStream("timetable\\" + fname + ".docx");
    doc.write(output);
    output.close();
}

From source file:genrate_doc.java

void teach_doc(String fname, String teach[][]) throws FileNotFoundException, IOException {
    File fp = new File("teacher files\\" + fname + ".docx");
    if (fp.exists()) {
        return;/*from   w ww . j a va 2 s.  c om*/
    }
    XWPFDocument doc = new XWPFDocument();
    insert_title(doc);
    changeOrientation(doc, "landscape");
    XWPFTable table = doc.createTable(7, 10);

    table.setCellMargins(200, 200, 200, 200);

    XWPFTableRow row0 = table.getRow(0);
    XWPFTableCell r0_cell0 = row0.getCell(0);
    r0_cell0.setText("DAY/TIME");
    XWPFTableRow row1 = table.getRow(1);
    XWPFTableCell r1_cell0 = row1.getCell(0);
    r1_cell0.setText("MON");
    XWPFTableRow row2 = table.getRow(2);
    XWPFTableCell r2_cell0 = row2.getCell(0);
    r2_cell0.setText("TUE");
    XWPFTableRow row3 = table.getRow(3);
    XWPFTableCell r3_cell0 = row3.getCell(0);
    r3_cell0.setText("WED");
    XWPFTableRow row4 = table.getRow(4);
    XWPFTableCell r4_cell0 = row4.getCell(0);
    r4_cell0.setText("THR");
    XWPFTableRow row5 = table.getRow(5);
    XWPFTableCell r5_cell0 = row5.getCell(0);
    r5_cell0.setText("FRI");
    XWPFTableRow row6 = table.getRow(6);
    XWPFTableCell r6_cell0 = row6.getCell(0);
    r6_cell0.setText("SAT");

    XWPFTableCell r0_cell1 = row0.getCell(1);
    r0_cell1.setText("08:40-09:40");
    XWPFTableCell r0_cell2 = row0.getCell(2);
    r0_cell2.setText("09:40-10:40");
    XWPFTableCell r0_cell3 = row0.getCell(3);
    r0_cell3.setText("10:40-11:00");
    XWPFTableCell r0_cell4 = row0.getCell(4);
    r0_cell4.setText("11:00-12:00");
    XWPFTableCell r0_cell5 = row0.getCell(5);
    r0_cell5.setText("12:00-01:00");
    XWPFTableCell r0_cell6 = row0.getCell(6);
    r0_cell6.setText("01:00-01:40");
    XWPFTableCell r0_cell7 = row0.getCell(7);
    r0_cell7.setText("01:40-02:30");
    XWPFTableCell r0_cell8 = row0.getCell(8);
    r0_cell8.setText("02:30-03:20");
    XWPFTableCell r0_cell9 = row0.getCell(9);
    r0_cell9.setText("03:20-04:10");

    // writing the time table
    /* XWPFTableCell r1_cell1 = row1.getCell(1); 
    r1_cell1.setText(tt[0][0]);
    XWPFTableCell r1_cell2 = row1.getCell(2); 
    r1_cell2.setText(tt[0][1]);
    XWPFTableCell r1_cell3 = row1.getCell(3); 
    r1_cell3.setText(tt[0][2]);
            
    XWPFTableCell r1_cell4 = row1.getCell(4); 
    r1_cell4.setText("B");
            
    XWPFTableCell r1_cell5 = row1.getCell(5); 
    r1_cell5.setText(tt[0][3]);
    XWPFTableCell r1_cell6 = row1.getCell(6); 
    r1_cell6.setText(tt[0][4]);*/
    XWPFTableRow row;
    XWPFTableCell cell;
    for (int i = 0; i < 6; i++) {
        row = table.getRow(i + 1);
        for (int j = 0; j < 9; j++) {
            cell = row.getCell(j + 1);
            cell.setText(teach[i][j]);
        }
    }

    FileOutputStream output = new FileOutputStream("teacher files\\" + fname + ".docx");
    doc.write(output);
    output.close();
}

From source file:br.com.techne.gluonsoft.eowexport.builder.WordBuilder.java

License:Apache License

/**
 * mtodo cria bytes de documento Excel/*from  ww w  . j  a va2s .  c  om*/
 * @param titles
 * @param columnIndex
 * @param dataRows
 * @param locale
 * @return
 * @throws Exception
 */
public static byte[] createStyledTable(String[] titles, String[] columnIndex,
        List<HashMap<String, Object>> dataRows, Locale locale) throws Exception {

    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();
    byte[] outBytes;
    try {
        int nRows = dataRows.size() + 1;
        int nCols = columnIndex.length == 0 ? dataRows.get(0).keySet().size() : columnIndex.length;

        XWPFTable table = doc.createTable(nRows, nCols);

        // Set the table style. If the style is not defined, the table style
        // will become "Normal".
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTString styleStr = tblPr.addNewTblStyle();
        styleStr.setVal("StyledTable");

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

        int rowCt = 1;

        addTitle(rows, titles);

        ValueCellUtil vcutil = new ValueCellUtil(locale);

        for (HashMap<String, Object> dataRow : dataRows) {
            addRow(rows, dataRow, rowCt, vcutil, columnIndex);
            rowCt++;
        }

        // write for return byte[]
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            doc.write(out);
            outBytes = out.toByteArray();
        } finally {
            out.close();
        }
    } finally {
        doc.close();
    }

    return outBytes;
}

From source file:com.deepoove.poi.tl.SimpleTable.java

License:Apache License

public static void createSimpleTable() throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFTable table = doc.createTable(3, 3);

    table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

    // table cells have a list of paragraphs; there is an initial
    // paragraph created when the cell is created. If you create a
    // paragraph in the document to put in the cell, it will also
    // appear in the document following the table, which is probably
    // not the desired result.
    XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);

    XWPFRun r1 = p1.createRun();//from  w ww  .  ja v  a  2 s  .  c om
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setItalic(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    table.getRow(2).getCell(2).setText("only text");

    FileOutputStream out = new FileOutputStream("simpleTable.docx");
    doc.write(out);
    out.close();
}

From source file:com.deepoove.poi.tl.SimpleTable.java

License:Apache License

/**
 * Create a table with some row and column styling. I "manually" add the
 * style name to the table, but don't check to see if the style actually
 * exists in the document. Since I'm creating it from scratch, it obviously
 * won't exist. When opened in MS Word, the table style becomes "Normal".
 * I manually set alternating row colors. This could be done using Themes,
 * but that's left as an exercise for the reader. The cells in the last
 * column of the table have 10pt. "Courier" font.
 * I make no claims that this is the "right" way to do it, but it worked
 * for me. Given the scarcity of XWPF examples, I thought this may prove
 * instructive and give you ideas for your own solutions.
        /*  ww w .j a  v a 2 s  .  c o  m*/
 * @throws Exception
 */
public static void createStyledTable() throws Exception {
    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();
    // -- OR --
    // open an existing empty document with styles already defined
    //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));

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

    // Set the table style. If the style is not defined, the table style
    // will become "Normal".
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CTString styleStr = tblPr.addNewTblStyle();
    styleStr.setVal("StyledTable");

    // Get a list of the rows in the table
    List<XWPFTableRow> rows = table.getRows();
    int rowCt = 0;
    int colCt = 0;
    for (XWPFTableRow row : rows) {
        // get table row properties (trPr)
        CTTrPr trPr = row.getCtRow().addNewTrPr();
        // set row height; units = twentieth of a point, 360 = 0.25"
        CTHeight ht = trPr.addNewTrHeight();
        ht.setVal(BigInteger.valueOf(360));

        // get the cells in this row
        List<XWPFTableCell> cells = row.getTableCells();
        // add content to each cell
        for (XWPFTableCell cell : cells) {
            // get a table cell properties element (tcPr)
            CTTcPr tcpr = cell.getCTTc().addNewTcPr();
            // set vertical alignment to "center"
            CTVerticalJc va = tcpr.addNewVAlign();
            va.setVal(STVerticalJc.CENTER);

            // create cell color element
            CTShd ctshd = tcpr.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);
            // create a run to contain the content
            XWPFRun rh = para.createRun();
            // style cell as desired
            if (colCt == nCols - 1) {
                // last column is 10pt Courier
                rh.setFontSize(10);
                rh.setFontFamily("Courier");
            }
            if (rowCt == 0) {
                // header row
                rh.setText("header row, col " + colCt);
                rh.setBold(true);
                para.setAlignment(ParagraphAlignment.CENTER);
            } else if (rowCt % 2 == 0) {
                // even row
                rh.setText("row " + rowCt + ", col " + colCt);
                para.setAlignment(ParagraphAlignment.LEFT);
            } else {
                // odd row
                rh.setText("row " + rowCt + ", col " + colCt);
                para.setAlignment(ParagraphAlignment.LEFT);
            }
            colCt++;
        } // for cell
        colCt = 0;
        rowCt++;
    } // for row

    // write the file
    FileOutputStream out = new FileOutputStream("styledTable.docx");
    doc.write(out);
    out.close();
}

From source file:com.glodon.tika.SimpleTable.java

License:Apache License

public static void createSimpleTable() throws Exception {
    XWPFDocument doc = new XWPFDocument();

    try {//from  w  ww .  ja v a 2s  .c  o m
        XWPFTable table = doc.createTable(3, 3);

        table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

        // table cells have a list of paragraphs; there is an initial
        // paragraph created when the cell is created. If you create a
        // paragraph in the document to put in the cell, it will also
        // appear in the document following the table, which is probably
        // not the desired result.
        XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);

        XWPFRun r1 = p1.createRun();
        r1.setBold(true);
        r1.setText("The quick brown fox");
        r1.setItalic(true);
        r1.setFontFamily("Courier");
        r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
        r1.setTextPosition(100);

        table.getRow(2).getCell(2).setText("only text");

        OutputStream out = new FileOutputStream("simpleTable.docx");
        try {
            doc.write(out);
        } finally {
            out.close();
        }
    } finally {
        doc.close();
    }
}

From source file:com.glodon.tika.SimpleTable.java

License:Apache License

/**
 * Create a table with some row and column styling. I "manually" add the
 * style name to the table, but don't check to see if the style actually
 * exists in the document. Since I'm creating it from scratch, it obviously
 * won't exist. When opened in MS Word, the table style becomes "Normal".
 * I manually set alternating row colors. This could be done using Themes,
 * but that's left as an exercise for the reader. The cells in the last
 * column of the table have 10pt. "Courier" font.
 * I make no claims that this is the "right" way to do it, but it worked
 * for me. Given the scarcity of XWPF examples, I thought this may prove
 * instructive and give you ideas for your own solutions.
        /*from  www .  j  a v a2  s. c o m*/
 * @throws Exception
 */
public static void createStyledTable() throws Exception {
    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();

    try {
        // -- OR --
        // open an existing empty document with styles already defined
        //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));

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

        // Set the table style. If the style is not defined, the table style
        // will become "Normal".
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTString styleStr = tblPr.addNewTblStyle();
        styleStr.setVal("StyledTable");

        // Get a list of the rows in the table
        List<XWPFTableRow> rows = table.getRows();
        int rowCt = 0;
        int colCt = 0;
        for (XWPFTableRow row : rows) {
            // get table row properties (trPr)
            CTTrPr trPr = row.getCtRow().addNewTrPr();
            // set row height; units = twentieth of a point, 360 = 0.25"
            CTHeight ht = trPr.addNewTrHeight();
            ht.setVal(BigInteger.valueOf(360));

            // get the cells in this row
            List<XWPFTableCell> cells = row.getTableCells();
            // add content to each cell
            for (XWPFTableCell cell : cells) {
                // get a table cell properties element (tcPr)
                CTTcPr tcpr = cell.getCTTc().addNewTcPr();
                // set vertical alignment to "center"
                CTVerticalJc va = tcpr.addNewVAlign();
                va.setVal(STVerticalJc.CENTER);

                // create cell color element
                CTShd ctshd = tcpr.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);
                // create a run to contain the content
                XWPFRun rh = para.createRun();
                // style cell as desired
                if (colCt == nCols - 1) {
                    // last column is 10pt Courier
                    rh.setFontSize(10);
                    rh.setFontFamily("Courier");
                }
                if (rowCt == 0) {
                    // header row
                    rh.setText("header row, col " + colCt);
                    rh.setBold(true);
                    para.setAlignment(ParagraphAlignment.CENTER);
                } else {
                    // other rows
                    rh.setText("row " + rowCt + ", col " + colCt);
                    para.setAlignment(ParagraphAlignment.LEFT);
                }
                colCt++;
            } // for cell
            colCt = 0;
            rowCt++;
        } // for row

        // write the file
        OutputStream out = new FileOutputStream("styledTable.docx");
        try {
            doc.write(out);
        } finally {
            out.close();
        }
    } finally {
        doc.close();
    }
}

From source file:com.hotaviano.tableexporter.docx.DOCXExporter.java

License:Open Source License

@Override
public byte[] export(String htmlTable) throws DocumentException {
    XWPFDocument doc = new XWPFDocument();

    Document document = createDocument(htmlTable);

    int cols = getHeaderSize(document);
    int rows = getHeaderSize(document) + 1;

    XWPFTable table = doc.createTable(rows, cols);

    createHeaderTable(table, document);/*  w w  w  .  j av a 2s .com*/
    createBodyTable(table, document);

    CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
    width.setType(STTblWidth.DXA);
    width.setW(BigInteger.valueOf(9072));

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        doc.write(out);
        out.close();
    } catch (IOException ex) {
        throw new DocumentException(ex);
    }

    return out.toByteArray();
}

From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

public static XWPFTable createTableAndAddReleasesTableHeaders(XWPFDocument document, String[] headers) {
    if (headers.length < 4) {
        throw new IllegalArgumentException("Too few table headers found. Need 4 table headers.");
    }//from  w w  w  . j  av  a  2  s . c  o m
    XWPFTable table = document.createTable(1, 4);
    styleTable(table);
    XWPFTableRow headerRow = table.getRow(0);

    for (int headerCount = 0; headerCount < headers.length; headerCount++) {
        XWPFParagraph paragraph = headerRow.getCell(headerCount).getParagraphs().get(0);
        styleTableHeaderParagraph(paragraph);

        XWPFRun run = paragraph.createRun();
        addFormattedText(run, headers[headerCount], 12, true);

        paragraph.setWordWrap(true);
    }
    return table;
}

From source file:com.simplexwork.mysql.tools.utils.DocumentUtils.java

License:Apache License

/**
 * ??word//from  w ww .j a  v  a  2s .  c  o m
 * 
 * @param tablesMap
 * @throws Exception
 */
public static void productDatabaseDoc(Map<String, TableInfo> tablesMap) throws Exception {
    XWPFDocument xwpfDocument = new XWPFDocument();

    for (Entry<String, TableInfo> entry : tablesMap.entrySet()) {
        TableInfo tableInfo = entry.getValue();

        XWPFParagraph xwpfParagraph = xwpfDocument.createParagraph();
        XWPFRun xwpfRun = xwpfParagraph.createRun();
        xwpfRun.setText(tableInfo.getTableName() + "(" + tableInfo.getTableComment() + ")");
        xwpfRun.setFontSize(18);
        xwpfRun.setTextPosition(10);

        XWPFTable xwpfTable = xwpfDocument.createTable(tableInfo.getColumns().size() + 1, 6);

        int i = 0;

        xwpfTable.getRow(i).getCell(0).setText("??");
        xwpfTable.getRow(i).getCell(1).setText("");
        xwpfTable.getRow(i).getCell(2).setText("");
        xwpfTable.getRow(i).getCell(3).setText("");
        xwpfTable.getRow(i).getCell(4).setText("");
        xwpfTable.getRow(i).getCell(5).setText("?");

        for (Column column : tableInfo.getColumns()) {
            int j = 0;
            i++;
            xwpfTable.getRow(i).getCell(j++).setText(column.getName());
            xwpfTable.getRow(i).getCell(j++).setText(column.getType());
            xwpfTable.getRow(i).getCell(j++).setText(column.isKey() ? "" : "");
            xwpfTable.getRow(i).getCell(j++).setText(column.isNullable() ? "?" : "");
            xwpfTable.getRow(i).getCell(j++).setText(column.getComment());
            xwpfTable.getRow(i).getCell(j++).setText(column.getExtra());
        }

    }

    File file = new File(generatedPath);
    if (!file.exists()) {
        file.mkdirs();
    }

    FileOutputStream fos = new FileOutputStream(generatedPath + "database.docx");
    xwpfDocument.write(fos);
    fos.close();

}