POI插件导出EXCEL报表
public void createExportExcel(Object obj,List list,List<ExportManager> exportList,String sheetName,String objectFlag, HttpServletResponse response) throws ParameterErrorException {HSSFWorkbook workbook = null;try {workbook = new HSSFWorkbook();//创建工作薄HSSFSheet sheet = workbook.createSheet();workbook.setSheetName(0, sheetName );HSSFFont font = workbook.createFont(); font.setColor(HSSFFont.COLOR_RED);font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);HSSFCellStyle cellStyle = workbook.createCellStyle();//创建格式cellStyle.setFont(font); HSSFRow row = sheet.createRow((short) 0);//第一行 表头for(short i = 0;i < exportList.size();i++){ HSSFCell cell = row.createCell((short) i); //创建第1行单元格 cell.setCellValue(exportList.get(i).getDisplayName()); cell.setCellStyle(cellStyle);} //第二行开始是数据List<List<Object>> allContentList = getAllContentList(obj,list, exportList,objectFlag); for(int i=1;i<allContentList.size()+1;i++){ HSSFRow rowdata = sheet.createRow((short) i); for (int j = 0; j < exportList.size(); j++) { HSSFCell celldata = rowdata.createCell((short) j); // 在上面行索引0的位置创建单元格 celldata.setCellType(HSSFCell.CELL_TYPE_STRING); // 定义单元格为字符串类型 celldata.setCellValue(allContentList.get(i-1).get(j)+ ""); // 在单元格中输入一些内容} } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); String fullDateStr = simpleDateFormat.format(new Date()); response.setContentType("application/vnd.ms-excel;charset=utf-8");// 表示以附件的形式把文件发送到客户端response.setHeader("Content-Disposition", "attachment;filename="+ new String((sheetName+fullDateStr+".xls").getBytes(), "ISO8859-1"));// 通过response的输出流把工作薄的流发送浏览器形成文件OutputStream os = response.getOutputStream();workbook.write(os);os.flush();} catch (Exception e) {throw new ParameterErrorException("导出excel失败,原因:"+e.getMessage());} }/** * * @throws ParameterErrorException * @Method_Name getAllContentList * @Description 拼装所有的导出内容 * @@param list * @@param tableHeaderArray * @Return List<List<String>> * @Author zhuangjinlei * @CreateDate 2010-4-1 * @LastUpdateDate 2010-4-1,Administrator */public List<List<Object>> getAllContentList(Object obj,List<Object> list,List<ExportManager> exportList,String objectFlag) throws ParameterErrorException{List<List<Object>> allContentList = new ArrayList<List<Object>>();Map<String, Object> map = null;if (objectFlag.equals("dayYield")) {for (int i = 0; i < list.size(); i++) {map = ServiceSearchTemplate.getInstance().getDayYieldSearch((YieldManager)list.get(i));allContentList.add(getSingleRowContentListCommPart(map, exportList));}}return allContentList;}/** * 组装单行导出记录 */public List<Object> getSingleRowContentListCommPart(Map<String, Object> map,List<ExportManager> exportList){List<Object> singleRowContentList = new ArrayList<Object>();for (int j = 0; j < exportList.size(); j++) {singleRowContentList.add(map.get(exportList.get(j).getHeader().trim().toUpperCase()));}return singleRowContentList;}
?