SpringBoot使用POI进行Excel下载

作者:兮川 时间:2022-06-14 13:16:13 

本文实例为大家分享了SpringBoot使用POI进行Excel下载的具体代码,供大家参考,具体内容如下

使用poi处理Excel特别方便,此处将处理Excel的代码分享出来。

1.maven引用


<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>

2.service逻辑代码


/**
* 获取下载模版
*/
public void salaryTemplate(HttpServletResponse response)throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook();
exportExcel(workbook);
response.setHeader("Content-type","application/vnd.ms-excel");

// 解决导出文件名中文乱码
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+new String("工资模版".getBytes("UTF-8"),"ISO-8859-1")+".xls");
workbook.write(response.getOutputStream());
}

//导入为模版
private void exportExcel(HSSFWorkbook workbook) throws Exception {
//创建创建sheet
HSSFSheet sheet = workbook.createSheet("工资");

//创建单元格样式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

//设置首行标题标题
HSSFRow headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellStyle(cellStyle);
headerRow.createCell(0).setCellValue("工号");
headerRow.createCell(1).setCellStyle(cellStyle);
headerRow.createCell(1).setCellValue("姓名");
headerRow.createCell(2).setCellStyle(cellStyle);
headerRow.createCell(2).setCellValue("年龄");

//创建三行数据
HSSFRow row;
for (int i = 0; i <4; i++) {
row = sheet.createRow(i + 1);
row.createCell(0).setCellStyle(cellStyle);
row.createCell(0).setCellValue(i);
row.createCell(1).setCellStyle(cellStyle);
row.createCell(1).setCellValue("张三");
row.createCell(2).setCellStyle(cellStyle);
row.createCell(2).setCellValue(19);
}
}

3.controller


@GetMapping("/salary/template")
public void salaryTemplate(HttpServletResponse response)throws Exception{
salaryService.salaryTemplate(response);
}

请求这个接口,下载下来就是Excel文件。写的比较简单,不过看代码基本就能看懂。

来源:https://blog.csdn.net/zc_ad/article/details/85242556

标签:SpringBoot,POI,下载
0
投稿

猜你喜欢

  • C#编程自学之数据类型和变量一

    2023-07-30 02:45:49
  • Spring AOP在web应用中的使用方法实例

    2023-12-24 12:20:00
  • MVVM和MVVMLight框架介绍及在项目中的使用详解

    2021-07-29 06:59:10
  • 浅谈Java list.remove( )方法需要注意的两个坑

    2023-02-01 06:08:44
  • 使用mybatis-plus-generator进行代码自动生成的方法

    2021-09-03 00:50:18
  • UGUI绘制多点连续的平滑曲线

    2022-01-16 06:22:45
  • c#实现一个超实用的证件照换底色小工具(附源码)

    2023-05-20 08:32:17
  • Mybatis基于注解形式的sql语句生成实例代码

    2023-03-07 03:48:11
  • SpringBoot整合dataworks的实现过程

    2023-11-29 12:13:09
  • Netty分布式获取异线程释放对象源码剖析

    2021-12-28 09:45:42
  • 详解C#读写Excel的几种方法

    2022-10-23 14:23:28
  • Spring实战之使用ClassPathResource加载xml资源示例

    2023-11-28 23:00:30
  • java并发编程之深入理解Synchronized的使用

    2023-10-11 09:21:13
  • android仿微信通讯录搜索示例(匹配拼音,字母,索引位置)

    2023-02-22 16:18:35
  • Java微信公众平台开发(13) 微信JSSDK中Config配置

    2022-12-29 15:58:24
  • 详解SpringBoot如何实现统一后端返回格式

    2022-11-27 05:26:24
  • Idea配置超详细图文教程(2020.2版本)

    2023-03-13 21:41:04
  • Android实现抽奖转盘实例代码

    2021-08-22 20:03:35
  • SpringBoot 整合 ElasticSearch操作各种高级查询搜索

    2023-03-25 17:12:40
  • C#隐藏控制台键盘输入的方法

    2022-04-29 21:11:06
  • asp之家 软件编程 m.aspxhome.com