简单介绍一下java中的Excel文件导出功能(基于HttpServletResponse实现下载)

首先,引入需要依赖的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 writhttp://www.cppcns.come(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.isEhttp://www.cppcns.commpty(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();
}
}
}

有了这个工具类就可以实现Ewww.cppcns.comxcel导出了,代码不难,这里就不多解释了。

在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 = "/thttp://www.cppcns.comestExcelOutPut")
@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");
}
}

浏览器输入链接:

即可自动下载测试数据组成的Excel:

java 导出excel分组 java excel 导出_java

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。