java使用POI读写Word

java使用POI读写Word

 /**
     * 填充内容
     */
    private  void writerRport () {
        String filePathEncoding = this.getClass().getClassLoader().getResource("report/测试报告模板V1.0.docx").getPath();
        try{
            String filePath = URLDecoder.decode(filePathEncoding,"utf-8");
            FileInputStream is = new FileInputStream(filePath);
            XWPFDocument document = new XWPFDocument(is);
            List paragraphs = document.getParagraphs();
            for(int i=0; i rows = xwpfTable.getRows();
        for (XWPFTableRow row : rows) {
            List cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
                CTTc cttc = cell.getCTTc();
                CTP ctp = cttc.getPList().get(0);
                CTPPr ctppr = ctp.getPPr();
                if (ctppr == null) {
                    ctppr = ctp.addNewPPr();
                }
                CTJc ctjc = ctppr.getJc();
                if (ctjc == null) {
                    ctjc = ctppr.addNewJc();
                }
                ctjc.setVal(STJc.Enum.forString(horizontalLocation)); //水平居中
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.valueOf(verticalLoction));//垂直居中
            }
        }
    }

    /**
     * 设置表格位置
     *
     * @param xwpfTable
     * @param location  整个表格居中center,left居左,right居右,both两端对齐
     */
    private void setTableLocation(XWPFTable xwpfTable, String location) {
        CTTbl cttbl = xwpfTable.getCTTbl();
        CTTblPr tblpr = cttbl.getTblPr() == null ? cttbl.addNewTblPr() : cttbl.getTblPr();
        CTJc cTJc = tblpr.addNewJc();
        cTJc.setVal(STJc.Enum.forString(location));
    }

你可能感兴趣的:(java,word,windows)