Search in sources :

Example 16 with XWPFTableRow

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

the class WordHelper method fillDecClinicalRewardData.

private XWPFTable fillDecClinicalRewardData(XWPFTable table, List<DecClinicalReward> decClinicalRewards) {
    if (CollectionUtil.isEmpty(decClinicalRewards)) {
        return table;
    }
    if (decClinicalRewards.size() > 1) {
        int height = table.getRow(1).getHeight();
        for (int i = 1; i < decClinicalRewards.size(); i++) {
            table.createRow().setHeight(height);
        }
    }
    List<XWPFTableRow> rows = table.getRows();
    List<XWPFTableCell> cells;
    int rowCount = 1;
    for (DecClinicalReward decClinicalReward : decClinicalRewards) {
        cells = rows.get(rowCount).getTableCells();
        String value = decClinicalReward.getRewardName();
        if (StringUtil.notEmpty(value)) {
            cells.get(0).setText(value);
        }
        Date rewardDate = decClinicalReward.getRewardDate();
        if (null != rewardDate) {
            value = sdf.format(rewardDate);
            cells.get(1).setText(value);
        }
        Integer type = decClinicalReward.getAwardUnit();
        if (null != type) {
            switch(type) {
                case 0:
                    value = "无";
                    break;
                case 1:
                    value = "国际";
                    break;
                case 2:
                    value = "国家";
                    break;
                case 3:
                    value = "省部";
                    break;
                case 4:
                    value = "市";
                    break;
                default:
                    value = "其他";
                    break;
            }
            cells.get(2).setText(value);
        }
        value = decClinicalReward.getNote();
        if (StringUtil.notEmpty(value)) {
            cells.get(3).setText(value);
        }
        for (XWPFTableCell cell : cells) {
            cell.setVerticalAlignment(XWPFVertAlign.CENTER);
        }
        rowCount++;
    }
    return table;
}
Also used : BigInteger(java.math.BigInteger) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) DecClinicalReward(com.bc.pmpheep.back.po.DecClinicalReward) Date(java.util.Date) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Example 17 with XWPFTableRow

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

the class WordHelper method fillDecTextbookPmphData.

private XWPFTable fillDecTextbookPmphData(XWPFTable table, List<DecTextbookPmph> decTextbookPmphs) {
    if (CollectionUtil.isEmpty(decTextbookPmphs)) {
        return table;
    }
    if (decTextbookPmphs.size() > 1) {
        int height = table.getRow(1).getHeight();
        for (int i = 1; i < decTextbookPmphs.size(); i++) {
            table.createRow().setHeight(height);
        }
    }
    List<XWPFTableRow> rows = table.getRows();
    List<XWPFTableCell> cells;
    int rowCount = 1;
    for (DecTextbookPmph decTextbookPmph : decTextbookPmphs) {
        cells = rows.get(rowCount).getTableCells();
        String value = decTextbookPmph.getMaterialName();
        if (StringUtil.notEmpty(value)) {
            cells.get(0).setText(value);
        }
        /* 0=无/1=国家/2=省部/3=协编/4=校本/5=其他教材 */
        Integer rank = decTextbookPmph.getRank();
        if (null != rank) {
            switch(rank) {
                case 0:
                    value = "无";
                    break;
                case 1:
                    value = "国家";
                    break;
                case 2:
                    value = "省部";
                    break;
                case 3:
                    value = "协编";
                    break;
                case 4:
                    value = "校本";
                    break;
                case 5:
                    value = "其他";
                    break;
                default:
                    value = "无";
                    break;
            }
            cells.get(1).setText(value);
        }
        // 0=无/1=主编/2=副主编/3=编委
        Integer position = decTextbookPmph.getPosition();
        if (null != position) {
            switch(position) {
                case 1:
                    value = "主编";
                    break;
                case 2:
                    value = "副主编";
                    break;
                case 3:
                    value = "编委";
                    break;
                default:
                    value = "无";
                    break;
            }
            cells.get(2).setText(value);
        }
        value = decTextbookPmph.getIsDigitalEditor() ? "是" : "否";
        cells.get(3).setText(value);
        Date publishDate = decTextbookPmph.getPublishDate();
        if (null != publishDate) {
            value = sdf.format(publishDate);
            cells.get(4).setText(value);
        }
        value = decTextbookPmph.getIsbn();
        if (StringUtil.notEmpty(value)) {
            cells.get(5).setText(value);
        }
        value = decTextbookPmph.getNote();
        if (StringUtil.notEmpty(value)) {
            cells.get(6).setText(value);
        }
        for (XWPFTableCell cell : cells) {
            cell.setVerticalAlignment(XWPFVertAlign.CENTER);
        }
        rowCount++;
    }
    return table;
}
Also used : BigInteger(java.math.BigInteger) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) DecTextbookPmph(com.bc.pmpheep.back.po.DecTextbookPmph) Date(java.util.Date) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Example 18 with XWPFTableRow

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

the class XWPFWordExtractor method appendTableText.

private void appendTableText(StringBuffer text, XWPFTable table) {
    //this works recursively to pull embedded tables from tables
    for (XWPFTableRow row : table.getRows()) {
        List<ICell> cells = row.getTableICells();
        for (int i = 0; i < cells.size(); i++) {
            ICell cell = cells.get(i);
            if (cell instanceof XWPFTableCell) {
                text.append(((XWPFTableCell) cell).getTextRecursively());
            } else if (cell instanceof XWPFSDTCell) {
                text.append(((XWPFSDTCell) cell).getContent().getText());
            }
            if (i < cells.size() - 1) {
                text.append("\t");
            }
        }
        text.append('\n');
    }
}
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 19 with XWPFTableRow

use of org.apache.poi.xwpf.usermodel.XWPFTableRow 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 20 with XWPFTableRow

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

the class MSWord method mergeCellHorizon.

/**
	 * 수평셀 병합
	 *
	 * @param table
	 * @param rowNum
	 * @param colNum
	 * @param span
	 */
public void mergeCellHorizon(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));
}
Also used : XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) CTDecimalNumber(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber) 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