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内容

相关推荐
顾北川_野几秒前
Android 手机设备的OEM-unlock解锁 和 adb push文件
android·java
hairenjing11232 分钟前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
江深竹静,一苇以航3 分钟前
springboot3项目整合Mybatis-plus启动项目报错:Invalid bean definition with name ‘xxxMapper‘
java·spring boot
confiself18 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
Wlq041523 分钟前
J2EE平台
java·java-ee
XiaoLeisj30 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
豪宇刘1 小时前
SpringBoot+Shiro权限管理
java·spring boot·spring
Elaine2023911 小时前
02多线程基础知识
java·多线程
gorgor在码农1 小时前
Redis 热key总结
java·redis·热key
百事老饼干1 小时前
Java[面试题]-真实面试
java·开发语言·面试