Browse Source

feat: 导出

master
李永德 2 months ago
parent
commit
eb6e524f12
  1. 92
      nladmin-system/nlsso-server/src/main/java/org/nl/common/utils/FileUtil.java

92
nladmin-system/nlsso-server/src/main/java/org/nl/common/utils/FileUtil.java

@ -19,12 +19,18 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.nl.common.exception.BadRequestException;
import org.nl.config.language.LangProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
@ -34,9 +40,7 @@ import java.io.*;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* File工具类扩展 hutool 工具包
@ -45,6 +49,7 @@ import java.util.Map;
* @date 2018-12-27
*/
public class FileUtil extends cn.hutool.core.io.FileUtil {
private static final Integer BATCH_WRITE_EXCEL_ROW_AMOUNT = 500;
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
@ -227,6 +232,86 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
IoUtil.close(out);
}
/**
* 流导出
* @param list
* @param response
* @throws IOException
*/
public void downloadExcelIO(List<Map<String, String>> list, String[] columnExcelNameArr, HttpServletResponse response) {
List<Map<String, String>> lastRestDataList = list;
int blockNum = 0;
List<String> allFileLocations = new ArrayList<>();
ServletOutputStream out = null;
try {
boolean allFinish = false;
while (!allFinish) {
int thisFileRowNumber = 1;
SXSSFWorkbook wb = null;
try {
// 流式写入EXCEL,只保留少数行(比如50行)数据在内存,防止内存溢出
wb = new SXSSFWorkbook(BATCH_WRITE_EXCEL_ROW_AMOUNT);
Sheet sh = wb.createSheet();
Row titleRow = sh.createRow(0);
for (int cellNum = 0; cellNum < columnExcelNameArr.length; cellNum++) {
Cell cell = titleRow.createCell(cellNum);
cell.setCellValue(columnExcelNameArr[cellNum]);
}
if (!CollectionUtils.isEmpty(lastRestDataList)) {
for (int i = 0; i < lastRestDataList.size(); i++) {
Row dataRow = sh.createRow(thisFileRowNumber);
Map<String, String> columnMap = lastRestDataList.get(i);
for (int cellNum = 0; cellNum < columnExcelNameArr.length; cellNum++) {
Cell cell = dataRow.createCell(cellNum);
String valueStr = columnMap.get(columnExcelNameArr[cellNum]);
cell.setCellValue(valueStr);
}
thisFileRowNumber++;
}
}
String localFilePath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + blockNum + ".xlsx";
allFileLocations.add(localFilePath);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + localFilePath);
out = response.getOutputStream();
wb.write(out);
// 必须清理流式写入Excel生成的临时文件
wb.dispose();
allFinish = true;
} catch (Exception ex) {
log.warn(ex.getMessage());
throw new BadRequestException(ex.getMessage());
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
log.warn(e.getMessage());
}
}
if (wb != null) {
try {
wb.dispose();
} catch (Exception e) {
log.warn(e.getMessage());
}
}
}
}
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
log.warn(e.getMessage());
}
}
}
}
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";
@ -343,5 +428,4 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
public static String getMd5(File file) {
return getMd5(getByte(file));
}
}

Loading…
Cancel
Save