工作中经常会有将后台数据以Excel导出的功能。
简单的方法有将response的contentType设置为application/vnd.ms-excel;
或在JSP页面直接设置成:
<%@page contentType="application/vnd.ms-excel; charset=GBK"%>
但,如何将数据写入到一个事先写好VBA和各种公式的Excel模版中,再响应给界面下载呢。
大致思路是,新建一个临时xls文件,获得xlt模版文件的输入流写入数据,再将这个输入流通过临时xls文件的输出流,将带有数据的模版生成这个临时xls文件,即最终要下载的Excel文件。
直接上代码。
File file = null;
HSSFSheet st = null;
HSSFWorkbook wb = null;
POIFSFileSystem fs = null;
FileOutputStream fos = null;
//建立临时文件,也是要下载的Excel文件。临时文件的路径为JDK针对不同操作系统定义的临时目录。
file = File.createTempFile("test", ".xls");
fs = new POIFSFileSystem(new FileInputStream(new File("Format.xlt")));//参数为Excel模版的输入流,可用其它方式。
wb = new HSSFWorkbook(fs);
st = wb.getSheetAt(0);
for(int row = 1; row < 100; row++){
for(int col = 0; col < 10; col++){
ExcelUtils.setValue(st, row, col, "test"+row+col);//循环写入数据
}
}
fos = new FileOutputStream(file);
try{
if (wb != null) {
wb.write(fos);
}
if (fos != null) {
fos.close();
}
} catch (NullPointerException ex){
}
//文件下载。
ExcelUtils.downFile(response, file, "application/vnd.ms-excel;charset=GBK");
ExcelUtils工具类的代码如下,其中包含了Excel数据读取的部分:
/**
* Excel工具类
*/
package *.*.*.*; import javax.servlet.http.HttpServletResponse; import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class ExcelUtils {
/**
* 从Excel文件得到二维数组,每个sheet的第一行为标题
*
* @param file Excel文件
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file) throws FileNotFoundException,
IOException {
return getData(file, 1);
} /**
* 从Excel文件得到二维数组
*
* @param file Excel文件
* @param ignoreRows 忽略的行数,通常为每个sheet的标题行数
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
ArrayList result = new ArrayList();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打开HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex); // 第一行为标题,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要设成这个,否则可能会出现乱码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0.####").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
}
if (columnIndex == 0 && value.trim().equals("")) {
break;
}
values[columnIndex] = StringUtils.rightTrim(value);
hasValue = true;
} if (hasValue) {
result.add(values);
}
}
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
} /**
* 根据contentType下载文件。
* @param response 响应
* @param File 要下载的文件源
* @param contentType
* @throws Exception
*/
public static void downFile(HttpServletResponse response, File file, String contentType) throws Exception {
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
String destFileName = file.getPath();
String shortFileName = FileUtils.getShortFileName(destFileName);
try{
shortFileName = java.net.URLEncoder.encode(shortFileName,"UTF-8");
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment;filename=" + shortFileName);
java.io.File filein = new java.io.File(destFileName);
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filein);
bis = new java.io.BufferedInputStream(fileInputStream);
bos = new java.io.BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
}catch(Exception e ){
e.printStackTrace();
throw e;
}finally{
if (bis != null) bis.close();
if (bos != null) bos.close();
}
} /**
* 设置Cell的值
*
* @param st
* @param rowIndex 行坐标
* @param columnIndex 列坐标
* @param value 值
*/
public static void setValue(HSSFSheet st, int rowIndex, int columnIndex,
String value) {
if (value == null) {
value = "";
}
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
row = st.createRow(rowIndex);
}
HSSFCell cell = row.getCell((short) columnIndex);
if (cell == null) {
cell = row.createCell((short) columnIndex);
}
//注意:一定要设成这个,否则可能会出现乱码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(value);
}
}
Excel工具jar包:http://files.cnblogs.com/hanmou/poi-3.0.1-FINAL-20070705.jar.zip