读取docx文件,并插入数据,转换pdf下载


	 <!--操作docx-->
    <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi-ooxml.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>${poi-scratchpad.version}</version>
            </dependency>

 <!--word转pdf-->
            <dependency>
                <groupId>com.aspose.words</groupId>
                <artifactId>words</artifactId>
                <version>20.4</version>
            </dependency>
/**
     *  导出
     * */
    public void exportExcel(HttpServletResponse response, String title, List<Kqstatistic> lists){
            // 导出模板路径
            String path = Objects.requireNonNull(Objects.requireNonNull(ClassUtils.getDefaultClassLoader()).getResource("")).getPath();
            String filePath = path + "static/docTemplate/DOME.docx";
        try {
            FileInputStream fileInputStream = new FileInputStream("C:\\sn\\ideaDaiMa\\省外办\\省外办01\\zjswb_oa\\hzst-main\\src\\main\\resources\\static\\docTemplate\\DOME.docx");
            XWPFDocument xwpf = new XWPFDocument(fileInputStream);
			
			//插入标题
			List<XWPFParagraph> paragraphs = xwpf.getParagraphs();
            for (int i = 0; i < paragraphs.size(); i++) {
                XWPFParagraph item = paragraphs.get(i);
                String text = item.getText();
                if ("${mark_newParagraph}".equals(text)){
                    XmlCursor xmlCursor = item.getCTP().newCursor();
                    XWPFParagraph xwpfParagraph = xwpf.insertNewParagraph(xmlCursor);
                    xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
                    XWPFRun run = xwpfParagraph.createRun();
                    run.setText(title);
                    run.setFontFamily("宋体");
                    run.setFontSize(18);
                    run.setBold(true);
                    xwpf.removeBodyElement(xwpf.getPosOfParagraph(item));
                }
            }

			
            List<XWPFTable> tables = xwpf.getTables();
            //表
            XWPFTable xwpfTable = tables.get(0);
            xwpfTable.removeRow(1);
            //插入行
            for (int i = 0; i < lists.size(); i++) {
                XWPFTableRow row = xwpfTable.createRow();
                    row.getCell(0).setText(String.valueOf(i));
                    row.getCell(1).setText(lists.get(i).getMemberName());
                    row.getCell(2).setText(lists.get(i).getDepartName());
                    row.getCell(3).setText(lists.get(i).getYingchuqin());
                    row.getCell(4).setText(lists.get(i).getShichuqin());
                    row.getCell(5).setText(lists.get(i).getGongwuweiqian());
                    row.getCell(7).setText(lists.get(i).getCdztInfo());
                    row.getCell(8).setText(lists.get(i).getWaichugongwu());
                    row.getCell(9).setText(lists.get(i).getJiaban());
                    row.getCell(10).setText(lists.get(i).getTiaoxiu());
                    row.getCell(11).setText(lists.get(i).getSangjia());
                    row.getCell(12).setText(lists.get(i).getBingjia());
                    row.getCell(13).setText(lists.get(i).getNianxiujia());
                    row.getCell(14).setText(lists.get(i).getChanjia());
                    row.getCell(15).setText(lists.get(i).getHunjia());
                    row.getCell(16).setText(lists.get(i).getSangjia());
                    row.getCell(17).setText(lists.get(i).getTanqinjia());
                    row.getCell(18).setText(lists.get(i).getChanjia());
                    row.getCell(19).setText(lists.get(i).getHulijia());
                    row.getCell(20).setText(lists.get(i).getChuchai());
                    row.getCell(21).setText(lists.get(i).getQueqing());
                    row.getCell(22).setText(lists.get(i).getTiaoxiu());
            }

            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();//二进制OutputStream
            //将docx文件写入到二进制流中
            xwpf.write(baos);
            ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray());//OutputStream写入InputStream二进制流
			//
            Document document = new Document(in);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
           response.setHeader("Content-disposition",
                    "attachment;filename=" + new String((title+".pdf").getBytes("gb2312"), "ISO8859-1"));
            //下载
            document.save(response.getOutputStream(), SaveFormat.PDF);

        } catch (Exception e) {
            e.printStackTrace();
        }

先把文件下载到本地 再把本地的doc转成pdf 再把本地的pdf上传到oss

 /*
    * 先把文件下载到本地  再把本地的doc转成pdf  再把本地的pdf上传到oss
    * */
    @RequestMapping("wordToPdf")
    @ResponseBody
    public void wordToPdf(String annexId) throws Exception {
        WFAnnex annex = annexService.getById(annexId);

        System.out.println("开始word转pdf----annexId="+ annexId);

        SystemStorepath systemStorepath = systemStorepathService.getOne(
                new LambdaQueryWrapper<SystemStorepath>().eq(SystemStorepath::getStorePathName, "wps清稿前").eq(SystemStorepath::getIsDelete, 0));



        String pdfPath = null;
        if(annex != null){
            String annexPath = annex.getAnnexPath();
            log.info("annexPath==="+ annexPath);

            if(annexPath.contains(".doc")){
                log.info("打开文件 file");
                //下载到本地的doc路径
                String docSavePath = systemStorepath.getStoreNetPath() + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/")) + annexId + WFConsts.FILE_SUFFIX_DOC;
                //将oss上的附件下载到本地
                OSSUtils.download(annexPath,docSavePath);

                File file = new File(docSavePath);

                log.info("file 是否存在?"+ file.exists());

                if(file.exists()){
                    log.info("file  exists  true....");

                    Document doc = new Document(new FileInputStream(file));

                    //下载到本地的pdf路径
                    String pdfSavePath = systemStorepath.getStoreNetPath() + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/")) + annexId + WFConsts.FILE_SUFFIX_PDF;

                    log.info("本地保存的路径 pdfSavePath==="+ pdfSavePath);

                    OutputStream out = new FileOutputStream(pdfSavePath);
                    //清除痕迹
                    doc.acceptAllRevisions();
                    //doc转pdf  保存到本地
                    doc.save(out, SaveFormat.PDF);

                    log.info("本地保存成功。。。  开始上传oss。。。");

                    //上传到oss的pdf路径
                    pdfPath = annexPath.replace(".docx",".pdf");
                    log.info("去掉docx后缀  pdfPath===" + pdfPath);
                    pdfPath = pdfPath.replace(".doc",".pdf");

                    log.info("去掉doc后缀  pdfPath===" + pdfPath);

                    log.info("上传到oss的pdf pdfPath===="+pdfPath);
                    //本地pdf上传到oss
                    pdfPath = OSSUtils.upload(pdfPath, new FileInputStream(pdfSavePath),null);

                    log.info("wordToPdf  success...");


                    log.info("删除本地文件");


                }

            }
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值