Search in sources :

Example 1 with XWPFTableRow

use of org.apache.poi.xwpf.usermodel.XWPFTableRow in project poi by apache.

the class HeaderFooterTable method main.

public static void main(String[] args) throws IOException {
    XWPFDocument doc = new XWPFDocument();
    // Create a header with a 1 row, 3 column table
    // changes made for issue 57366 allow a new header or footer
    // to be created empty. This is a change. You will have to add
    // either a paragraph or a table to the header or footer for
    // the document to be considered valid.
    XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
    XWPFTable tbl = hdr.createTable(1, 3);
    // Set the padding around text in the cells to 1/10th of an inch
    int pad = (int) (.1 * 1440);
    tbl.setCellMargins(pad, pad, pad, pad);
    // Set table width to 6.5 inches in 1440ths of a point
    tbl.setWidth((int) (6.5 * 1440));
    // Can not yet set table or cell width properly, tables default to
    // autofit layout, and this requires fixed layout
    CTTbl ctTbl = tbl.getCTTbl();
    CTTblPr ctTblPr = ctTbl.addNewTblPr();
    CTTblLayoutType layoutType = ctTblPr.addNewTblLayout();
    layoutType.setType(STTblLayoutType.FIXED);
    // Now set up a grid for the table, cells will fit into the grid
    // Each cell width is 3120 in 1440ths of an inch, or 1/3rd of 6.5"
    BigInteger w = new BigInteger("3120");
    CTTblGrid grid = ctTbl.addNewTblGrid();
    for (int i = 0; i < 3; i++) {
        CTTblGridCol gridCol = grid.addNewGridCol();
        gridCol.setW(w);
    }
    // Add paragraphs to the cells
    XWPFTableRow row = tbl.getRow(0);
    XWPFTableCell cell = row.getCell(0);
    XWPFParagraph p = cell.getParagraphArray(0);
    XWPFRun r = p.createRun();
    r.setText("header left cell");
    cell = row.getCell(1);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header center cell");
    cell = row.getCell(2);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header right cell");
    // Create a footer with a Paragraph
    XWPFFooter ftr = doc.createFooter(HeaderFooterType.DEFAULT);
    p = ftr.createParagraph();
    r = p.createRun();
    r.setText("footer text");
    OutputStream os = new FileOutputStream(new File("headertable.docx"));
    doc.write(os);
    doc.close();
}
Also used : CTTblGridCol(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) XWPFFooter(org.apache.poi.xwpf.usermodel.XWPFFooter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTTblLayoutType(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CTTblGrid(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) File(java.io.File)

Example 2 with XWPFTableRow

use of org.apache.poi.xwpf.usermodel.XWPFTableRow in project tika by apache.

the class XWPFWordExtractorDecorator method extractTable.

private void extractTable(XWPFTable table, XWPFListManager listManager, XHTMLContentHandler xhtml) throws SAXException, XmlException, IOException {
    xhtml.startElement("table");
    xhtml.startElement("tbody");
    for (XWPFTableRow row : table.getRows()) {
        xhtml.startElement("tr");
        for (ICell cell : row.getTableICells()) {
            xhtml.startElement("td");
            if (cell instanceof XWPFTableCell) {
                extractIBodyText((XWPFTableCell) cell, listManager, xhtml);
            } else if (cell instanceof XWPFSDTCell) {
                xhtml.characters(((XWPFSDTCell) cell).getContent().getText());
            }
            xhtml.endElement("td");
        }
        xhtml.endElement("tr");
    }
    xhtml.endElement("tbody");
    xhtml.endElement("table");
}
Also used : XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFSDTCell(org.apache.poi.xwpf.usermodel.XWPFSDTCell) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) ICell(org.apache.poi.xwpf.usermodel.ICell)

Example 3 with XWPFTableRow

use of org.apache.poi.xwpf.usermodel.XWPFTableRow in project Gargoyle by callakrsos.

the class MSWord method addToTable.

/**
	 * 테이블을 추가한다.
	 *
	 * List의 로우는 각 행을 의미. TreeSet은 테이블 컬럼Index 데이터를 의미.
	 *
	 * @param list
	 */
public XWPFTable addToTable(List<List<String>> list, ITableCustomProperty property) /*
																						* ,
																						* ThFunction
																						* <
																						* ,
																						* U
																						* ,
																						* V
																						* ,
																						* R
																						* >
																						*/
{
    if (list == null || list.isEmpty()) {
        return null;
    }
    // -- OR --
    // open an existing empty document with styles already defined
    // XWPFDocument doc = new XWPFDocument(new
    // FileInputStream("base_document.docx"));
    int rowSize = list.size();
    int colSize = list.get(0).size();
    XWPFTable table = doc.createTable(rowSize, colSize);
    // 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");
    }
    /* Table Width조절을 위한 작업 */
    {
        CTTbl ctTbl = table.getCTTbl();
        CTTblPr tblPr = ctTbl.getTblPr();
        CTTblWidth tblW = tblPr.getTblW();
        // 화면에 WIDTH를 딱 맞춤.
        tblW.setW(BigInteger.valueOf(5000));
        tblW.setType(STTblWidth.PCT);
    }
    // Get a list of the rows in the table
    // List<XWPFTableRow> rows = table.getRows();
    int rowCt = 0;
    int colCt = 0;
    for (List<String> set : list) {
        XWPFTableRow row = table.getRow(rowCt);
        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 */
        240));
        // get the cells in this row
        List<XWPFTableCell> cells = row.getTableCells();
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            if (cells.size() == colCt) {
                row.createCell();
            }
            // get a table cell properties element (tcPr)
            XWPFTableCell cell = cells.get(colCt);
            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("FFFE99");
            } else if (rowCt % 2 == 0) {
                // even row
                // FFFFFF : 흰색
                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
            // 2015.3.17 fix
            // XWPFRun rh = para.createRun();
            KrXWPFRun rh = new KrXWPFRun(para.createRun());
            rh.setFontFamily(fontName);
            // style cell as desired
            if (colCt == colSize - 1) {
                // last column is 10pt Courier
                rh.setFontSize(DEFAULT_FONT_SIZE);
            }
            String content = it.next();
            if (rowCt == 0) {
                // header row
                rh.setText(content);
                rh.setFontSize(H5);
                rh.setBold(true);
                para.setAlignment(ParagraphAlignment.CENTER);
            } else if (rowCt % 2 == 0) {
                // even row
                addTableText(rh, content);
                rh.setSubscript(VerticalAlign.BASELINE);
                para.setAlignment(ParagraphAlignment.LEFT);
            } else {
                addTableText(rh, content);
                // odd row
                rh.setSubscript(VerticalAlign.BASELINE);
                para.setAlignment(ParagraphAlignment.LEFT);
            }
            colCt++;
        }
        // for cell
        colCt = 0;
        rowCt++;
    }
    if (property != null) {
        property.doCustom(table);
    }
    return table;
}
Also used : CTTblWidth(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTShd(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd) CTString(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString) CTTrPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) CTHeight(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight) CTString(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTVerticalJc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)

Example 4 with XWPFTableRow

use of org.apache.poi.xwpf.usermodel.XWPFTableRow in project Gargoyle by callakrsos.

the class MSWord method spanCellsAcrossRow.

/**
	 * 테이블의 특정 로우의 컬럼에 해당하는 부분의 셀을 나눔. 나누기는 하나.. 컬럼의 셀수가 일치하지 않고 틀어짐.
	 *
	 * @param table
	 * @param rowNum
	 * @param colNum
	 * @param span
	 */
private static void spanCellsAcrossRow(XWPFTable table, int rowNum, int colNum, int span) {
    XWPFTableRow row = table.getRow(rowNum);
    XWPFTableCell cell = row.getCell(colNum);
    CTDecimalNumber grdSpan = cell.getCTTc().getTcPr().getGridSpan();
    if (grdSpan == null) {
        grdSpan = cell.getCTTc().getTcPr().addNewGridSpan();
    }
    grdSpan.setVal(BigInteger.valueOf((long) span));
/*
		 * row가 0인경우 바로 아래래 로우의 컬럼수도 일치하도록 셀을 추가한다. 추가하지않는경우 컬럼셀의 수가 불일치한 상태로
		 * 문서가 만들어진다.
		 */
// if (rowNum == 0)
// {
// addTableCell(DIRECTION._0, table, rowNum, span);
// } else
// {
// addTableCell(DIRECTION.UP_DOWN, table, rowNum, span);
// }
}
Also used : XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) CTDecimalNumber(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Example 5 with XWPFTableRow

use of org.apache.poi.xwpf.usermodel.XWPFTableRow in project pmph by BCSquad.

the class WordHelper method fillDeclarationData.

private XWPFTable fillDeclarationData(XWPFTable table, DeclarationEtcBO bo) {
    List<XWPFTableRow> rows = table.getRows();
    List<XWPFTableCell> cells = rows.get(0).getTableCells();
    /* 第一行 */
    String realname = bo.getRealname();
    if (StringUtil.notEmpty(realname)) {
        cells.get(1).setText(realname);
    }
    /* 第二行 */
    cells = rows.get(1).getTableCells();
    String sex = bo.getSex();
    if (StringUtil.notEmpty(sex)) {
        cells.get(1).setText(sex);
    }
    String birthday = bo.getBirthday();
    if (StringUtil.notEmpty(birthday)) {
        cells.get(3).setText(birthday);
    }
    Integer experience = bo.getExperience();
    if (ObjectUtil.notNull(experience)) {
        cells.get(5).setText(String.valueOf(experience));
    }
    /* 第三行 */
    cells = rows.get(2).getTableCells();
    String orgname = bo.getOrgName();
    if (StringUtil.notEmpty(orgname)) {
        cells.get(1).setText(orgname);
    }
    String position = bo.getPosition();
    if (StringUtil.notEmpty(position)) {
        cells.get(3).setText(position);
    }
    String title = bo.getTitle();
    if (StringUtil.notEmpty(title)) {
        cells.get(5).setText(title);
    }
    /* 第四行 */
    cells = rows.get(3).getTableCells();
    String postcode = bo.getPostcode();
    if (StringUtil.notEmpty(postcode)) {
        cells.get(1).setText(postcode);
    }
    String address = bo.getAddress();
    if (StringUtil.notEmpty(address)) {
        cells.get(3).setText(address);
    }
    /* 第五行 */
    cells = rows.get(4).getTableCells();
    String telephone = bo.getTelephone();
    if (StringUtil.notEmpty(telephone)) {
        cells.get(1).setText(telephone);
    }
    String fax = bo.getFax();
    if (StringUtil.notEmpty(fax)) {
        cells.get(3).setText(fax);
    }
    String handphone = bo.getHandphone();
    if (StringUtil.notEmpty(handphone)) {
        cells.get(5).setText(handphone);
    }
    /* 第六行 */
    cells = rows.get(5).getTableCells();
    String email = bo.getEmail();
    if (StringUtil.notEmpty(email)) {
        cells.get(1).setText(email);
    }
    String idcard = bo.getIdcard();
    if (StringUtil.notEmpty(idcard)) {
        cells.get(3).setText(idcard);
    }
    String degree = bo.getDegree();
    if (StringUtil.notEmpty(degree)) {
        cells.get(5).setText(degree);
    }
    /* 第七行 */
    cells = rows.get(6).getTableCells();
    String isDispensed = bo.getIsDispensed();
    if (ObjectUtil.notNull(isDispensed)) {
        cells.get(1).setText(isDispensed);
    }
    String expertise = bo.getExpertise();
    if (StringUtil.notEmpty(expertise)) {
        cells.get(3).setText(expertise);
    }
    String isUtec = bo.getIsUtec();
    if (ObjectUtil.notNull(isUtec)) {
        cells.get(5).setText(isUtec);
    }
    return table;
}
Also used : BigInteger(java.math.BigInteger) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Aggregations

XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)27 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)26 BigInteger (java.math.BigInteger)9 Date (java.util.Date)8 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)6 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)5 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)5 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)3 CTTblPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)3 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ICell (org.apache.poi.xwpf.usermodel.ICell)2 XWPFSDTCell (org.apache.poi.xwpf.usermodel.XWPFSDTCell)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 CTDecimalNumber (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber)2 CTHeight (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight)2 CTShd (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd)2 CTString (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString)2 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)2 DeclarationEtcBO (com.bc.pmpheep.back.bo.DeclarationEtcBO)1