public void exportXls(){
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
ServletOutputStream servletOutputStream;
try {
servletOutputStream = httpServletResponse.getOutputStream();
} catch (IOException e) {
procException(e, "获取导出文件HttpServletResponse异常");
return;
}
try {
httpServletResponse.setHeader("Content-disposition","attachment; filename=" + new String("测试".getBytes("gb2312"), "ISO8859-1") + ".xls");
} catch (UnsupportedEncodingException e) {
procException(e, "设置ResponseHeader异常,无法设置文件名");
return;
}
httpServletResponse.setContentType("application/vnd.ms-excel");
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet("信息统计");
// 设置列宽
sheet.setColumnWidth(0, 2000);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 8000);
sheet.setColumnWidth(3, 4000);
sheet.setColumnWidth(4, 4000);
sheet.setColumnWidth(5, 4000);
sheet.setColumnWidth(6, 4000);
sheet.setColumnWidth(7, 4000);
sheet.setColumnWidth(8, 4000);
sheet.setColumnWidth(9, 4000);
sheet.setColumnWidth(10, 4000);
// 生成一个样式(表头样式)
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont font = workbook.createFont();
// font.setColor(HSSFColor.VIOLET.index);
// font.setFontHeightInPoints((short) 10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到第一行标题的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
// 设置字体自动换行
style2.setWrapText(true);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到数据的样式
style2.setFont(font2);
HSSFRow row = sheet.createRow(0);
// 生成表头
String[] header = new String[]{"1","2","3","4","5","6","7","8","9","10","11"};
for (int i = 0; i < header.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(header[i]);
cell.setCellValue(text);
}
//这个地方是获取jsf的表格内容,要是用的别的前台框架,这个地方换成相对应的数据就好
List<Map<String, Object>> list = (List<Map<String, Object>>) getDataTable().getWrappedData();
int cicle = 1;
for (int i = 0; i < list.size(); i++) {
// 在表头下边 开始创建行并加入值
Map<String, Object> map = list.get(i);
row = sheet.createRow(cicle);
row.setRowStyle(style);
// 1
HSSFCell cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString(i + 1 + ""));
cell.setCellStyle(style2);
// 2
String busNum = map.get("busNum") == null ? "" : map.get("busNum").toString();
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString(busNum));
cell.setCellStyle(style2);
// 3
String areaName = map.get("areaName") == null ? "" : map.get("areaName").toString();
cell = row.createCell(2);
cell.setCellValue(new HSSFRichTextString(areaName));
cell.setCellStyle(style2);
//4
String name = map.get("name") == null ? "" : map.get("name").toString();
cell = row.createCell(3);
cell.setCellValue(new HSSFRichTextString(name));
cell.setCellStyle(style2);
//5
String share = map.get("share") == null ? "" : map.get("share").toString();
cell = row.createCell(4);
cell.setCellValue(new HSSFRichTextString(share));
cell.setCellStyle(style2);
//6
String floorNum = map.get("floorNum") == null ? "" : map.get("floorNum").toString();
cell = row.createCell(5);
cell.setCellValue(new HSSFRichTextString(floorNum));
cell.setCellStyle(style2);
//7
String mainArea = map.get("mainArea") == null ? "" : map.get("mainArea").toString();
cell = row.createCell(6);
cell.setCellValue(new HSSFRichTextString(mainArea));
cell.setCellStyle(style2);
//8
String subHouse = map.get("subHouse") == null ? "" : map.get("subHouse").toString();
cell = row.createCell(7);
cell.setCellValue(new HSSFRichTextString(subHouse));
cell.setCellStyle(style2);
//9
String subarea = map.get("subarea") == null ? "" : map.get("subarea").toString();
cell = row.createCell(8);
cell.setCellValue(new HSSFRichTextString(subarea));
cell.setCellStyle(style2);
//10
String account = map.get("account") == null ? "" : map.get("account").toString();
cell = row.createCell(9);
cell.setCellValue(new HSSFRichTextString(account));
cell.setCellStyle(style2);
//11
String totalAmount = map.get("totalAmount") == null ? "" : map.get("totalAmount").toString();
cell = row.createCell(10);
cell.setCellValue(new HSSFRichTextString(totalAmount));
cell.setCellStyle(style2);
cicle++;
}
try {
workbook.write(servletOutputStream);
//告诉浏览器已经完成
FacesContext.getCurrentInstance().responseComplete();
} catch (IOException e) {
procException(e, "文件流写入异常");
}finally {
if(servletOutputStream != null){
try {
servletOutputStream.close();
} catch (IOException e) {
procException(e, "关闭servletOutputStream异常");
}
}
}
}