Explorar el Código

修改导出问题清单代码

tags/V1.3.3^2
wanjing hace 11 meses
padre
commit
9d1dc56cae
Se han modificado 5 ficheros con 19 adiciones y 68 borrados
  1. +2
    -2
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/InspectionFileController.java
  2. +2
    -2
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/InspectionFileExportExcelDto.java
  3. +7
    -53
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/inspectionfile/export/ExportInspectionFileByIdListService.java
  4. +7
    -3
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/utils/excel/CustomCellWriteWidthHandler.java
  5. +1
    -8
      tuoheng-service/tuoheng-admin/src/test/java/com/tuoheng/admin/service/InspectionFileServiceTest.java

+ 2
- 2
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/InspectionFileController.java Ver fichero

@@ -147,7 +147,7 @@ public class InspectionFileController {
* @return
*/
@GetMapping("/export/{idList}")
public JsonResult exportExcleByIdList(HttpServletRequest request, HttpServletResponse response, @PathVariable("idList") List<String> idList) {
return iInspectionFileService.exportExcleByIdList(response, idList);
public void exportExcleByIdList(HttpServletRequest request, HttpServletResponse response, @PathVariable("idList") List<String> idList) {
iInspectionFileService.exportExcleByIdList(response, idList);
}
}

+ 2
- 2
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/InspectionFileExportExcelDto.java Ver fichero

@@ -12,7 +12,7 @@ import java.net.URL;

@Data
@HeadRowHeight(30)
@ContentRowHeight(150)
@ContentRowHeight(180)
@ContentFontStyle()
public class InspectionFileExportExcelDto {

@@ -26,7 +26,7 @@ public class InspectionFileExportExcelDto {
* 问题类型
*/
@ExcelProperty(value = {"问题类型"}, index = 1)
private String questionDesc;
private String questionName;

/**
* 问题图片

+ 7
- 53
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/inspectionfile/export/ExportInspectionFileByIdListService.java Ver fichero

@@ -4,10 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.conver.InspectionFileConverMapper;
import com.tuoheng.admin.dto.InspectionFileExportExcelDto;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.InspectionFile;
@@ -18,17 +15,17 @@ import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.utils.DownloadUtil;
import com.tuoheng.admin.utils.excel.CustomCellWriteHandler;
import com.tuoheng.admin.utils.excel.CustomCellWriteWidthHandler;
import com.tuoheng.admin.utils.excel.ImageModifyHandler;
import com.tuoheng.admin.utils.excel.StyleUtils;
import com.tuoheng.common.core.config.UploadFileConfig;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.FileUtils;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
@@ -85,7 +82,7 @@ public class ExportInspectionFileByIdListService {
return JsonResult.success();
}

String fileName = inspection.getName() + "-疑似问题清单.xlsx";
String fileName = inspection.getCode() + "-疑似问题清单.xlsx";
String filePath = UploadFileConfig.uploadFolder + "inspection_file/" + fileName;
File fd = new File(UploadFileConfig.uploadFolder + "inspection_file");
if (!fd.exists()) {
@@ -96,11 +93,10 @@ public class ExportInspectionFileByIdListService {

this.easyExcel(inspection, inspectionFileExportExcelDtoList, filePath);

response.setContentType("application/octet-stream;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
DownloadUtil.download(response, filePath, fileName);

FileUtils.deleteFile(filePath);

return JsonResult.success();
}

@@ -125,7 +121,7 @@ public class ExportInspectionFileByIdListService {
inspectionFile = inspectionFileList.get(i);
inspectionFileExportExcelDto = new InspectionFileExportExcelDto();
inspectionFileExportExcelDto.setSequence(i + 1);
inspectionFileExportExcelDto.setQuestionDesc(inspectionFile.getQuestionDesc());
inspectionFileExportExcelDto.setQuestionName(inspectionFile.getQuestionName());
try {
inspectionFileExportExcelDto.setFileOriginal(new URL(CommonConfig.imageURL + inspectionFile.getFileOriginal()));
inspectionFileExportExcelDto.setFileImage(new URL(CommonConfig.imageURL + inspectionFile.getFileImage()));
@@ -178,58 +174,16 @@ public class ExportInspectionFileByIdListService {

EasyExcel.write(filePath, InspectionFileExportExcelDto.class).sheet("问题清单数据")
.head(heads)
// .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20)) // 简单的列宽策略,列宽20
.registerWriteHandler(new CustomCellWriteHandler())
.registerWriteHandler(new CustomCellWriteWidthHandler())
// .registerWriteHandler(new CustomCellWriteHeightConfig())
.registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(new ImageModifyHandler())
.doWrite(inspectionFileExportExcelDtoList); // 数据源


// // 内容样式
// WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 水平居中
// contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); // 垂直居中
// contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);//细实线
// contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
// contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
// contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//
//
// //设计内容居中
// contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); //设置内容自动换行
// contentWriteCellStyle.setWrapped(true);
//
// //设置头部样式
// WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// //设置头部标题居中
// headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
// HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
//
// // 将数据写进流,如果是响应流则写进header对应的文件里
// ExcelWriter excelWriter = EasyExcel.write(filePath, InspectionFileExportExcelDto.class).build();
//
// //指定sheet的名字和头信息
// WriteSheet writeSheet = EasyExcel.writerSheet(0, "日历导出")
// .head(heads)
// .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20))
// .registerWriteHandler(horizontalCellStyleStrategy)
// .registerWriteHandler(new CustomCellWriteHandler())
//// .registerWriteHandler(new CustomCellWriteHeightConfig())
// .registerWriteHandler(new ImageModifyHandler())
// .build();
// //将数据真正写入excel中
// excelWriter.write(inspectionFileExportExcelDtoList, writeSheet);
// //千万别忘记finish 会帮忙关闭流
// excelWriter.finish();

} catch (Exception e) {
log.info("生成数据导出excel异常");
throw new RuntimeException(e);
}
log.info("开始写入表格中");

return JsonResult.success();
}
}

tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/utils/excel/CustomCellWriteHandler.java → tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/utils/excel/CustomCellWriteWidthHandler.java Ver fichero

@@ -18,7 +18,7 @@ import java.util.Map;
* @description: 自动列宽
* @version: 1.0
**/
public class CustomCellWriteHandler extends AbstractColumnWidthStyleStrategy {
public class CustomCellWriteWidthHandler extends AbstractColumnWidthStyleStrategy {
// 增加缓存 避免重复计算,影响性能
private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();

@@ -35,8 +35,12 @@ public class CustomCellWriteHandler extends AbstractColumnWidthStyleStrategy {
}
Integer columnWidth = 20;
Integer columnIndex = cell.getColumnIndex();
if (2 == columnIndex || 3 == columnIndex) {
columnWidth = 75;
if (0 == columnIndex) {
columnWidth = 15;
} else if (2 == columnIndex || 3 == columnIndex) {
columnWidth = 60;
} else if (4 == columnIndex || 5 == columnIndex || 6 == columnIndex) {
columnWidth = 35;
}
maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);

+ 1
- 8
tuoheng-service/tuoheng-admin/src/test/java/com/tuoheng/admin/service/InspectionFileServiceTest.java Ver fichero

@@ -2,11 +2,7 @@ package com.tuoheng.admin.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -26,9 +22,6 @@ import com.tuoheng.common.core.utils.CommonUtils;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -207,7 +200,7 @@ public class InspectionFileServiceTest {
EasyExcel.write(filePath, InspectionFileExportExcelDto.class).sheet("问题清单数据")
.head(heads)
// .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20)) // 简单的列宽策略,列宽20
.registerWriteHandler(new CustomCellWriteHandler())
.registerWriteHandler(new CustomCellWriteWidthHandler())
// .registerWriteHandler(new CustomCellWriteHeightConfig())
.registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(new ImageModifyHandler())

Cargando…
Cancelar
Guardar