前后端导出excel
Posted 棘丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前后端导出excel相关的知识,希望对你有一定的参考价值。
后端
1.导入poi包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
2.完成向前端导出文件流
@RequestMapping(value = "export", method = RequestMethod.GET)
public void export(HttpServletResponse response) {
//获取所有
List<MicData> micData = dataService.findAllExcept();
try (HSSFWorkbook wb = new HSSFWorkbook()) {
writeChargeSheet(micData, wb);
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=" + "data.xls");
try {
wb.write(response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException("请重新导出");
}
}
private void writeChargeSheet(List<MicData> micDataList, HSSFWorkbook wb) {
Sheet sheet = wb.createSheet("麦克风记录数据");
writeHeadCell(sheet.createRow(0), "线号", "创建时间", "测试人员", "测试次数", "杆号", "1号麦克风Max","1号麦克风Min","2号麦克风Max","2号麦克风Min",
"3号麦克风Max","3号麦克风Min","4号麦克风Max","4号麦克风Min","5号麦克风Max","5号麦克风Min","6号麦克风Max","6号麦克风Min","7号麦克风Max","7号麦克风Min",
"8号麦克风Max","8号麦克风Min","9号麦克风Max","9号麦克风Min");
sheet.setColumnWidth(0, 10 * 256);
sheet.setColumnWidth(1, 19 * 256);
sheet.setColumnWidth(2, 13 * 256);
sheet.setColumnWidth(4, 13 * 256);
sheet.setColumnWidth(5, 10 * 256);
sheet.setColumnWidth(6, 8 * 256);
sheet.setColumnWidth(8, 8 * 256);
sheet.setColumnWidth(9, 8 * 256);
sheet.setColumnWidth(10, 8 * 256);
sheet.setColumnWidth(11, 8 * 256);
sheet.setColumnWidth(12, 8 * 256);
sheet.setColumnWidth(13, 8 * 256);
sheet.setColumnWidth(14, 8 * 256);
sheet.setColumnWidth(15, 8 * 256);
sheet.setColumnWidth(16, 8 * 256);
sheet.setColumnWidth(17, 8 * 256);
sheet.setColumnWidth(18, 8 * 256);
sheet.setColumnWidth(19, 8 * 256);
sheet.setColumnWidth(20, 8 * 256);
sheet.setColumnWidth(21, 8 * 256);
sheet.setColumnWidth(22, 8 * 256);
sheet.setColumnWidth(23, 8 * 256);
sheet.setColumnWidth(24, 8 * 256);
for (int i = 0; i < micDataList.size(); i++) {
writeChargeCell(sheet.createRow(i + 1), micDataList.get(i));
}
}
private void writeHeadCell(Row r, String... vs) {
for (int i = 0; i < vs.length; i++) {
r.createCell(i).setCellValue(vs[i]);
}
}
private void writeChargeCell(Row r, MicData micData) {
int cellIndex = 0;
r.createCell(cellIndex++).setCellValue(micData.getLineNo());
r.createCell(cellIndex++).setCellValue(micData.getCreateTime());
r.createCell(cellIndex++).setCellValue(micData.getCreateName());
r.createCell(cellIndex++).setCellValue(micData.getNumber());
r.createCell(cellIndex++).setCellValue(micData.getRodNo());
r.createCell(cellIndex++).setCellValue(micData.getMic1Max());
r.createCell(cellIndex++).setCellValue(micData.getMic1Min());
r.createCell(cellIndex++).setCellValue(micData.getMic2Max());
r.createCell(cellIndex++).setCellValue(micData.getMic2Min());
r.createCell(cellIndex++).setCellValue(micData.getMic3Max());
r.createCell(cellIndex++).setCellValue(micData.getMic3Min());
r.createCell(cellIndex++).setCellValue(micData.getMic4Max());
r.createCell(cellIndex++).setCellValue(micData.getMic4Min());
r.createCell(cellIndex++).setCellValue(micData.getMic5Max());
r.createCell(cellIndex++).setCellValue(micData.getMic5Min());
r.createCell(cellIndex++).setCellValue(micData.getMic6Max());
r.createCell(cellIndex++).setCellValue(micData.getMic6Min());
r.createCell(cellIndex++).setCellValue(micData.getMic7Max());
r.createCell(cellIndex++).setCellValue(micData.getMic7Min());
r.createCell(cellIndex++).setCellValue(micData.getMic8Max());
r.createCell(cellIndex++).setCellValue(micData.getMic8Min());
r.createCell(cellIndex++).setCellValue(micData.getMic9Max());
r.createCell(cellIndex++).setCellValue(micData.getMic9Min());
}
3.前端接收
function derVice(){
$.ajax({
headers: {
\'content-type\':\'application/json;charset=ISO8859-1\'
},
type: "GET",
url: "api/export",
xhrFields: {
responseType: "arraybuffer",
},
success: function (res, _, xhr) {
console.log(res);
let [, fileName] = xhr
.getResponseHeader("Content-Disposition")
.match(/filename=(.*)$/);
let blob = new Blob([res], {
type: "application/vnd.ms-excel",
});
let link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
window.URL.revokeObjectURL(link.href);
},
});
}
以上是关于前后端导出excel的主要内容,如果未能解决你的问题,请参考以下文章
关于springboot通过jxls用模板导出excel的前后台实现
从零开始搭建django前后端分离项目 系列五(实战之excel流式导出)
前端如何实现将多页数据合并导出到Excel单Sheet页解决方案|内附代码