Java中实现简单的Excel导出
简单介绍一下Java中的Excel文件导出功能(基于HttpServletResponse实现下载)
首先,引入需要依赖的jar包:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>
编写一个工具类:
package exceloutput; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.UUID; /** * @author haozz * @date 2018/6/6 9:57 * @description excel导出抽象工具类 **/ public abstract class ExportAbstractUtil { public void write(HttpServletResponse response, Workbook workbook){ String fileName = UUID.randomUUID().toString()+".xls"; pwrite(response,workbook,fileName); } public void write(HttpServletResponse response,Workbook workbook,String fileName){ if(StringUtils.isEmpty(fileName)){ fileName = UUID.randomUUID().toString()+".xls"; } pwrite(response,workbook,fileName); } public void write(HttpServletResponse response, List<List<String>> lists,String fileName){ if(StringUtils.isEmpty(fileName)){ fileName = UUID.randomUUID().toString()+".xls"; } SXSSFWorkbook workbook = new SXSSFWorkbook(lists.size()); SXSSFSheet sheet = workbook.createSheet(fileName.substring(0,fileName.indexOf(".xls"))); Integer rowIndex = 0; Row row = null; Cell cell = null; for(List<String> rowData: lists ){ Integer columnIndex = 0; row = sheet.createRow(rowIndex++); for(String columnVal:rowData){ cell = row.createCell(columnIndex++); cell.setCellValue(columnVal); } } pwrite(response,workbook,fileName); } private void pwrite(HttpServletResponse response,Workbook workbook,String fileName){ response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); try { response.addHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("UTF-8"),"ISO8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); fileName= UUID.randomUUID().toString()+".xls"; response.addHeader("Content-Disposition", "attachment; filename="+fileName); } try { workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } }
有了这个工具类就可以实现Excel导出了,代码不难,这里就不多解释了。
在SpringBoot项目中编写一个导出Excel的Controller,并继承上面的ExportAbstractUtil,给出一个接口用作测试:
package com.csdn.myboot.controller; import com.csdn.myboot.utils.ExportAbstractUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author haozz * @date 2018/6/6 10:14 * @description **/ @Controller @RequestMapping(value = "/index") public class HelloCtrl extends ExportAbstractUtil{ @RequestMapping(value = "/testExcelOutPut") @ResponseBody public void testExcelOutPut(HttpServletResponse response){ //拼接数据start List<List<String>> lists = new ArrayList<List<String>>(); String rows[] = {"year","month","day"}; List<String> rowsTitle = Arrays.asList(rows); lists.add(rowsTitle); for(int i = 0; i<=9;i++){ String [] rowss = {"1","2","3"}; List<String> rowssList = Arrays.asList(rowss); lists.add(rowssList); } //拼接数据end write(response,lists,"导出Excel.xls"); } }
浏览器输入链接:
即可自动下载测试数据组成的Excel:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
相关文章
springboot aspect通过@annotation进行拦截的实例代码详解
这篇文章主要介绍了springboot aspect通过@annotation进行拦截的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-08-08mybatis如何使用Java8的日期LocalDate和LocalDateTime详解
这篇文章主要给大家介绍了关于mybatis如何使用Java8的日期LocalDate和LocalDateTime的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。2017-09-09Netty分布式ByteBuf中PooledByteBufAllocator剖析
这篇文章主要为大家介绍了Netty分布式ByteBuf剖析PooledByteBufAllocator简述,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-03-03
最新评论