当前位置: 首页 > article >正文

Java easypoi导出word表格显示

1.成品

2.依赖

        <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.代码

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


http://www.kler.cn/a/293270.html

相关文章:

  • 数据集标注txt文件读取小工具
  • HAproxy 详解
  • Wireshark
  • Python多进程间通讯(包含共享内存方式)
  • 解决表格出现滚动条样式错乱问题
  • RHCE的学习(16)(shell脚本编程)
  • RAML学习
  • VBA进行excel坐标转换
  • CSP-S 2022 提高级 第一轮 阅读程序(3)
  • Redis进阶(五):集群
  • AWS-亚马逊网络服务(基础服务)-AWS 定价计算器-概述与动手部署:
  • c++ 实现线程池
  • 关于pip和conda环境路径不同的解决办法。
  • Mysql递归查询
  • 蜜罐网络MHN安装过程中的坑
  • Webpack 的loader和plugin原理
  • 类比推理-错题集
  • SpringBoot开发——如何防御XSS攻击
  • sqli-labs靶场(56-60)
  • 云计算之ECS
  • 常工院星闪节能团队参加悉尼大学设计交流项目
  • 中间代码例题
  • OSPF 协议介绍
  • Zipkin链路追踪②:如何集成?
  • 网络训练和推理过程
  • Android切换日夜模式导致Activity重建