execl导出

来源:互联网 发布:双11是天猫还是淘宝 编辑:程序博客网 时间:2024/05/23 15:06
 注意:需要工具poi jar包 /**     * execl导出 * @param list   内容 * @param request  请求 * @param response  响应 */public static void exportExcelWorkbook(List<?> list,HttpServletRequest request, HttpServletResponse response){       OutputStream out =null;try {Workbook work = new HSSFWorkbook();Sheet sheet = work.createSheet("sheet1");//CellStyle  cellStyle =work.createCellStyle();//cellStyle .setFillForegroundColor(HSSFColor.AQUA.index);//cellStyle.setFillBackgroundColor(HSSFColor.AQUA.index);    //cellStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);Row row = sheet.createRow(0);//sheet.setDefaultColumnStyle(1, sheetStyle);                                  //先加载列名                                for (int i = 0; i < HERDES.length; i++) {Cell cell=row.createCell(i);//cell.setCellStyle(cellStyle);cell.setCellValue(HERDES[i]);sheet.setColumnWidth(i, (30 * 110));}                                  //new row 开始加载内容                               for (int i = 0; i < list.size(); i++) {row = sheet.createRow(i + 1);Object veObj =list.get(i);String[] arr = veObj.toString().split(",");   //这里重写了toString(),更好的转换数组型for (int k = 0; k < HERDES.length; k++) {row.createCell(k).setCellValue(arr[k].substring(arr[k].indexOf("=") + 1).equals("null") ? " ": arr[k].substring(arr[k].indexOf("=") + 1));}}//LOGGER.info("============excel数据填充加载完成===========");response.setHeader("Content-Disposition","attachment;filename=" + new String(filename.getBytes("utf-8"), "iso8859-1"));response.setContentType("application/ynd.ms-excel;charset=UTF-8");out = response.getOutputStream();work.write(out);} catch (Exception e) {                  LOGGER.error(e.getMessage());}finally {   try { out.flush(); out.close();// LOGGER.info("execl导出流关闭");  } catch (IOException e) { LOGGER.error(e.getMessage());  }}  }

原创粉丝点击