Java easypoi导出word表格显示

1.成品

2.依赖

java 复制代码
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>

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

3.代码

java 复制代码
@Override
    public void importBInfo(HttpServletResponse response,Integer id) {
        TestRecordFault testRecordFault = testRecordFaultMapper.selectById(id);
        List<VehicleParts> vehiclePartsList = vehiclePartsMapper.selectList(new LambdaQueryWrapper<VehicleParts>().eq(VehicleParts::getModelId,
                testRecordFault.getModelId()));
        Map<Integer, VehicleParts> vehiclePartsMap = vehiclePartsList.stream().collect(Collectors.toMap(VehicleParts::getId, vehicleParts -> vehicleParts));
        JSONObject vehicle = new JSONObject();
        Unit unit = null;
        if (StringUtils.isNotBlank(testRecordFault.getVehicleInfo())) {
            vehicle = JSONObject.parseObject(testRecordFault.getVehicleInfo());
            Integer unitId = vehicle.getInteger("unitId");
            if (unitId != null) {
                unit = unitMapper.selectById(unitId);
            }
        }
        // 1 设置编码格式、文件名称
        response.setCharacterEncoding("utf-8");
        String contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        String fileName = testRecordFault.getFileName() + ".docx";
        response.setContentType(contentType);
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        Map<String, Object> map = new HashMap<>();
        // 1.基本属性填充
        map.put("fName", "F" + testRecordFault.getModelId());
        map.put("fileName", Optional.ofNullable(testRecordFault.getFileName()).orElse("--"));
        map.put("modelName", Optional.ofNullable(testRecordFault.getModelName()).orElse("--")); //Optional.ofNullable().orElse("--")
        map.put("powerHours", Optional.ofNullable(vehicle.get("powerHours")).orElse("--"));
        map.put("plateNumber", Optional.ofNullable(vehicle.get("plateNumber")).orElse("--"));
        map.put("powerPackWorking", Optional.ofNullable(vehicle.get("powerPackWorking")).orElse("--"));
        map.put("unitName", Optional.ofNullable(unit.getUnitName()).orElse("--"));
        map.put("powerPackYieldHours", Optional.ofNullable(vehicle.get("powerPackYieldHours")).orElse("--"));
        map.put("factoryDate", Optional.ofNullable(vehicle.get("factoryDate")).orElse("--"));
        map.put("taskPayloadWorkingHours", Optional.ofNullable(vehicle.get("taskPayloadWorkingHours")).orElse("--"));
        map.put("mileage", Optional.ofNullable(testRecordFault.getMileage()).orElse(0.0) );
        map.put("createTime", Optional.ofNullable(testRecordFault.getCreateTime()).orElse("--"));
        map.put("remark",Optional.ofNullable(testRecordFault.getRemark()).orElse("--"));

        //list所属系统

        //拼接数据结构
        // 2.商品详情列表填充
        List<Map<String, Object>> goodsWordList = new ArrayList<>();

        //解析下行数据
        List<DetailToB> list = JSON.parseArray(testRecordFault.getDetail(), DetailToB.class);
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> goodsMap = new HashMap<>();
            goodsMap.put("id", i+1); //Optional.ofNullable().orElse("--")
            goodsMap.put("testingProcess", Optional.ofNullable(list.get(i).getFileName()).orElse("--")); //检测流程名称
            goodsMap.put("item", Optional.ofNullable(list.get(i).getTestProject()).orElse("--")); //检测项目
            goodsMap.put("result", Optional.ofNullable(list.get(i).getTestResult()).orElse("--")); //检测结果
            goodsMap.put("partsName",Optional.ofNullable(list.get(i).getLittleUnit()).orElse("--")); //对应最小可更换单元
            goodsMap.put("subsystemName",Optional.ofNullable(list.get(i).getGenusSystem()).orElse("--")); //所属分系统
            goodsWordList.add(goodsMap);
        }
        map.put("goods", goodsWordList);

        //导出word
        String location = "/template/testB.docx";
        try {
            XWPFDocument document = new MyXWPFDocument(Objects.requireNonNull(this.getClass().getResourceAsStream(location)));
            // 读取模板
            WordExportUtil.exportWord07(document, map);
            document.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4.模板

5.testB内容

相关推荐
星落梦生4 小时前
SpringAOP基础笔记(7)
java·开发语言
金銀銅鐵5 小时前
带您看懂字节码指令(四):一条指令里有什么
java·jvm
最强小杰5 小时前
某 OpenAI 兼容网关上的 `kimi-k3.1-pro`:Cline、Zed 接入与 SSE 排错示例
java·服务器·网络·ai
Flynn Liang6 小时前
流量治理不是限流那么简单:I/O 密集型 Java 服务的 6 层防线
java·微服务·高并发·限流·熔断·系统稳定性·resilience4j
学长毕设6 小时前
基于SpringBoot智慧养老服务平台设计与实现-附源码
java·spring boot·后端
曹牧6 小时前
Java:“刷新雪崩”
java·开发语言
秋天的怀念_David6 小时前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Forget the Dream8 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
观察员9 小时前
告别频繁锁屏:5行 PowerShell 脚本实现 Windows 自动防休眠
windows·命令行
黑十9 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能