SpringBoot快速集成jxls-poi(自定义模板,支持本地文件导出,在线文件导出)

 更新时间:2020年9月6日 23:28  点击:1435

在项目持续集成的过程中,有时候需要实现报表导出和文档导出,类似于excel中这种文档的导出,在要求不高的情况下,有人可能会考虑直接导出csv文件来简化导出过程。但是导出xlsx文件,其实过程相对更复杂。解决方案就是使用poi的jar包。使用源生的poi来操作表格,代码冗余,处理复杂,同时poi的相关联的依赖还会存在版本兼容问题。所以直接使用poi来实现表格导出,维护成本大,不易于拓展。

我们需要学会站在巨人的肩膀上解决问题,jxls-poi这个就很好解决这个excel表格导出的多样化的问题。类似jsp和thymealf的模板定义,使得表格导出变得简单可控。

不多BB上代码

1.引入关键依赖包

<!-- jxls-api依赖 -->
		<dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls-poi</artifactId>
			<version>1.0.15</version>
		</dependency>

		<dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls</artifactId>
			<version>2.4.6</version>
		</dependency>

这里只需要两个依赖便操作excel表格了。

2.定义模板文件


新建一个excel文件,后缀名为.xlsx,在resources目录下新增一个jxls的文件夹,把模板文件放在这个文件夹下,便于后续的spring-boot的集成。

3.导出工具类

/**
 * @author machenike
 */
public class ExcelUtils {

  /***
   * excel导出到response
   * @param fileName   导出文件名
   * @param templateFile 模板文件地址
   * @param params     数据集合
   * @param response   response
   */
  public static void exportExcel(String fileName, InputStream templateFile, Map<String, Object> params,
                  HttpServletResponse response) throws IOException {
    response.reset();
    response.setHeader("Accept-Ranges", "bytes");
    OutputStream os = null;
    response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
    response.setContentType("application/octet-stream;charset=UTF-8");
    try {
      os = response.getOutputStream();
      exportExcel(templateFile, params, os);
    } catch (IOException e) {
      throw e;
    }
  }

  /**
   * 导出excel到输出流中
   * @param templateFile 模板文件
   * @param params 传入参数
   * @param os 输出流
   * @throws IOException
   */
  public static void exportExcel(InputStream templateFile, Map<String, Object> params, OutputStream os) throws IOException {
    try {
      Context context = new Context();
      Set<String> keySet = params.keySet();
      for (String key : keySet) {
        //设置参数变量
        context.putVar(key, params.get(key));
      }
      Map<String, Object> myFunction = new HashMap<>();
      myFunction.put("fun", new ExcelUtils());
      // 启动新的jxls-api 加载自定义方法
      Transformer trans = TransformerFactory.createTransformer(templateFile, os);
      JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) trans.getTransformationConfig().getExpressionEvaluator();
      evaluator.getJexlEngine().setFunctions(myFunction);

      // 载入模板、处理导出
      AreaBuilder areaBuilder = new XlsCommentAreaBuilder(trans);
      List<Area> areaList = areaBuilder.build();
      areaList.get(0).applyAt(new CellRef("sheet1!A1"), context);
      trans.write();
    } catch (IOException e) {
      throw e;
    } finally {
      try {
        if (os != null) {
          os.flush();
          os.close();
        }
        if (templateFile != null) {
          templateFile.close();
        }
      } catch (IOException e) {
        throw e;
      }
    }
  }

  /**
   * 格式化时间
   */
  public Object formatDate(Date date) {
    if (date != null) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String dateStr = sdf.format(date);
      return dateStr;
    }
    return "--";
  }


  /**
   * 设置超链接方法
   */
  public WritableCellValue getLink(String address, String title) {
    return new WritableHyperlink(address, title);
  }
}

这个工具类中我定义两个导出用的方法
一个是直接是HttpServletResponse 导出在线下载文件
一个使用输入流导出
同时模板中还支持方法传入。

4.定义导出服务类

全局配置jxls模板的基础路径

#jxls模板的基础路径
jxls.template.path: classpath:jxls/

定义服务接口

/**
 * excel用service
 */
public interface ExcelService {

  /**
   * 导出excel,写入输出流中
   * @param templateFile
   * @param params
   * @param os
   * @return
   */
  boolean getExcel(String templateFile,Map<String,Object> params, OutputStream os);

  /**
   * 导出excel,写入response中
   * @param templateFile
   * @param fileName
   * @param params
   * @param response
   * @return
   */
  boolean getExcel(String templateFile,String fileName, Map<String,Object> params, HttpServletResponse response);

  /**
   * 导出excel,写入文件中
   * @param templateFile
   * @param params
   * @param outputFile
   * @return
   */
  boolean getExcel(String templateFile, Map<String,Object> params, File outputFile);
}

excel导出用服务实现类

/**
 * excel用serviceImpl
 */
@Service
public class ExcelServiceImppl implements ExcelService {

  private static final Logger logger = LoggerFactory.getLogger(ExcelServiceImppl.class);

  /**
   * 模板文件的基础路径
   */
  @Value("${jxls.template.path}")
  private String templatePath;

  @Override
  public boolean getExcel(String templateFile, Map<String, Object> params, OutputStream os) {
    FileInputStream inputStream = null;
    try {
      //获取模板文件的输入流
      inputStream = new FileInputStream(ResourceUtils.getFile(templatePath + templateFile));
      //导出文件到输出流
      ExcelUtils.exportExcel(inputStream, params, os);
    } catch (IOException e) {
      logger.error("excel export has error" + e);
      return false;
    }
    return true;
  }

  @Override
  public boolean getExcel(String templateFile, String fileName, Map<String, Object> params, HttpServletResponse response) {
    FileInputStream inputStream = null;
    try {
      //获取模板文件的输入流
      inputStream = new FileInputStream(ResourceUtils.getFile(templatePath + templateFile));
      //导出文件到response
      ExcelUtils.exportExcel(fileName,inputStream,params,response);
    } catch (IOException e) {
      logger.error("excel export has error" + e);
      return false;
    }
    return true;
  }

  @Override
  public boolean getExcel(String templateFile, Map<String, Object> params, File outputFile) {
    FileInputStream inputStream = null;
    try {
      //获取模板文件的输入流
      inputStream = new FileInputStream(ResourceUtils.getFile(templatePath + templateFile));
      File dFile = outputFile.getParentFile();
      //文件夹不存在时创建文件夹
      if(dFile.isDirectory()){
        if(!dFile.exists()){
          dFile.mkdir();
        }
      }
      //文件不存在时创建文件
      if(!outputFile.exists()){
        outputFile.createNewFile();
      }
      //导出excel文件
      ExcelUtils.exportExcel(inputStream, params, new FileOutputStream(outputFile));
    } catch (IOException e) {
      logger.error("excel export has error" + e);
      return false;
    }
    return true;
  }
}

如上,服务类提供了,三种导出的实现方法

  • 导出excel写入response中
  • 导出excel写入输出流中
  • 导出excel写入文件中

这三种方法足以覆盖所有的业务需求

5.编辑jxls模板

这里为方便导出最好定义与模板匹配的实体类,便于数据的装载和导出

public class UserModel {

  private Integer id;

  private String name;

  private String sex;

  private Integer age;

  private String remark;

  private Date date;

  private String link;
  }


插入标注定义模板,定义迭代对象jx:each

jx:each(items="list" var="item" lastCell="G3")

上面G3 模板中迭代的结束位置,然后用类似EL表达式的方式填充到模板当中


填写范围jx:area

jx:area(lastCell="G3")

模板编辑完成后保存即可,

6.代码测试使用

导出为本地文件

@SpringBootTest
class BlogJxlsApplicationTests {

  @Autowired
  ExcelService excelService;

  @Test
  void contextLoads() {
    Map<String, Object> params = new HashMap();
    List<UserModel> list = new ArrayList<>();
    list.add(new UserModel(1, "test01", "男", 25, "tttttttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(2, "test02", "男", 20, "tttttttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(3, "test04", "女", 25, "ttttddddasdadatttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(4, "test08", "男", 20, "ttttttdasdatttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(5, "test021", "女", 25, "ttttdatttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(7, "test041", "男", 25, "ttdadatttttttt",new Date(),"htpp://wwww.baidu.com"));

		params.put("list", list);
    excelService.getExcel("t1.xlsx", params, new File("D:\\test05.xlsx"));
  }
}

导出成功

在线导出文件

@RestController
public class TestController {
  @Autowired
  ExcelService excelService;

  @RequestMapping("test")
  public void testFile(HttpServletResponse response){
    Map<String, Object> params = new HashMap();
    List<UserModel> list = new ArrayList<>();
    list.add(new UserModel(1, "test01", "男", 25, "tttttttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(2, "test02", "男", 20, "tttttttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(3, "test04", "女", 25, "ttttddddasdadatttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(4, "test08", "男", 20, "ttttttdasdatttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(5, "test021", "女", 25, "ttttdatttttt",new Date(),"htpp://wwww.baidu.com"));
    list.add(new UserModel(7, "test041", "男", 25, "ttdadatttttttt",new Date(),"htpp://wwww.baidu.com"));

    params.put("list", list);
    excelService.getExcel("t1.xlsx",System.currentTimeMillis()+".xlsx", params,response);
  }
}


导出成功


源码地址
https://github.com/DavidLei08/BlogJxls.git

到此这篇关于SpringBoot快速集成jxls-poi(自定义模板,支持本地文件导出,在线文件导出)的文章就介绍到这了,更多相关SpringBoot集成jxls-poi内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • 解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题

    这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • 详解springBoot启动时找不到或无法加载主类解决办法

    这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • 解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • Springboot+TCP监听服务器搭建过程图解

    这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
  • Spring Boot项目@RestController使用重定向redirect方式

    这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • springboot中使用@Transactional注解事物不生效的坑

    这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • 详解SpringBoot之访问静态资源(webapp...)

    这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14
  • SpringBoot接口接收json参数解析

    这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
  • springboot多模块包扫描问题的解决方法

    这篇文章主要介绍了springboot多模块包扫描问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解

    这篇文章主要介绍了Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-18
  • Springboot实现多线程注入bean的工具类操作

    这篇文章主要介绍了Springboot实现多线程注入bean的工具类操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-27
  • Springboot+MDC+traceId日志中打印唯一traceId

    本文主要介绍了Springboot+MDC+traceId日志中打印唯一traceId,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-10-17
  • SpringBoot部署到Linux读取resources下的文件及遇到的坑

    本文主要给大家介绍SpringBoot部署到Linux读取resources下的文件,在平时业务开发过程中,很多朋友在获取到文件内容乱码或者文件读取不到的问题,今天给大家分享小编遇到的坑及处理方案,感兴趣的朋友跟随小编一起看看吧...2021-06-21
  • 关于springboot中nacos动态路由的配置

    这篇文章主要介绍了springboot中nacos动态路由的配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-11
  • 解决Springboot整合shiro时静态资源被拦截的问题

    这篇文章主要介绍了解决Springboot整合shiro时静态资源被拦截的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-26
  • SpringBoot高版本修改为低版本时测试类报错的解决方案

    这篇文章主要介绍了SpringBoot高版本修改为低版本时测试类报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-18
  • springboot配置多数据源后mybatis拦截器失效的解决

    这篇文章主要介绍了springboot配置多数据源后mybatis拦截器失效的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-23