系统吧 专业的电脑操作系统,支持重装各种原版系统xp/win7/win8/win10 这个系统怎么样?试试系统吧系统,体验办公系统畅快愉悦感。 各种电脑系统安装过程一样,神一般的畅快!
-->
当前位置:主页 > 电脑技术 >

大师为你详解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>

大师为你详解springboot实现根据Excel模板导出表格

二 :根据模板导出表格Excel

第一步:制作导出模板。

1、wps2019新建一个表格

2、制作需要的模板

大师为你详解springboot实现根据Excel模板导出表格

大师为你详解springboot实现根据Excel模板导出表格

第二步:编写实现类。

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();

}

}

}

大师为你详解springboot实现根据Excel模板导出表格

第三步:测试。

1、打开:http://localhost:8080/exportExcel

2、查看导出结果,结果如下所示,测试成功。

大师为你详解springboot实现根据Excel模板导出表格

大师为你详解springboot实现根据Excel模板导出表格

注意事项

开发环境jdk1.8 maven apache-maven-3.5.4 eclipse 2017

xp 更多>>
win7 更多>>
win8 更多>>
win10 更多>>
U盘 更多>>
电脑技术 更多>>
网站地图 | 豫ICP备2021035069号-4 | 友情链接qq:191064436
系统吧

版权所有 © 2012-2023 系统吧 免责声明:本站资源均收集于互联网,其著作权归原作者所有,如果有侵犯您权利的资源,请来信告知,我们将及时删除相应资源。