項目需要導出數據,保存爲excel形式,自己弄了一天,終於摸索出一條可行的方法,現在貼在下面,大家相互學習。
public static String toExcelReport(List<ReportVO> empVOs, HttpServletRequest request) throws Exception { // 第一步,創建一個webbook,對應一個Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet HSSFSheet sheet = wb.createSheet("舉報表"); // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,創建單元格,並設置值表頭 設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式 HSSFCell cell = row.createCell((short) 0); cell.setCellValue("舉報人"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("被舉報人"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("舉報時間"); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("舉報內容"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("被舉報信息"); cell.setCellStyle(style); cell = row.createCell((short) 5); cell.setCellValue("是否處理"); cell.setCellStyle(style); cell = row.createCell((short) 6); cell.setCellValue("處理人"); cell.setCellStyle(style); cell = row.createCell((short) 7); cell.setCellValue("處理時間"); cell.setCellStyle(style); // 第五步,寫入實體數據 實際應用中這些數據從數據庫得到, // List list = CreateSimpleExcelToDisk.getStudent(); for (int i = 0; i < empVOs.size(); i++) { row = sheet.createRow((int) i + 1); ReportVO stu = (ReportVO) empVOs.get(i); // 第四步,創建單元格,並設置值 // row.createCell((short) 0).setCellValue((double) stu.getId()); row.createCell((short) 0).setCellValue(stu.getEmpName()); row.createCell((short) 1).setCellValue(stu.getEmpNameReport()); row.createCell((short) 2).setCellValue(stu.getDateline()); row.createCell((short) 3).setCellValue(stu.getMm_report_content()); row.createCell((short) 4).setCellValue(stu.getMsgCont()==null?"":stu.getMsgCont()); if("0".equals(stu.getIs_use())){ row.createCell((short) 5).setCellValue("未處理"); }else { row.createCell((short) 5).setCellValue("已處理"); } // row.createCell((short) 7).setCellValue(stu.getDateline()); row.createCell((short) 6).setCellValue(stu.getManagerName()==null?"":stu.getManagerName()); if(!StringUtil.isNullOrEmpty(stu.getEnd_dateline())){ row.createCell((short) 7).setCellValue(stu.getEnd_dateline()); }else { row.createCell((short) 7).setCellValue(""); } } // 第六步,將文件存到指定位置 try {
// 注意:這是絕對路徑 // SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//設置日期格式 // String fileName = String.valueOf(df.format(new Date())); // FileOutputStream fout = new FileOutputStream("D:/jubao_"+fileName+".xls"); // wb.write(fout); // fout.close(); // return fileName;
// 這個是相對路徑,request就是獲得項目的相對路徑的 SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//設置日期格式 String fileName = String.valueOf(df.format(new Date())); String path = request.getSession().getServletContext().getRealPath("upload"); FileOutputStream fout = new FileOutputStream(path+"/jubao_"+fileName+".xls"); wb.write(fout); fout.close(); return "/jubao_"+fileName+".xls"; } catch (Exception e) { e.printStackTrace(); return null; } }重點說明一下幾個地方:
1、HttpServletRequest request 是爲了獲得項目的相對地址,保存文件到項目下面。如果你保存文件到絕對地址,這個可以不用。
2、
String path = request.getSession().getServletContext().getRealPath("upload"); FileOutputStream fout = new FileOutputStream(path+"/jubao_"+fileName+".xls");
這個就是保存的相對路徑,如果你要保存絕對路徑,可以這麼寫
FileOutputStream fout = new FileOutputStream("D://jubao_"+fileName+".xls");
、------------------------------------
通過以上的方法就可以保存數據到excel文件,怎麼通過瀏覽器下載呢,哈哈,其實很簡單,一行代碼搞定:
window.location.href = data.url;//這樣就可以彈出下載對話框了
注意!data.url 一定是相對路徑,絕對路徑是下載不了的!!!這就是上面爲什麼用request保存文件到項目下面的原因!