`
2010_315
  • 浏览: 10882 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

30秒 list 转化成 excel(STRUTS2+HIBERNATE+POI)

阅读更多
response.setContentType("application/vnd.ms-excel");
String fileName = "统计列表.xls";
response.setHeader("Content-Disposition", "attachment; filename="
+ java.net.URLEncoder.encode(fileName, "UTF-8"));

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
HSSFCellStyle style = wb.createCellStyle();
HSSFCellStyle style2 = wb.createCellStyle();
HSSFCellStyle style3 = wb.createCellStyle();
HSSFFont font = wb.createFont();
HSSFFont font2 = wb.createFont();

// 目录栏样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// set cell border
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setWrapText(true);

// 标头样式
style2.setFillBackgroundColor(HSSFColor.GREEN.index);
// 设定此样式的的背景颜色填充
style2.setFillPattern(HSSFCellStyle.FINE_DOTS);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// set cell border
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);

// 主体内容样式
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// set cell border
style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
style3.setBorderRight(HSSFCellStyle.BORDER_THIN);

font.setColor(HSSFFont.COLOR_NORMAL);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font2.setColor(HSSFFont.COLOR_RED);
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font2.setFontHeightInPoints((short) 15);
style.setFont(font);
style2.setFont(font2);

sheet = wb.createSheet();
sheet.setColumnWidth((short)0,(short)3000);
sheet.setColumnWidth((short)1,(short)5000);
sheet.setColumnWidth((short)2,(short)34000);
sheet.setColumnWidth((short)3,(short)5000);
sheet.setColumnWidth((short)4,(short)3000);

wb.setSheetName(0, "汇总", HSSFCell.ENCODING_UTF_16);
sheet.setDefaultColumnWidth((short) 12);

// 第一行列表头
row = sheet.createRow(0);

cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("求助信息列表");
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 11));
cell.setCellStyle(style2);

row = sheet.createRow(1);

cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号");
cell.setCellStyle(style);

cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("电话");
cell.setCellStyle(style);

cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("内容");
cell.setCellStyle(style);

cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("发布时间");
cell.setCellStyle(style);

cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("状态");
cell.setCellStyle(style);

cell = row.createCell((short) 5);


Mo mm = null;
int i = 0;
for ( ; i < moList.size(); i++) {
mm=(Mo) moList.get(i);
row = sheet.createRow(i + 2);

cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mm.getId());//会员号
cell.setCellStyle(style3);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mm.getMobile()); //手机号
cell.setCellStyle(style3);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(mm.getMessage());
cell.setCellStyle(style3);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(mm.getCreatetime()); //性别
cell.setCellStyle(style3);
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if(("1").equals(mm.getStatus())){
cell.setCellValue("新发布");
}
else if(("1").equals(mm.getStatus())){
cell.setCellValue("待处理");
}
else if(("2").equals(mm.getStatus())){
cell.setCellValue("已核实");
}
else if(("3").equals(mm.getStatus())){
cell.setCellValue("无法核实");
}
else if(("4").equals(mm.getStatus())){
cell.setCellValue("删除");
}
else if(("5").equals(mm.getStatus())){
cell.setCellValue("已解决");
}
else if(mm.getStatus()==null){
cell.setCellValue("新发布");
}

cell.setCellStyle(style3);
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);

}
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
log.info("SimpleJobStaticAction ends");
}
catch(Exception e)
{
log.info("CommodityAction出错");
e.printStackTrace();
}
return null;

1.注
页面编码:
<meta http-equiv="Content-Type" content="application/vnd.ms-excel; charset=utf-8" />
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics