-
大师为你详解springboot实现根据Excel模板导出表格
- 作者:孙紫潇 分类:电脑技术 发布时间:2017-09-19 18:04:36
电脑现已成为我们工作、生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到springboot实现根据Excel模板导出表格的问题,如果我们遇到了springboot实现根据Excel模板导出表格的情况,该怎么处理怎么才能解决springboot实现根据Excel模板导出表格带来的困扰呢,对于这样的问题其实我们只需要电脑eclipse或者intellij IDEA第一步:创建springboot项目。1、使用intellij IDEA创建springboot项目1.1、 打开创建页面 选择File-new-project..1.2、选择创建的项目为spring ini...
电脑现已成为我们工作、生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到springboot实现根据Excel模板导出表格的问题,如果我们遇到了springboot实现根据Excel模板导出表格的情况,该怎么处理怎么才能解决springboot实现根据Excel模板导出表格带来的困扰呢,对于这样的问题其实我们只需要电脑eclipse或者intellij IDEA第一步:创建springboot项目。1、使用intellij IDEA创建springboot项目1.1、 打开创建页面 选择File-new-project..1.2、选择创建的项目为spring initializr 进入springboot项目创建步骤(也可以选择类型java,创建一个普通java项目)1.3、输入项目名字,选择依赖web(根这样就解决了这样的问题,接下来给大家带来springboot实现根据Excel模板导出表格的详细操作步骤。
工具/原料
电脑
eclipse或者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文件引入Excel表格的POI导出导入依赖包。
<!--引入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、wps2019新建一个表格
2、制作需要的模板
第二步:编写实现类。
1、编写控制器,代码如下所示
import java.io.FileInputStream;
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(敏感词)ExportController{
@GetMapping("/exportExcel")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
try {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("E:/file/Excel导出模板.xls"));
// 根据页面index 获取sheet页
HSSFSheet sheet = wb.getSheet("Sheet1");
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/exportExcel
2、查看导出结果,结果如下所示,测试成功。
注意事项
开发环境jdk1.8 maven apache-maven-3.5.4 eclipse 2017
猜您喜欢
- 麒麟9000S猛兽出笼:单核跑分测试揭秘..2024-03-19
- 如何打开神秘的LRC文件?让我为你揭秘..2024-03-13
- 快速安装高效插件,让你成为百家号大..2024-03-13
- 如何为你的电脑硬盘上把锁,防止重要..2024-02-29
- 迈向成功! 详解win11系统添加打印机的..2024-02-22
- 探索win10专业版与企业版:究竟谁将成..2023-12-31
相关推荐
- 腾讯视频扫码登录在哪里 腾讯视频扫..2023-10-28
- 抖音里都在用的装逼小方法(超简单)..2017-10-01
- shift delete删除的文件怎么恢复_恢复S..2021-06-19
- Win11网络需要操作连接wifi使用怎么弄..2022-11-14
- 迅雷下载慢怎么办?下载慢,一招搞定..2017-07-26
- 电脑轻松滚动截长图,一招在手,截屏..2023-12-17