替换word中的字符和插入table数据

1、创建模板

2、引入jar包

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
</dependency>

3、组装数据

    @RequestMapping(value = "/print")
    @ResponseBody
    public void pdfPrint(String id) throws Exception {
        String[] ids = Convert.toStrArray(id);
        for(int i=0;i<ids.length;i++){
            YwglWzglRk ywglWzglRk = ywglWzglRkService.selectYwglWzglRkById(ids[i]);
            YwglWzglRkjl YwglWzglRkjl = new YwglWzglRkjl();
            YwglWzglRkjl.setRkid(ids[i]);
            List<YwglWzglRk> list = ywglWzglRkService.selectYwglWzglRkjlList(YwglWzglRkjl);
            HashMap<String, String> mapPDF = new HashMap<String, String>();
            mapPDF.put("gys", ywglWzglRk.getGysmc());//供应商
            mapPDF.put("djrq", DateUtils.parseDateToStr("yyyy-MM-dd",ywglWzglRk.getDjrq()));//单据日期
            mapPDF.put("djbm", ywglWzglRk.getDjbm());//单据编码
            mapPDF.put("zdr", ShiroUtils.getSysUser().getUserName());//制单人
            mapPDF.put("dysj", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));//打印日期
            mapPDF.put("table", "table");//打印日期
            doParagraphsPicture("D:\\rkd.docx", mapPDF,list, "D:\\入库单.docx");
        }
    }
 /**
     * 处理段落文字,把表示替换为图片插入文当中
     *
     * @throws InvalidFormatException
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void doParagraphsPicture(String tmpFile, Map<String, String> contentMap,List<YwglWzglRk> list, String exportFile) throws InvalidFormatException, IOException {
        InputStream is = new FileInputStream(new File(tmpFile));
        XWPFDocument document = new XWPFDocument(is);
        //**----------------------------处理段落------------------------------------**//*
        List<XWPFParagraph> paragraphList = document.getParagraphs();
        try {
            if (paragraphList != null && paragraphList.size() > 0) {
                for (XWPFParagraph paragraph : paragraphList) {
                    if (paragraph.getRuns().size() > 0) {
                        List<XWPFRun> runs = paragraph.getRuns();
                        for (XWPFRun run : runs) {
                            String text = run.getText(0);
                            // System.out.printf("文档中的参数:++++++++++++++++++++++++++++++++++"+text);
                            if (text != null) {
                                String key = text.replaceAll("\\$\\{", "").replaceAll("}", "");
                                System.out.println("text+++++:" + text);
                                if (!StringUtils.isEmpty(contentMap.get(key))) {
                                    if (text.indexOf("table") > -1) {// 动态表格
                                        run.setText("", 0);
                                        XmlCursor cursor = paragraph.getCTP().newCursor();
                                        XWPFTable tableOne = document.insertNewTbl(cursor);// ---这个是关键
                                        // 设置表格宽度,第一行宽度就可以了,这个值的单位,目前我也还不清楚,还没来得及研究
                                        tableOne.setWidth(8500);
                                        // 表格第一行,对于每个列,必须使用createCell(),而不是getCell(),因为第一行嘛,肯定是属于创建的,没有create哪里来的get呢
                                        XWPFTableRow tableOneRowOne = tableOne.getRow(0);//行
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRowOne.getCell(0), "微软雅黑", "10", 0, "center", "center", "#000000", "#B4C6E7", "10%", "序号");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "10", 0, "center", "center", "#000000", "#B4C6E7", "45%", "商品名称");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "10", 0, "center", "center", "#000000", "#B4C6E7", "45%", "规格型号");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "10", 0, "center", "center", "#000000", "#B4C6E7", "45%", "数量");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "10", 0, "center", "center", "#000000", "#B4C6E7", "45%", "单位");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "10", 0, "center", "center", "#000000", "#B4C6E7", "45%", "单价");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "10", 0, "center", "center", "#000000", "#B4C6E7", "45%", "价税合计");
                                        // 表格第二行
                                        BigDecimal rksl = new BigDecimal("0");
                                        BigDecimal hjje = new BigDecimal("0");
                                        int index = 1;
                                        for(YwglWzglRk rk : list){
                                            XWPFTableRow tableOneRowTwo = tableOne.createRow();//行
                                            new PoiWordTools().setWordCellSelfStyle(tableOneRowTwo.getCell(0), "微软雅黑", "9", 0, "center", "center", "#000000", "#FFFFFF", "10%", index+"");
                                            new PoiWordTools().setWordCellSelfStyle(tableOneRowTwo.getCell(1), "微软雅黑", "9", 0, "center", "center", "#000000", "#FFFFFF", "45%", rk.getWzmc());
                                            new PoiWordTools().setWordCellSelfStyle(tableOneRowTwo.getCell(2), "微软雅黑", "9", 0, "center", "center", "#000000", "#FFFFFF", "45%", DictUtils.getDictLabel("ckwzfl_type",rk.getWzlb()));
                                            new PoiWordTools().setWordCellSelfStyle(tableOneRowTwo.getCell(3), "微软雅黑", "9", 0, "center", "center", "#000000", "#FFFFFF", "45%", rk.getSl().toString());
                                            new PoiWordTools().setWordCellSelfStyle(tableOneRowTwo.getCell(4), "微软雅黑", "9", 0, "center", "center", "#000000", "#FFFFFF", "45%", DictUtils.getDictLabel("jldw_code",rk.getJldw()));
                                            new PoiWordTools().setWordCellSelfStyle(tableOneRowTwo.getCell(5), "微软雅黑", "9", 0, "center", "center", "#000000", "#FFFFFF", "45%", rk.getCgj().toString());
                                            new PoiWordTools().setWordCellSelfStyle(tableOneRowTwo.getCell(6), "微软雅黑", "9", 0, "center", "center", "#000000", "#FFFFFF", "45%", String.valueOf(rk.getCgj().multiply(new BigDecimal(String.valueOf(rk.getSl()))).setScale(2, RoundingMode.HALF_UP)));
                                            rksl = rksl.add(rk.getSl());
                                            hjje = hjje.add(rk.getCgj().multiply(new BigDecimal(String.valueOf(rk.getSl()))).setScale(2, RoundingMode.HALF_UP));
                                            index++;
                                        }
                                        XWPFTableRow tableOneRow3 = tableOne.createRow();//行
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRow3.getCell(0), "微软雅黑", "9", 0, "center", "center", "#000000", "#B4C6E7", "10%", "合计");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRow3.getCell(1), "微软雅黑", "9", 0, "center", "center", "#000000", "#B4C6E7", "45%", "");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRow3.getCell(2), "微软雅黑", "9", 0, "center", "center", "#000000", "#B4C6E7", "45%", "");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRow3.getCell(3), "微软雅黑", "9", 0, "center", "center", "#000000", "#B4C6E7", "45%", String.valueOf(rksl));
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRow3.getCell(4), "微软雅黑", "9", 0, "center", "center", "#000000", "#B4C6E7", "45%", "");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRow3.getCell(5), "微软雅黑", "9", 0, "center", "center", "#000000", "#B4C6E7", "45%", "");
                                        new PoiWordTools().setWordCellSelfStyle(tableOneRow3.getCell(6), "微软雅黑", "9", 0, "center", "center", "#000000", "#B4C6E7", "45%", String.valueOf(hjje));
                                    } else {
                                        run.setText(contentMap.get(key), 0);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            File file = new File(exportFile);
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(exportFile);
            document.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值