没啥可说的,直接上代码,仅供参考! controller类:
/**
* 历史数据导出
* @param pzwh
* @param response
*/
@RequestMapping(value = "/outExcel")
@ResponseBody
public void outExcel(String pzwh,HttpServletResponse response) {
Page<GuoHealthFoodRegisterHistory> page = new Page<GuoHealthFoodRegisterHistory>(1, ConstUtil.EXCEL_PAGESIZE);
guoHealthFoodRegisterHistoryService.outExcel(page, pzwh,response);
}
service:
void outExcel(Page<GuoHealthFoodRegisterHistory> page, String pzwh, HttpServletResponse response);
serviceImpl:
@Override
public void outExcel(Page<GuoHealthFoodRegisterHistory> page, String pzwh, HttpServletResponse response) {
List<GuoHealthFoodRegisterHistory> list = this.baseMapper.getSpecialFoodList(page, pzwh);
response.reset();//重置响应头
String fileName = "注册历史.xls";
try{
fileName = java.net.URLEncoder.encode(fileName,"UTF-8");
}catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response.setContentType("application/x-download");//告知浏览器下载文件,而不是直接打开,浏览器默认为打开
response.addHeader("Content-Disposition" ,"attachment;filename="+fileName);
//excel名字,表头,关键字(get方法使用),实体,数据,HSSFWorkbooks
//ExcelUtil.getHSSFWorkbook("", ConstUtil.title, ConstUtil.key,SearchInfo.class,(List)map.get("rows"), null);
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
ExportExcel ex = new ExportExcel();
int pageNum = 60000; //每个sheet页存储60000条数据
int sheetNum = list.size()/pageNum+1; //有几个sheet页
int listNum = 0;
for(int n = 0;n<sheetNum;n++) {
//每个sheet页里的最大值,第一个sheet页60000,第二个120000
long k = pageNum * (n + 1);
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("注册历史数据" + n);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
int j = 0;//多少行
//创建行
HSSFRow row = sheet.createRow(j);
//创建单元格
HSSFCell cell = null;
// 第四步,创建单元格内容样式
HSSFCellStyle contentstyle = ex.setCellStyle(wb);
//创建表头样式
String title[] = ConstUtil.guo_title;
ex.setHeaderStyle(wb,cell,sheet,row,title);
for(int m =listNum;m<k;m++){
row = sheet.createRow(++j);
cell = row.createCell(0);
cell.setCellValue(list.get(m).getCpbh());
cell.setCellStyle(contentstyle);
cell = row.createCell(1);
cell.setCellValue(list.get(m).getPzwh());
cell.setCellStyle(contentstyle);
cell = row.createCell(2);
cell.setCellValue(list.get(m).getCpmc());
cell.setCellStyle(contentstyle);
cell = row.createCell(3);
cell.setCellValue(list.get(m).getSqrmcZw());
cell.setCellStyle(contentstyle);
cell = row.createCell(4);
if(("1").equals(list.get(m).getYxqz())){
cell.setCellValue("国产");
}else{
cell.setCellValue("进口");
}
cell.setCellStyle(contentstyle);
if(m==k-1){ //
listNum = pageNum*(n+1);
break;
}
if(m == list.size()-1){
k=list.size();
}
}
}
ex.excelClose(wb,response);
}
ExcelPort类:在serviceImpl里面第四步创建单元格内容样式、创建表头样式、关闭流所需方法
package com.itown.corplib.inquiry.common;
import com.itown.corplib.common.utils.ConstUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
public class ExportExcel {
/*
* 设置单元格样式
* */
public HSSFCellStyle setCellStyle(HSSFWorkbook wb){
HSSFCellStyle contentstyle = wb.createCellStyle();
//边框样式
contentstyle.setBorderTop(BorderStyle.THIN);
contentstyle.setBorderBottom(BorderStyle.THIN);
contentstyle.setBorderLeft(BorderStyle.THIN);
contentstyle.setBorderRight(BorderStyle.THIN);
//边框颜色
contentstyle.setBottomBorderColor(IndexedColors.BLACK.index);
contentstyle.setTopBorderColor(IndexedColors.BLACK.index);
contentstyle.setLeftBorderColor(IndexedColors.BLACK.index);
contentstyle.setRightBorderColor(IndexedColors.BLACK.index);
return contentstyle;
}
/**
* 设置表头样式
* @param wb
* @param cell
* @param sheet
* @param row
* @param title
*/
public void setHeaderStyle(HSSFWorkbook wb,HSSFCell cell,HSSFSheet sheet,HSSFRow row,String title[]){
HSSFCellStyle headStyle = wb.createCellStyle();
//边框样式
headStyle.setBorderTop(BorderStyle.THIN);
headStyle.setBorderBottom(BorderStyle.THIN);
headStyle.setBorderLeft(BorderStyle.THIN);
headStyle.setBorderRight(BorderStyle.THIN);
//边框颜色
headStyle.setBottomBorderColor(IndexedColors.BLACK.index);
headStyle.setTopBorderColor(IndexedColors.BLACK.index);
headStyle.setLeftBorderColor(IndexedColors.BLACK.index);
headStyle.setRightBorderColor(IndexedColors.BLACK.index);
//设置居中
headStyle.setAlignment(HorizontalAlignment.CENTER);
//设置字体
HSSFFont font = wb.createFont();
//是否加粗
font.setBold(true);
//设置字号
font.setFontHeightInPoints((short)11);
headStyle.setFont(font);
//创建标题
for(int i = 0; i<title.length; i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(headStyle);
//设置列宽
sheet.setColumnWidth(i,(int)((25 + 0.72) * 256));
}
}
/**
* 关闭Excel
* @param wb
* @param response
*/
public void excelClose(HSSFWorkbook wb,HttpServletResponse response){
try{
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
备注:实体类和sql根据自己实际业务书写,这里就不展示了。
结果:针对于大数据量导出以多sheet页形式,每个sheet页存储60000条,测试导出65w条数据大概在36秒左右,导出5列,sql查询速度比较快,几乎不影响导出速度,如果sql查询速度慢会影响导出效率,导出的列数多也可能会影响导出效率。
第一次写这些,不足之处还请大家多多批评指正。