springboot实现无模板Excel表格导出

2025-07-21 05:59:41

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领跑者。

工具/原料

电脑

eclipse或者intellij IDEA

一 :创建springboot项目

1、第一步:使用intellij IDEA创建springboot项目。1、使用intellij IDEA创建springboot项目1.1、 打开创建页面 选择File-new-project..1.2、选择创建的项目为spring initializr 进入springboot项目创建步骤(也可以选择类型java,创建一个普通java项目)1.3、输入项目名字,选择依赖web(根据项目需求选择,此次需要),选择存放目录-完成(Finish)2、使用eclipse创建springboot项目

springboot实现无模板Excel表格导出
springboot实现无模板Excel表格导出
springboot实现无模板Excel表格导出
springboot实现无模板Excel表格导出

2、第二步:pom文件引入POI导出导入Excel表格的依赖包。<!--引入poi --争犸禀淫> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.0</version> </dependency>

springboot实现无模板Excel表格导出

二 :无模板导出Excel表格

1、第一步:编写实现代码。1、代码实现如下所示:import java.io.OutputStream;import javax.serv造婷用痃let.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class Excel(敏感词)ExportController2 { @GetMapping("/exportExcel2") public void exportExcel(HttpServletRequest request, HttpServletResponse response) { try { HSSFWorkbook wb = new HSSFWorkbook(); // 根据页面index 获取sheet页 HSSFSheet sheet = wb.createSheet("人员基本信息"); for (int i = 0; i < 100; i++) { // 创建HSSFRow对象 HSSFRow row = sheet.createRow(i + 1); // 创建HSSFCell对象 设置单元格的值 row.createCell(0).setCellValue("张三" + i); row.createCell(1).setCellValue(i); row.createCell(2).setCellValue("男" + i); row.createCell(3).setCellValue("科研" + i); } // 输出Excel文件 OutputStream output = response.getOutputStream(); response.reset(); // 设置文件头 response.setHeader("Content-Disposition", "attchement;filename=" + new String("人员信息.xls".getBytes("gb2312"), "ISO8859-1")); response.setContentType("application/msexcel"); wb.write(output); wb.close(); } catch (Exception e) { e.printStackTrace(); } }}

springboot实现无模板Excel表格导出
springboot实现无模板Excel表格导出

2、第二步:测试。1、打开:http://localhost:8080/exportExcel22、查看导出结果

springboot实现无模板Excel表格导出

四:总结

1、模板导出和无模板导出对比。1、模板导出好处是方便设置复杂的标题头,不需要使用编程知识(只要会Excel就行)可以轻易设置各种复杂的样式,格式。但是缺点也很明显模板一样是针对某个页面的不能通用,大量使用会导致资源浪费。2、无模板是使用最多的情况,大部分业务的导出标题单一、格式固定。一般在使用中架构师会将poi导出框架封装成固定的导出格式,只需要传标题名称、字段和数据集合(一般是list)就可以完成导出。缺点:需要你对poi导出框架有一定的使用基础。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢