[代码优化](v2.6):序列化优化,Excel 导出优化

pull/545/head^2
zhengjie 2020-12-13 12:19:35 +08:00
parent 112c7b3e4d
commit cbaa49fbdf
2 changed files with 1 additions and 43 deletions

View File

@ -20,15 +20,11 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import me.zhengjie.exception.BadRequestException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -217,8 +213,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
sheet.trackAllColumnsForAutoSizing();
//列宽自适应
writer.autoSizeColumnAll();
//列宽自适应支持中文单元格
sizeChineseColumn(sheet, writer);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名不能为中文中文请自行编码
@ -231,34 +225,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
IoUtil.close(out);
}
/**
* ()
*/
private static void sizeChineseColumn(SXSSFSheet sheet, BigExcelWriter writer) {
for (int columnNum = 0; columnNum < writer.getColumnCount(); columnNum++) {
int columnWidth = sheet.getColumnWidth(columnNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
SXSSFRow currentRow;
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(columnNum) != null) {
SXSSFCell currentCell = currentRow.getCell(columnNum);
if (currentCell.getCellTypeEnum() == CellType.STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
// 如果长度大于最大值 65280那就取 length
if (columnWidth < length || (columnWidth * 256 > 65280)) {
columnWidth = length;
}
}
}
}
sheet.setColumnWidth(columnNum, columnWidth * 256);
}
}
public static String getFileType(String type) {
String documents = "txt doc pdf ppt pps xlsx xls docx";
String music = "mp3 wav wma mpa ram ra aac aif m4a";

View File

@ -81,15 +81,7 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
config.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
//保留空的字段
SerializerFeature.WriteMapNullValue,
//String null -> ""
SerializerFeature.WriteNullStringAsEmpty,
//Number null -> 0
SerializerFeature.WriteNullNumberAsZero,
//List null-> []
SerializerFeature.WriteNullListAsEmpty,
//Boolean null -> false
SerializerFeature.WriteNullBooleanAsFalse);
SerializerFeature.WriteMapNullValue);
converter.setFastJsonConfig(config);
converter.setSupportedMediaTypes(supportMediaTypeList);
converter.setDefaultCharset(StandardCharsets.UTF_8);