实战分享之springboot+easypoi快速业务集成2

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 实战分享之springboot+easypoi快速业务集成2

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')
      });
    },
相关文章
|
18天前
|
消息中间件 Java Kafka
Springboot集成高低版本kafka
Springboot集成高低版本kafka
|
25天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
283 0
|
30天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
426 0
|
21小时前
|
Java Spring
Spring Boot脚手架集成校验框架
Spring Boot脚手架集成校验框架
6 0
|
2天前
|
Java Docker 容器
SpringBoot项目集成XXL-job
SpringBoot项目集成XXL-job
|
4天前
|
Java 关系型数据库 数据库
【SpringBoot系列】微服务集成Flyway
【4月更文挑战第7天】SpringBoot微服务集成Flyway
【SpringBoot系列】微服务集成Flyway
|
19天前
|
SQL Java 调度
SpringBoot集成quartz定时任务trigger_state状态ERROR解决办法
SpringBoot集成quartz定时任务trigger_state状态ERROR解决办法
|
26天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
54 1
|
29天前
|
Java 测试技术 Maven
SpringBoot集成Elasticsearch
SpringBoot集成Elasticsearch
24 0
|
1月前
|
NoSQL Java Redis
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
272 1