-
细说springboot实现无模板Excel表格导出
- 作者:朱芸兮 分类:电脑技术 发布时间:2017-09-19 18:04:36
电脑现已成为我们工作、生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到springboot实现无模板Excel表格导出的问题,如果我们遇到了springboot实现无模板Excel表格导出的情况,该怎么处理怎么才能解决springboot实现无模板Excel表格导出带来的困扰呢,对于这样的问题其实我们只需要电脑eclipse或者intellij IDEA第一步:使用intellij IDEA创建springboot项目。1、使用intellij IDEA创建springboot项目1.1、 打开创建页面 选择File-new-project..1.2、选择创建的项目为...
电脑现已成为我们工作、生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到springboot实现无模板Excel表格导出的问题,如果我们遇到了springboot实现无模板Excel表格导出的情况,该怎么处理怎么才能解决springboot实现无模板Excel表格导出带来的困扰呢,对于这样的问题其实我们只需要电脑eclipse或者intellij IDEA第一步:使用intellij IDEA创建springboot项目。1、使用intellij IDEA创建springboot项目1.1、 打开创建页面 选择File-new-project..1.2、选择创建的项目为spring initializr 进入springboot项目创建步骤(也可以选择类型java,创建一个普通java项目)1.3、输这样就解决了这样的问题,接下来给大家带来springboot实现无模板Excel表格导出的详细操作步骤。
工具/原料
电脑
eclipse或者intellij IDEA
一 :创建springboot项目
第一步:使用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项目
第二步: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>
二 :无模板导出Excel表格
第一步:编写实现代码。
1、代码实现如下所示:
import java.io.OutputStream;
import javax.servlet.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;
@RestController
public 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();
}
}
}
第二步:测试。
1、打开:http://localhost:8080/exportExcel2
2、查看导出结果
四:总结
模板导出和无模板导出对比。
1、模板导出好处是方便设置复杂的标题头,不需要使用编程知识(只要会Excel就行)可以轻易设置各种复杂的样式,格式。但是缺点也很明显模板一样是针对某个页面的不能通用,大量使用会导致资源浪费。
2、无模板是使用最多的情况,大部分业务的导出标题单一、格式固定。一般在使用中架构师会将poi导出框架封装成固定的导出格式,只需要传标题名称、字段和数据集合(一般是list)就可以完成导出。缺点:需要你对poi导出框架有一定的使用基础。
注意事项
开发环境jdk1.8 maven apache-maven-3.5.4 eclipse 2017
猜您喜欢
- 掌握这个技巧,轻松实现Windows电脑截..2024-04-05
- 掌握这个技巧,轻松实现Windows电脑微..2024-04-04
- 掌握这个快捷键,轻松实现电脑窗口截..2024-03-28
- 掌握这个秘诀,轻松实现电脑定时关机..2024-03-28
- 掌握这几种远程连接方法,轻松实现..2024-03-25
- 华为手机投屏电脑,轻松实现大屏操作..2024-03-23
相关推荐
- 如何查手机话费明细账,如何查手机话..2017-10-12
- Win7虚拟内存怎么设置最好 win7虚拟内存..2023-02-05
- 电脑系统城教你电脑怎么创建宽带连接..2021-07-27
- 微软Win11 6月累积更新补丁KB5014697(220..2022-11-15
- 怎样把照片设为电脑全屏桌面?壁纸,怎..2020-12-05
- tenda路由器远程web管理设置方法..2021-06-28