Struts2 poi導出excel
最近實習的項目中做了個導出,記錄一下方便以后使用:
Action:
private InputStream excelFile;
private String downloadFileName;
Setter&Getter
public String getDownloadFileName() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd ");
String downloadFileName = (sf.format(new Date()).toString())
+ "項目信息.xls";
try {
downloadFileName = new String(downloadFileName.getBytes(),
"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public InputStream getExcelFile() {
return excelFile;
}
public void setExcelFile(InputStream excelFile) {
this.excelFile = excelFile;
}
關鍵代碼:
// 導出
public String export() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
List<FrontProjectList> dataList = projectApplyBaseService
.query_ProjectApply3(pqc, 0, projectApplyBaseService
.count_queryTotalProject_consumption(pqc) + 1);
HSSFWorkbook workbook = exportExcel(dataList);
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
return "excel";
}
public HSSFWorkbook exportExcel(List dataList) throws Exception {
HSSFWorkbook workbook = null;
try {
// 這里的數據即時你要從后台取得的數據
// 創建工作簿實例
workbook = new HSSFWorkbook();
// 創建工作表實例
HSSFSheet sheet = workbook.createSheet("TscExcel");
// 設置列寬
this.setSheetColumnWidth(sheet);
// 獲取樣式
HSSFCellStyle style = this.createTitleStyle(workbook);
//
if (dataList != null && dataList.size() > 0) {
// 創建第一行標題,標題名字的本地信息通過resources從資源文件中獲取
HSSFRow row = sheet.createRow((short) 0);// 建立新行
this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING, "序號");
this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING,
"項目名稱");
this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING,
"項目類別");
this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING,
"建設單位");
this.createCell(row, 4, style, HSSFCell.CELL_TYPE_STRING,
"牽頭責任單位");
this.createCell(row, 5, style, HSSFCell.CELL_TYPE_STRING,
"建設起始年");
this.createCell(row, 6, style, HSSFCell.CELL_TYPE_STRING,
"總投資(萬元)");
this.createCell(row, 7, style, HSSFCell.CELL_TYPE_STRING,
"申報時間");
// 給excel填充數據
for (int i = 0; i < dataList.size(); i++) {
// 將dataList里面的數據取出來,假設這里取出來的是Model,也就是某個javaBean的意思啦
FrontProjectList model = (FrontProjectList) dataList.get(i);
HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行
this.createCell(row1, 0, style, HSSFCell.CELL_TYPE_STRING,
i + 1);
if (model.getXmmc() != null)
this.createCell(row1, 1, style,
HSSFCell.CELL_TYPE_STRING, model.getXmmc());
if (model.getXmlb() != null)
this.createCell(row1, 2, style,
HSSFCell.CELL_TYPE_STRING, model.getXmlb());
if (model.getXmdw() != null)
this.createCell(row1, 3, style,
HSSFCell.CELL_TYPE_STRING, model.getXmdw());
if (model.getZrbm() != null)
this.createCell(row1, 4, style,
HSSFCell.CELL_TYPE_STRING, model.getZrbm());
if (model.getJsqsn() != null)
this.createCell(row1, 5, style,
HSSFCell.CELL_TYPE_STRING, model.getJsqsn());
if (model.getZtz() != null)
this.createCell(row1, 6, style,
HSSFCell.CELL_TYPE_STRING, model.getZtz());
if (model.getSbsj() != null)
this.createCell(row1, 7, style,
HSSFCell.CELL_TYPE_STRING, model.getSbsj());
}
} else {
this.createCell(sheet.createRow(0), 0, style,
HSSFCell.CELL_TYPE_STRING, "查無資料");
}
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
private void setSheetColumnWidth(HSSFSheet sheet) {
// 根據你數據里面的記錄有多少列,就設置多少列
sheet.setColumnWidth(0, 3000);
sheet.setColumnWidth(1, 8000);
sheet.setColumnWidth(2, 3000);
sheet.setColumnWidth(3, 8000);
sheet.setColumnWidth(4, 8000);
sheet.setColumnWidth(5, 5000);
sheet.setColumnWidth(6, 5000);
sheet.setColumnWidth(7, 5000);
}
// 設置excel的title樣式
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 200);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
return style;
}
// 創建Excel單元格
private void createCell(HSSFRow row, int column, HSSFCellStyle style,
int cellType, Object value) {
HSSFCell cell = row.createCell(column);
if (style != null) {
cell.setCellStyle(style);
}
switch (cellType) {
case HSSFCell.CELL_TYPE_BLANK: {
}
break;
case HSSFCell.CELL_TYPE_STRING: {
cell.setCellValue(value.toString());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC: {
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(value.toString()));
}
break;
default:
break;
}
}
Struts.xml
<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>