Merge pull request #7790 from dataease/pr@dev@fix_report_log_export

fix: 定时报告执行记录导出乱码
This commit is contained in:
fit2cloud-chenyw 2024-01-24 14:43:16 +08:00 committed by GitHub
commit aa1740742e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,5 @@
package io.dataease.plugins.server;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
@ -24,6 +23,8 @@ import io.dataease.service.ScheduleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
@ -35,10 +36,10 @@ import org.springframework.web.util.HtmlUtils;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -340,35 +341,52 @@ public class XEmailTaskServer {
Pager<List<XpackTaskInstanceDTO>> listPager = instancesGrid(0, 0, request);
List<XpackTaskInstanceDTO> instanceDTOS = listPager.getListObject();
ExcelSheetModel excelSheetModel = excelSheetModel(instanceDTOS);
List<ExcelSheetModel> sheetModels = new ArrayList<>();
sheetModels.add(excelSheetModel);
File file = ExcelUtils.exportExcel(sheetModels, null, null);
InputStream inputStream = new FileInputStream(file);
HttpServletResponse response = ServletUtils.response();
OutputStream outputStream = response.getOutputStream();
try {
String filename = file.getName();
response.reset();
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/octet-stream;charset=UTF-8");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
ServletOutputStream outputStream = response.getOutputStream();
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
// 读取文件
bis = new BufferedInputStream(inputStream);
int i = bis.read(buff);
while (i != -1) {
outputStream.write(buff, 0, buff.length);
outputStream.flush();
i = bis.read(buff);
Workbook wb = new SXSSFWorkbook();
Sheet detailsSheet = wb.createSheet(excelSheetModel.getSheetName());
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();
//设置字体大小
font.setFontHeightInPoints((short) 12);
//设置字体加粗
font.setBold(true);
//给字体设置样式
cellStyle.setFont(font);
//设置单元格背景颜色
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//设置单元格填充样式(使用纯色背景颜色填充)
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
List<List<String>> details = null;
if (CollectionUtils.isNotEmpty(details = excelSheetModel.getData())) {
details.add(0, excelSheetModel.getHeads());
for (int i = 0; i < details.size(); i++) {
Row row = detailsSheet.createRow(i);
List<String> rowData = details.get(i);
if (rowData != null) {
for (int j = 0; j < rowData.size(); j++) {
Cell cell = row.createCell(j);
cell.setCellValue(rowData.get(j));
if (i == 0) {// 头部
detailsSheet.setColumnWidth(j, 255 * 20);
cell.setCellStyle(cellStyle);
}
}
}
}
}
response.setContentType("application/vnd.ms-excel");
//文件名称
String fileName = excelSheetModel.getSheetName();
String encodeFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + encodeFileName + ".xlsx");
wb.write(outputStream);
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (file.exists())
FileUtil.del(file);
} catch (Exception e) {
DataEaseException.throwException(e);
}
}