Search in sources :

Example 16 with CTTblPr

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr 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 17 with CTTblPr

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr in project poi by apache.

the class SimpleTable method createStyledTable.

/**
     * 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.

     * @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();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTShd(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd) CTTrPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) CTHeight(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CTString(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) CTVerticalJc(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)

Example 18 with CTTblPr

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr in project poi by apache.

the class XWPFTable method setWidth.

/**
     * @param width
     */
public void setWidth(int width) {
    CTTblPr tblPr = getTrPr();
    CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
    tblWidth.setW(new BigInteger("" + width));
}
Also used : CTTblWidth(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) BigInteger(java.math.BigInteger)

Example 19 with CTTblPr

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr in project poi by apache.

the class XWPFTable method setInsideHBorder.

public void setInsideHBorder(XWPFBorderType type, int size, int space, String rgbColor) {
    CTTblPr tblPr = getTrPr();
    CTTblBorders ctb = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders();
    CTBorder b = ctb.isSetInsideH() ? ctb.getInsideH() : ctb.addNewInsideH();
    b.setVal(xwpfBorderTypeMap.get(type));
    b.setSz(BigInteger.valueOf(size));
    b.setSpace(BigInteger.valueOf(space));
    b.setColor(rgbColor);
}
Also used : CTTblBorders(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders) CTBorder(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)

Example 20 with CTTblPr

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr in project poi by apache.

the class XWPFTable method getInsideVBorderType.

public XWPFBorderType getInsideVBorderType() {
    XWPFBorderType bt = null;
    CTTblPr tblPr = getTrPr();
    if (tblPr.isSetTblBorders()) {
        CTTblBorders ctb = tblPr.getTblBorders();
        if (ctb.isSetInsideV()) {
            CTBorder border = ctb.getInsideV();
            bt = stBorderTypeMap.get(border.getVal().intValue());
        }
    }
    return bt;
}
Also used : CTTblBorders(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders) CTBorder(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)

Aggregations

CTTblPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)26 CTTblBorders (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders)11 CTBorder (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder)10 CTTblWidth (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth)7 CTString (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString)6 CTTblCellMar (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblCellMar)5 CTDecimalNumber (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber)4 BigInteger (java.math.BigInteger)3 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)3 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)3 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)3 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)3 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)2 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)2 CTHeight (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight)2 CTShd (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd)2 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)2 CTTcPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)2