怎么在Java中实现导出Excel
本篇文章为大家展示了怎么在Java中实现导出Excel,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
在常熟等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、网站设计 网站设计制作按需策划设计,公司网站建设,企业网站建设,高端网站设计,成都全网营销,外贸网站制作,常熟网站建设费用合理。
首先,引入需要依赖的jar包:
org.apache.poi poi 3.14 org.apache.poi poi-ooxml 3.14
编写一个工具类:
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> 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
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; @Controller @RequestMapping(value = "/index") public class HelloCtrl extends ExportAbstractUtil{ @RequestMapping(value = "/testExcelOutPut") @ResponseBody public void testExcelOutPut(HttpServletResponse response){ //拼接数据start List> lists = new ArrayList
>(); String rows[] = {"year","month","day"}; List
rowsTitle = Arrays.asList(rows); lists.add(rowsTitle); for(int i = 0; i<=9;i++){ String [] rowss = {"1","2","3"}; List rowssList = Arrays.asList(rowss); lists.add(rowssList); } //拼接数据end write(response,lists,"导出Excel.xls"); } }
上述内容就是怎么在Java中实现导出Excel,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
分享题目:怎么在Java中实现导出Excel
新闻来源:http://scyanting.com/article/jdpihd.html