controller返回为map,map中存文件名
AjaxResult.success(filename);
//创建工作薄
Workbook wb = new XSSFWorkbook();
//创建sheet
Sheet sheet = wb.createSheet("运营数据报表");
// 自动设置宽度
sheet.autoSizeColumn(0);
sheet.setColumnWidth(0,14000);
sheet.setDefaultRowHeight((short) 500);
Row titleRow = sheet.createRow(0);
Cell cell = titleRow.createCell(i);
cell.setCellStyle(titleStyle);
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0,0,0,sheet.getRow(0).getLastCellNum()-1));
doExport(wb);//执行导出方法,下面
执行导出方法
public AjaxResult doExport(Workbook wb){
OutputStream out = null;
try {
String filename = "数据报表.xlsx";
out = new FileOutputStream(getAbsoluteFile(filename));
wb.write(out);
return AjaxResult.success(filename);
} catch (Exception e) {
e.printStackTrace();
throw new CustomException("导出Excel失败,请联系网站管理员!");
} finally {
if (wb != null) {
try {
wb.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
单元格样式
public CellStyle createTitleStyle(Workbook wb){
CellStyle titleStyle = wb.createCellStyle();
// 设置单元格对齐方式,水平居左
titleStyle.setAlignment(HorizontalAlignment.LEFT);
//单元格边框
titleStyle.setBorderTop(BorderStyle.THIN);
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
//titleStyle.setBorderBottom(BorderStyle.THIN);
// 设置字体样式
Font titleFont = wb.createFont();
// 字体高度
titleFont.setFontHeightInPoints((short) 16);
// 字体样式
titleFont.setFontName("宋体");
titleFont.setBold(true);
titleStyle.setFont(titleFont);
// 设置居中样式,水平居中
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleInnerStyle.setRightBorderColor((short) 0);//边框黑色
titleInnerStyle.setBorderRight(BorderStyle.THIN);
titleInnerStyle.setBorderLeft(BorderStyle.THIN);
titleInnerStyle.setLeftBorderColor((short) 0);//左侧白色色边框
return titleStyle;
}
前端:
exportExcel() {
const _this = this
this.$confirm('是否确认导出数据项?', '警告', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(function() {
_this.loading = true
return exportReport(_this.dataParam)
}).then(response => {
_this.loading = false
this.download(response.msg)
}).catch(function() {})
/* /!* generate workbook object from table *!/
var xlsxParam = { raw: true } // 导出的内容只做解析,不进行格式转换
var wb = XLSX.utils.table_to_book(document.querySelector('#exportTab'), xlsxParam)
/!* get binary string as output *!/
var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
try {
FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '审核情况表.xlsx')
} catch (e) {
if (typeof console !== 'undefined') {
console.log(e, wbout)
}
}
return wbout*/
},