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

简介: 实战分享之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')
      });
    },
相关文章
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1791 3
|
人工智能 自然语言处理 API
快速集成GPT-4o:下一代多模态AI实战指南
快速集成GPT-4o:下一代多模态AI实战指南
860 101
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
1740 2
Spring Boot 3.x 微服务架构实战指南
|
消息中间件 Ubuntu Java
SpringBoot整合MQTT实战:基于EMQX实现双向设备通信
本教程指导在Ubuntu上部署EMQX 5.9.0并集成Spring Boot实现MQTT双向通信,涵盖服务器搭建、客户端配置及生产实践,助您快速构建企业级物联网消息系统。
3572 1
|
人工智能 Java API
Java与大模型集成实战:构建智能Java应用的新范式
随着大型语言模型(LLM)的API化,将其强大的自然语言处理能力集成到现有Java应用中已成为提升应用智能水平的关键路径。本文旨在为Java开发者提供一份实用的集成指南。我们将深入探讨如何使用Spring Boot 3框架,通过HTTP客户端与OpenAI GPT(或兼容API)进行高效、安全的交互。内容涵盖项目依赖配置、异步非阻塞的API调用、请求与响应的结构化处理、异常管理以及一些面向生产环境的最佳实践,并附带完整的代码示例,助您快速将AI能力融入Java生态。
1849 12
|
消息中间件 Java Apache
SpringBoot集成RocketMq
RocketMQ 是一款开源的分布式消息中间件,采用纯 Java 编写,支持事务消息、顺序消息、批量消息、定时消息及消息回溯等功能。其优势包括去除对 ZooKeeper 的依赖、支持异步和同步刷盘、高吞吐量及消息过滤等特性。RocketMQ 具备高可用性和高可靠性,适用于大规模分布式系统,能有效保障消息传输的一致性和顺序性。
2810 2
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
1003 3
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
1209 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
人工智能 自然语言处理 分布式计算
AI 驱动传统 Java 应用集成的关键技术与实战应用指南
本文探讨了如何将AI技术与传统Java应用集成,助力企业实现数字化转型。内容涵盖DJL、Deeplearning4j等主流AI框架选择,技术融合方案,模型部署策略,以及智能客服、财务审核、设备诊断等实战应用案例,全面解析Java系统如何通过AI实现智能化升级与效率提升。
1128 0