项目中用到的,Excel导出的功能,贴出来. :D
package com.chinasofti.basic.util;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* 导出为excel chinasofti.com Inc. Copyright (c) 2002-2012 All Rights Reserved.
*
* Creation Date : 2012-3-8 Last modification by Author: Administrator
*/
public class ExportExcelFile implements ExcelContext {
public static final String FILE_TYPE = ".xls";
/**
* 导出的文件名称
*/
private String fileName;
/**
*
* @param headers
* 表头
* @param dataList
* 数据
* @param response
* response
* @param request
* request
*/
@SuppressWarnings("static-access")
public void export(List<String> headers,
List<Map<String, ValueType>> dataList,
HttpServletResponse response, HttpServletRequest request) {
String file_name = this.getFileName();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, file_name);
// sheet.setc
// 设置表头为黑体
HSSFFont titleFont = workbook.createFont();
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFont(titleFont);
// 居右
HSSFCellStyle cellStyle_right = workbook.createCellStyle();
cellStyle_right.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
// 居左
HSSFCellStyle cellStyle_left = workbook.createCellStyle();
cellStyle_left.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// 居中
HSSFCellStyle cellStyle_center = workbook.createCellStyle();
cellStyle_center.setAlignment(HSSFCellStyle.ALIGN_CENTER);
int row = 0;
int columnHead = 0;
// 表头
{
HSSFRow rowCell = sheet.createRow(row);
rowCell.setHeight((short) 400);
// 序号
HSSFCell cell = rowCell.createCell(columnHead);
cell.setCellValue(ExcelContext.CELL_NUM_NAME);
cell.setCellStyle(titleStyle);
// cell.setCellStyle(cellStyle_left);
sheet.setColumnWidth(columnHead,
(short) (Integer.valueOf(10) * 256));
columnHead++;
for (String header : headers) {
cell = rowCell.createCell(columnHead);
cell.setCellValue(header);
cell.setCellStyle(titleStyle);
sheet.setColumnWidth(columnHead,
(short) (Integer.valueOf(25) * 256));// 设置列宽,20个字符宽
columnHead++;
}
row++;
}
// 数据
for (Map<String, ValueType> elmment : dataList) {
HSSFRow rowDataCell = sheet.createRow(row);
// 设置序号列
HSSFCell numCell = rowDataCell.createCell(0);
numCell.setCellValue(row);
numCell.setCellStyle(cellStyle_left);
int size = elmment.size();
// 读取明细数据
for (int i = 1; i < size + 1; i++) {
HSSFCell dataCell = rowDataCell.createCell(i);
ValueType property = elmment.get(String.valueOf(i));
if (this.TEXT_TYPE.equals(property.getType())) {
// dataCell.setCellType(HSSFCellStyle.ALIGN_LEFT);
} else if (this.NUMBER_TYPE.equals(property.getType())) {
dataCell.setCellStyle(cellStyle_right);
}
dataCell.setCellValue(property.getValue());
}
row++;
}
// 文件名称格式
DateFormat format = new SimpleDateFormat("yyyyMMdd");
String strNowDate = format.format(new Date());
file_name = file_name + "_" + strNowDate + FILE_TYPE;
response.setCharacterEncoding("gb2312");
// 设置response内容的类型
response.setContentType("application/x-download");
try {
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(file_name, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
OutputStream os;
try {
os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getFileName() {
if (fileName.endsWith(FILE_TYPE)) {
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
项目用调用的方式:
ExportExcelFile exportFile = new ExportExcelFile();
// 设置EXCEL表头
exportFile.setFileName(ExcelContext.CONFIG_FILE_NAME);
exportFile.export(headers,dataList,response(), request());
package com.chinasofti.basic.util;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* 导出为excel chinasofti.com Inc. Copyright (c) 2002-2012 All Rights Reserved.
*
* Creation Date : 2012-3-8 Last modification by Author: Administrator
*/
public class ExportExcelFile implements ExcelContext {
public static final String FILE_TYPE = ".xls";
/**
* 导出的文件名称
*/
private String fileName;
/**
*
* @param headers
* 表头
* @param dataList
* 数据
* @param response
* response
* @param request
* request
*/
@SuppressWarnings("static-access")
public void export(List<String> headers,
List<Map<String, ValueType>> dataList,
HttpServletResponse response, HttpServletRequest request) {
String file_name = this.getFileName();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, file_name);
// sheet.setc
// 设置表头为黑体
HSSFFont titleFont = workbook.createFont();
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFont(titleFont);
// 居右
HSSFCellStyle cellStyle_right = workbook.createCellStyle();
cellStyle_right.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
// 居左
HSSFCellStyle cellStyle_left = workbook.createCellStyle();
cellStyle_left.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// 居中
HSSFCellStyle cellStyle_center = workbook.createCellStyle();
cellStyle_center.setAlignment(HSSFCellStyle.ALIGN_CENTER);
int row = 0;
int columnHead = 0;
// 表头
{
HSSFRow rowCell = sheet.createRow(row);
rowCell.setHeight((short) 400);
// 序号
HSSFCell cell = rowCell.createCell(columnHead);
cell.setCellValue(ExcelContext.CELL_NUM_NAME);
cell.setCellStyle(titleStyle);
// cell.setCellStyle(cellStyle_left);
sheet.setColumnWidth(columnHead,
(short) (Integer.valueOf(10) * 256));
columnHead++;
for (String header : headers) {
cell = rowCell.createCell(columnHead);
cell.setCellValue(header);
cell.setCellStyle(titleStyle);
sheet.setColumnWidth(columnHead,
(short) (Integer.valueOf(25) * 256));// 设置列宽,20个字符宽
columnHead++;
}
row++;
}
// 数据
for (Map<String, ValueType> elmment : dataList) {
HSSFRow rowDataCell = sheet.createRow(row);
// 设置序号列
HSSFCell numCell = rowDataCell.createCell(0);
numCell.setCellValue(row);
numCell.setCellStyle(cellStyle_left);
int size = elmment.size();
// 读取明细数据
for (int i = 1; i < size + 1; i++) {
HSSFCell dataCell = rowDataCell.createCell(i);
ValueType property = elmment.get(String.valueOf(i));
if (this.TEXT_TYPE.equals(property.getType())) {
// dataCell.setCellType(HSSFCellStyle.ALIGN_LEFT);
} else if (this.NUMBER_TYPE.equals(property.getType())) {
dataCell.setCellStyle(cellStyle_right);
}
dataCell.setCellValue(property.getValue());
}
row++;
}
// 文件名称格式
DateFormat format = new SimpleDateFormat("yyyyMMdd");
String strNowDate = format.format(new Date());
file_name = file_name + "_" + strNowDate + FILE_TYPE;
response.setCharacterEncoding("gb2312");
// 设置response内容的类型
response.setContentType("application/x-download");
try {
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(file_name, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
OutputStream os;
try {
os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getFileName() {
if (fileName.endsWith(FILE_TYPE)) {
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
项目用调用的方式:
ExportExcelFile exportFile = new ExportExcelFile();
// 设置EXCEL表头
exportFile.setFileName(ExcelContext.CONFIG_FILE_NAME);
exportFile.export(headers,dataList,response(), request());