EasyPOI导出 Word表格

项目中引用jar包

		<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>

文档样例

在这里插入图片描述

代码实现逻辑

public static void exportWord(String templatePath, String fileName,
                                  HttpServletRequest request, HttpServletResponse response) {
									  
		Map<String, Object> map = new HashMap<>();
        map.put("故障编号", "JXH_002");
        map.put("故障机号", "JH_092");
        map.put("故障日期", "2024-08-04");
        map.put("试验名称", "试验机名称1234");
        map.put("故障类型", "故障类型123");
        map.put("故障描述", "试验过程管理模块实现单试验管理,管理单个试验的试验名称、试验性质、试验周期及试验地点等属性,同时具备试验进度管理,能够添加不同试验阶段下的工作信息。");
        map.put("故障原因", "试验过程管理模块实现单试验管理,管理单个试验的试验名称、试验性质、试验周期及试验地点等属性,同时具备试验进度管理,能够添加不同试验阶段下的工作信息。");
        map.put("处理措施", "试验过程管理模块实现单试验管理,管理单个试验的试验名称、试验性质、试验周期及试验地点等属性,同时具备试验进度管理,能够添加不同试验阶段下的工作信息。");
        map.put("后续改进建议", "试验过程管理模块实现单试验管理,管理单个试验的试验名称、试验性质、试验周期及试验地点等属性,同时具备试验进度管理,能够添加不同试验阶段下的工作信息。");
        map.put("编写", "编写人");
        map.put("校对", "校对人");
        map.put("审核", "审核人");
         
		
		
        Assert.notNull(templatePath, "模板路径不能为空");
        Assert.notNull(fileName, "导出文件名不能为空");
        Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
        try {
            /*String userAgent = request.getHeader("user-agent").toLowerCase();
            if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } else {
                fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
            }*/
            XWPFDocument doc = null;
            try {
                doc = WordExportUtil.exportWord07(templatePath + "测试文档.docx", map);
            } catch (Exception e) {
                System.out.println("111");
                throw new RuntimeException(e);
            }
            //文本换行
            for (XWPFTable table : doc.getTables()) {//表格
                for (XWPFTableRow row : table.getRows()) {//行
                    for (XWPFTableCell cell : row.getTableCells()) {//单元格 : 直接cell.setText()只会把文字加在原有的后面,删除不了文字
                        addBreakInCell(cell);
                    }
                }
            }
            File file = new File(templatePath + fileName);
            FileOutputStream out = new FileOutputStream(file);
            // 设置强制下载不打开
           /* response.setContentType("application/force-download");
            // 设置文件名
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            OutputStream out = response.getOutputStream();*/
            doc.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // delAllFile(temDir);// 这一步看具体需求,要不要删
        }

    }

    private static void addBreakInCell(XWPFTableCell cell) {
        if (cell.getText() != null && cell.getText().contains("\n")) {
            for (XWPFParagraph p : cell.getParagraphs()) {
                for (XWPFRun run : p.getRuns()) {//XWPFRun对象定义具有一组公共属性的文本区域
                    if (run.getText(0) != null && run.getText(0).contains("\n")) {
                        String[] lines = run.getText(0).split("\n");
                        if (lines.length > 0) {
                            run.setText(lines[0], 0); // set first line into XWPFRun
                            for (int i = 1; i < lines.length; i++) {
                                // add break and insert new text
                                run.addBreak();//中断
//                                    run.addCarriageReturn();//回车符,但是不起作用
                                run.setText(lines[i]);
                            }
                        }
                    }
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值