参考别的作者资料
generateExcel 为不可配置地址的导出excel的工具类;
generateExcel3 为可配置地址的导出excel的工具类,只需传入默认文件名即可。
package com.sgcc.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExportExcel {
/**
* 生成excel的工具类
*
* @param excelHeader
* 生成文件的表头
* @param dataList
* 从数据库中查出来的数据
* @param address
* 需要保存到的地址
* @return
*/
public static boolean generateExcel(String[] excelHeader,
List<Object[]> dataList, String address) {
boolean bool = false;
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("sheet");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 设置表头的名字
HSSFCell cell;
for (int i = 0; i < excelHeader.length; i++) {
cell = row.createCell((short) i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style);
}
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
for (int i = 0; i < dataList.size(); i++) {
row = sheet.createRow((int) i + 1);
Object[] obj = dataList.get(i);
// 创建单元格,并设置值
for (int j = 0; j < obj.length; j++) {
row.createCell((short) j).setCellValue(String.valueOf(obj[j]));
}
}
// 第六步,将文件存到指定位置
try {
// 获取当前系统时间
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String nowDate = df.format(new Date());
address += nowDate+".xls";
FileOutputStream fout = new FileOutputStream(address);
//new FileOutputStream(new File("dir + fileName"));
wb.write(fout);
fout.close();
bool = true;
return bool;
} catch (Exception e) {
e.printStackTrace();
return bool;
}
}
public static boolean generateExcel3(String[] excelHeader,
List<Object[]> dataList, String address,HttpServletRequest request, HttpServletResponse res) throws ServletException,
IOException {
boolean bool = false;
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("sheet");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 设置表头的名字
HSSFCell cell;
for (int i = 0; i < excelHeader.length; i++) {
cell = row.createCell((short) i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style);
}
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
for (int i = 0; i < dataList.size(); i++) {
row = sheet.createRow((int) i + 1);
Object[] obj = dataList.get(i);
// 创建单元格,并设置值
for (int j = 0; j < obj.length; j++) {
row.createCell((short) j).setCellValue(String.valueOf(obj[j]));
}
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
res.reset();
res.setContentType("application/vnd.ms-excel;charset=utf-8");
res.setHeader("Content-Disposition", "attachment;filename=" + new String((address + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = res.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}