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

简介: 实战分享之springboot+easypoi快速业务集成1

1.依赖引入

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>

2.封装easypoi工具类

```package com.wzz.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**

  • Excel导入导出工具类
  • @author pangu
    */
    public class ExcelUtil {

    /**

    • 导出工具类
    • @param list
    • @param title
    • @param sheetName
    • @param pojoClass
    • @param fileName
    • @param isCreateHeader
    • @param response
      */
      public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,

                              String fileName, boolean isCreateHeader, HttpServletResponse response){
      

      ExportParams exportParams = new ExportParams(title, sheetName);
      exportParams.setCreateHeadRows(isCreateHeader);
      defaultExport(list, pojoClass, fileName, response, exportParams);
      }

      /**

    • 导出工具类
    • @param list
    • @param title
    • @param sheetName
    • @param pojoClass
    • @param fileName
    • @param response
      */
      public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,

                              HttpServletResponse response){
      

      ExportParams exportParams = new ExportParams(title, sheetName);
      exportParams.setStyle(ExcelStyleUtil.class);
      defaultExport(list, pojoClass, fileName, response, exportParams);
      }

      public static void exportExcel(List> list, String fileName, HttpServletResponse response){
      defaultExport(list, fileName, response);
      }

      private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,

                                 HttpServletResponse response, ExportParams exportParams) {
      

      //TODO设置标题颜色

      Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
      if (workbook != null); downLoadExcel(fileName, response, workbook);
      }

      private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
      try {

       response.setCharacterEncoding("UTF-8");
       response.setHeader("content-Type", "application/vnd.ms-excel");
       response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
       workbook.write(response.getOutputStream());
      

      } catch (IOException e) {

       //throw new NormalException(e.getMessage());
      

      }
      }

      private static void defaultExport(List> list, String fileName, HttpServletResponse response) {
      Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
      if (workbook != null);
      downLoadExcel(fileName, response, workbook);
      }

      public static List importExcel(String filePath,Integer titleRows,Integer headerRows, Class pojoClass){
      if (StringUtils.isBlank(filePath)){

       return null;
      

      }
      ImportParams params = new ImportParams();
      params.setTitleRows(titleRows);
      params.setHeadRows(headerRows);
      List list = null;
      try {

       list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
      

      }catch (NoSuchElementException e){

       //throw new NormalException("模板不能为空");
      

      } catch (Exception e) {

       e.printStackTrace();
       //throw new NormalException(e.getMessage());
      

      } return list;
      }

      public static List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass){
      if (file == null){ return null;
      }
      ImportParams params = new ImportParams();
      params.setTitleRows(titleRows);
      params.setHeadRows(headerRows);
      List list = null;
      try {

       list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
      

      }catch (NoSuchElementException e){

       // throw new NormalException("excel文件不能为空");
      

      } catch (Exception e) {

       //throw new NormalException(e.getMessage());
       System.out.println(e.getMessage());
      

      }
      return list;
      }

}
```

相关文章
|
9月前
|
监控 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注册中心服务 构建商品
1378 3
|
8月前
|
人工智能 自然语言处理 API
快速集成GPT-4o:下一代多模态AI实战指南
快速集成GPT-4o:下一代多模态AI实战指南
622 101
|
7月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
1278 2
Spring Boot 3.x 微服务架构实战指南
|
8月前
|
消息中间件 Ubuntu Java
SpringBoot整合MQTT实战:基于EMQX实现双向设备通信
本教程指导在Ubuntu上部署EMQX 5.9.0并集成Spring Boot实现MQTT双向通信,涵盖服务器搭建、客户端配置及生产实践,助您快速构建企业级物联网消息系统。
2833 1
|
8月前
|
人工智能 Java API
Java与大模型集成实战:构建智能Java应用的新范式
随着大型语言模型(LLM)的API化,将其强大的自然语言处理能力集成到现有Java应用中已成为提升应用智能水平的关键路径。本文旨在为Java开发者提供一份实用的集成指南。我们将深入探讨如何使用Spring Boot 3框架,通过HTTP客户端与OpenAI GPT(或兼容API)进行高效、安全的交互。内容涵盖项目依赖配置、异步非阻塞的API调用、请求与响应的结构化处理、异常管理以及一些面向生产环境的最佳实践,并附带完整的代码示例,助您快速将AI能力融入Java生态。
1396 12
|
10月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
689 3
|
10月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
891 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
9月前
|
人工智能 自然语言处理 分布式计算
AI 驱动传统 Java 应用集成的关键技术与实战应用指南
本文探讨了如何将AI技术与传统Java应用集成,助力企业实现数字化转型。内容涵盖DJL、Deeplearning4j等主流AI框架选择,技术融合方案,模型部署策略,以及智能客服、财务审核、设备诊断等实战应用案例,全面解析Java系统如何通过AI实现智能化升级与效率提升。
767 0