前端代码
that.xxx.doPost('后端访问url',queryParam,'blob').then((res: any) => {
if (res.status == 200) {
// debugger;
const blob = new Blob([res.data],{type: 'application/octet-stream;charset=UTF-8'});
const fileName = '凭证应用统计分析.xls';
const link = document.createElement('a');
link.download = fileName;
link.style.display = 'none';
link.href = URL.createObjectURL(blob);
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href);
document.body.removeChild(link);
}
});
‘blob’一定要加上,否则下载文件打开乱码
后端代码
@EcpPostMapping("/方法名")
@Override
public void exportStatisticData(final HttpServletRequest req, final HttpServletResponse resp,
@RequestBody(required = true) final Map condition) {
byte[] exportBytes = null;
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
try {
String fileName = "凭证应用统计分析";
String fileType = "xls";
Map resultMap = iTEvpStatisticService.queryEvpStatisticByCondition(condition);
final Workbook exportWorkbook = ExcelHelpUtil.createWorkbook(fileType);
exportWorkbook.createSheet(fileName);
final CellStyle headCellStyle = exportWorkbook.createCellStyle();
final CellStyle contentCenterCellStyle = exportWorkbook.createCellStyle();
final Font headCellFont = exportWorkbook.createFont();
final Font contentFont = exportWorkbook.createFont();
final Sheet sheet0 = exportWorkbook.getSheetAt(0);
sheet0.setDefaultRowHeightInPoints((short) 25);
sheet0.setDefaultColumnWidth(25);
final Row headRow = sheet0.createRow(0); // 标题行
for (int j = 0; j < 5; j++) {
final Cell cell = headRow.createCell(j);
headCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
headCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
headCellStyle.setWrapText(true);
headCellFont.setFontHeightInPoints((short) 10);
headCellFont.setFontName("微软雅黑");
headCellFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headCellStyle.setFont(headCellFont);
headCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
headCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
headCellStyle.setBorderRight(CellStyle.BORDER_THIN);
headCellStyle.setBorderTop(CellStyle.BORDER_THIN);
headCellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
headCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell.setCellStyle(headCellStyle);
}
ExcelHelpUtil.setCellValue(fileType, sheet0, 0, 0, "序号");
ExcelHelpUtil.setCellValue(fileType, sheet0, 0, 1, "标题1");
ExcelHelpUtil.setCellValue(fileType, sheet0, 0, 2, "标题2");
ExcelHelpUtil.setCellValue(fileType, sheet0, 0, 3, "标题3");
ExcelHelpUtil.setCellValue(fileType, sheet0, 0, 4, "标题4");
List<Map> list = (List<Map>) resultMap.get("datas");
if (list != null && list.size() > 0) {
int indx = 1;
for (int i = 0; i < list.size(); i++) {
Map<String, Object> dataMap = list.get(i);
if (dataMap == null) {
continue;
}
final Row row = sheet0.createRow(indx);
row.setHeightInPoints((short) 25);
for (int j = 0; j < 5; j++) {
final Cell cell = row.createCell(j);
contentCenterCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
contentCenterCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
contentCenterCellStyle.setWrapText(true);
contentFont.setFontHeightInPoints((short) 10);
contentFont.setFontName("微软雅黑");
contentCenterCellStyle.setFont(contentFont);
contentCenterCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
contentCenterCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
contentCenterCellStyle.setBorderRight(CellStyle.BORDER_THIN);
contentCenterCellStyle.setBorderTop(CellStyle.BORDER_THIN);
cell.setCellStyle(contentCenterCellStyle);
}
ExcelHelpUtil.setCellValue(fileType, sheet0, indx, 0, String.valueOf(i + 1));
String buyerName = "";
if (dataMap.get("buyer_name") != null) {
buyerName = String.valueOf(dataMap.get("buyer_name"));
}
ExcelHelpUtil.setCellValue(fileType, sheet0, indx, 1, buyerName);
String buyerTaxNo = "";
if (dataMap.get("buyer_tax_no") != null) {
buyerTaxNo = String.valueOf(dataMap.get("buyer_tax_no"));
}
ExcelHelpUtil.setCellValue(fileType, sheet0, indx, 2, buyerTaxNo);
String pdfcount = "";
if (dataMap.get("pdfcount") != null) {
pdfcount = String.valueOf(dataMap.get("pdfcount"));
}
ExcelHelpUtil.setCellValue(fileType, sheet0, indx, 3, pdfcount);
String xbrlcount = "";
if (dataMap.get("xbrlcount") != null) {
xbrlcount = String.valueOf(dataMap.get("xbrlcount"));
}
ExcelHelpUtil.setCellValue(fileType, sheet0, indx, 4, xbrlcount);
indx++;
}
}
exportWorkbook.write(byteArrayOut);
exportBytes = byteArrayOut.toByteArray();
resp.reset();
resp.setStatus(200);
resp.setContentType("application/octet-stream;charset=UTF-8");
resp.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"",
URLEncoder.encode(fileName + "." + fileType, "UTF-8")));
OutputStream outputStream = new BufferedOutputStream(resp.getOutputStream());
resp.setCharacterEncoding("UTF-8");
outputStream.write(exportBytes);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}