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