/**
*最近工作时,用到了poi导出Excel,抽时间整理了一下
*使用前需要下载apache poi.jar包
*/
package com.excel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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;
/**
* 2013-7-3
* ExcelUtil.java
* @version 1.0
* @param <T>
*/
public class ExcelUtil<T> {
private InputStream excelFile;
@SuppressWarnings("unused")
private String downloadFileName;
/** 自己的方法 */
public String myMethed() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
List<T> lists = new ArrayList<T>();
HSSFWorkbook workbook = exportExcel(lists, column().length, column());
workbook.write(output);
byte[] bates = output.toByteArray();
excelFile = new ByteArrayInputStream(bates);
output.flush();
output.close();
return "excel";
}
/** 显示的列名称 */
public String[] column() {
String[] columnName = {"column1", "column2"};
return columnName;
}
/**
* List<User> dataList 数据集合
* column 列数
* columnName 列名
* */
public HSSFWorkbook exportExcel(List<T> dataList, int column, String[] columnName) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook(); // 创建一个webbook
HSSFSheet sheet = wb.createSheet("sheet1"); // 在webbook中添加一个sheet
HSSFRow row = sheet.createRow((int) 0); // 在sheet中添加表头第0行
HSSFCellStyle style = createCellStyle(wb); // 创建单元格,获取样式
HSSFCell cell; // 列布局
for(int i=0; i < column; i++) {
cell = row.createCell((int) i); // 列数
cell.setCellValue(columnName[i]); // 列名
cell.setCellStyle(style); // 列样式
sheet.setColumnWidth(i, 3000); // 设置列宽
}
// 行布局
for(int i = 0; i < dataList.size(); i++) {
row = sheet.createRow((int) i + 1);
T o = (T) dataList.get(i);
row.createCell(0).setCellValue(o.getClass().getName()); // 获取对象属性值从第i个值开始
row.createCell(1).setCellValue(o.getClass().getName());
}
return wb;
}
/** 设置excel的title样式 */
private HSSFCellStyle createCellStyle(HSSFWorkbook wb) {
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 260);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
return style;
}
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
public String getDownloadFileName() throws Exception {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd ");
String downloadFileName = (sf.format(new Date()).toString()) + "项目信息.xls";
downloadFileName = new String(downloadFileName.getBytes(), "ISO8859-1");
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
}
// struts2 配置
<action name="export" class="com.excel.ExcelUtil">
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${downloadFileName}" </param>
<param name="bufferSize">1024</param>
<param name="inputName">excelFile</param>
</result>
</action>
jsp 访问路径:
<a href="<%=basePath%>export_myMethed">导出Excel</a>