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

细说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项目

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

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

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

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

第二步: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、代码实现如下所示:

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

}

}

}

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

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

第二步:测试。

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

2、查看导出结果

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

四:总结

模板导出和无模板导出对比。

1、模板导出好处是方便设置复杂的标题头,不需要使用编程知识(只要会Excel就行)可以轻易设置各种复杂的样式,格式。但是缺点也很明显模板一样是针对某个页面的不能通用,大量使用会导致资源浪费。

2、无模板是使用最多的情况,大部分业务的导出标题单一、格式固定。一般在使用中架构师会将poi导出框架封装成固定的导出格式,只需要传标题名称、字段和数据集合(一般是list)就可以完成导出。缺点:需要你对poi导出框架有一定的使用基础。

注意事项

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

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

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