3.Controller层直接调用
* 导出Excel excelVo
* 注意这里暂时只能用get方法,不能用post
*/
@GetMapping("/exportExcel")
@ApiOperation("根据过滤条件导出excel数据")
public void exportExcel(HttpServletResponse response,
@RequestParam(name = "pageNo") int pageNo,
@RequestParam("pageSize") int pageSize,
@RequestParam(name = "unit",required = false) String unit,
@RequestParam(name = "username",required = false) String username,
@RequestParam(name = "truename",required = false) String truename,
@RequestParam(name = "date",required = false) String date) throws IOException {
Evorecorddto queryInfo = new Evorecorddto();
queryInfo.setPageNo(pageNo);
queryInfo.setPageSize(pageSize);
if (unit !=null){
queryInfo.setUnit(unit);
}
if (username!=null){
queryInfo.setUsername(username);
}
if (truename!=null){
queryInfo.setTruename(truename);
}
if (date!=null){
ObjectMapper objectMapper = new ObjectMapper();
Evorecorddto.DateRange dateRange = objectMapper.readValue(date, Evorecorddto.DateRange.class);
queryInfo.setDate(dateRange);
}
// 查询所有答题用户列表
List<excelVo> records = evaluationService.excelprint(queryInfo);
ExcelUtil.exportExcel(records, null, "答题排名", excelVo.class, "答题排名", response);
}
另外需要修改excel导出颜色的话可以修改官方提供的接口
4.修改接口
```package com.wzz.utils;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
/**
- @author:Kevin
- @create: 2023-08-24 10:14
- @Description:
*/
public class ExcelStyleUtil implements IExcelExportStyler {
// 数据行类型
private static final String DATA_STYLES = "dataStyles";
// 标题类型
private static final String TITLE_STYLES = "titleStyles";
//数据行样式
private CellStyle styles;
// 标题样式
private CellStyle titleStyle;
public ExcelStyleUtil(Workbook workbook) {
this.init(workbook);
}
private void init(Workbook workbook) {
this.styles = initStyles(workbook);
this.titleStyle = initTitleStyle(workbook);
}
@Override
public CellStyle getHeaderStyle(short i) {
return null;
}
@Override
public CellStyle getTitleStyle(short i) {
return titleStyle;
}
@Override
public CellStyle getStyles(boolean b, ExcelExportEntity excelExportEntity) {
return styles;
}
@Override
public CellStyle getStyles(Cell cell, int i, ExcelExportEntity excelExportEntity, Object o, Object o1) {
return getStyles(true,excelExportEntity);
}
@Override
public CellStyle getTemplateStyles(boolean b, ExcelForEachParams excelForEachParams) {
return null;
}
/**
* 初始化--标题行样式
* @param workbook
* @return
*/
private CellStyle initTitleStyle(Workbook workbook) {
return buildCellStyle(workbook,TITLE_STYLES);
}
/**
* 初始化--数据行样式
* @param workbook
* @return
*/
private CellStyle initStyles(Workbook workbook) {
return buildCellStyle(workbook,DATA_STYLES);
}
/**
* 设置单元格样式
* @param workbook
* @param type 类型 用来区分是数据行样式还是标题样式
* @return
*/
private CellStyle buildCellStyle(Workbook workbook,String type) {
CellStyle style = workbook.createCellStyle();
// 字体样式
Font font = workbook.createFont();
if(TITLE_STYLES.equals(type)){
// 背景色
style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
font.setFontHeightInPoints((short)12);
font.setBold(true);
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
}
if(DATA_STYLES.equals(type)){
font.setFontHeightInPoints((short)10);
}
font.setFontName("Courier New");
style.setFont(font);
// 设置底边框
style.setBorderBottom(BorderStyle.THIN);
// 设置左边框
style.setBorderLeft(BorderStyle.THIN);
// 设置右边框;
style.setBorderRight(BorderStyle.THIN);
// 设置顶边框;
style.setBorderTop(BorderStyle.THIN);
// 设置底边颜色
style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
// 设置左边框颜色;
style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
// 设置右边框颜色;
style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
// 设置顶边框颜色;
style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
// 设置自动换行;
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HorizontalAlignment.CENTER);
// 设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(VerticalAlignment.CENTER);
return style;
}
}
5.前端代码
```//导出excel
exportExcel(){
//https://slgstu.top/api/common/exportExcel
axios.get("https://slgstu.top/api/common/exportExcel",{
params: this.queryInfo,
headers: { 'Content-Type': 'application/json,charset=utf-8'},
responseType: 'blob', //二进制流
}).then(res => {
console.log(res)
this.$notify({
title: '提示',
message: '正在导出,请稍等!'
});
// console.log(res);
// let blob = new Blob([res], { type: 'application/vnd.ms-excel,charset=utf-8' });
// let url =window.URL.createObjectURL(blob);
// let link = document.createElement('a');
// link.download = '答题记录.xlsx';
// link.href = url;
// link.click();
downloadFile(res.data, "答题排名", 'xlsx')
});
},