1. 程式人生 > >Java的Excl表格的匯出功能(程式碼全)

Java的Excl表格的匯出功能(程式碼全)

本文的專案框架為SSM框架
前端頁面需要點選觸發按鈕進入controller。

@RequestMapping("/telExport")
    public void exportFile(HttpServletResponse response,HttpServletRequest request, String searchvalue, String commuId,String parentId, String treename, String type, String starttime,String endtime, String tn) {
//上方帶入的是我所需要查詢內容的引數,可刪除編寫你自己需要的引數
//下方程式碼 可忽略
        Subject currentUser = SecurityUtils.getSubject
(); String userid = currentUser.getSession().getAttribute("_USER_ID").toString(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<CloudRecord> clo = null; String value = null; if (StringUtils.isNotEmpty(searchvalue)) { try { value = URLDecoder.decode
(searchvalue, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } Date st = null; Date et = null; if (StringUtils.isNotEmpty(starttime)) { try { st = df.parse(starttime); } catch (ParseException e) { e.printStackTrace
(); } } if (StringUtils.isNotEmpty(endtime)) { try { et = df.parse(endtime); } catch (ParseException e) { e.printStackTrace(); } } switch (type) {// 查詢需要匯出的內容 case "2": String cityId = parentId.substring(0, 4) + "00"; clo = cloudRecordService.selectCityTelList(cityId, userid, type, value, st, et); break; case "3": clo = cloudRecordService.selectCityTelList(parentId, userid, type, value, st, et); break; case "4": clo = cloudRecordService.selectCityTelList(treename, userid, type, value, st, et); break; case "5": clo = cloudRecordService.selectCityTelList(commuId, userid, type, value, st, et); break; } //直至此處上方程式碼可忽略,上方的程式碼主要是請求資料庫查詢需要匯出的內容,需更換為你自己的內容 //下發為匯出功能不可缺少的步驟 OutputStream os = null; XSSFWorkbook xWorkbook = null; try { os = response.getOutputStream(); response.reset(); response.setContentType("application/msexcel"); response.setHeader("Content-disposition", "attachment; filename=" + tn + df.format(new Date()) + ".xlsx"); response.setContentType("application/msexcel;charset=UTF-8");// 設定型別 response.setHeader("Pragma", "No-cache");// 設定頭 response.setHeader("Cache-Control", "no-cache");// 設定頭 response.setDateHeader("Expires", 0);// 設定日期頭 xWorkbook = new XSSFWorkbook(); XSSFSheet xSheet = xWorkbook.createSheet("通話"); // set Sheet頁頭部 setSheetHeader(xWorkbook, xSheet); // set Sheet頁內容 setSheetContent(xWorkbook, xSheet, clo); xWorkbook.write(os); } catch (IOException e) { e.printStackTrace(); } finally { if (null != os) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } if (null != xWorkbook) { try { xWorkbook.close(); } catch (Exception e) { e.printStackTrace(); } } } } private void setSheetHeader(XSSFWorkbook xWorkbook, XSSFSheet xSheet) { //有多少列要匯出 設定多少個 xSheet.setColumnWidth(0, 20 * 256); xSheet.setColumnWidth(1, 20 * 256); xSheet.setColumnWidth(2, 20 * 256); xSheet.setColumnWidth(3, 20 * 256); xSheet.setColumnWidth(4, 20 * 256); xSheet.setColumnWidth(5, 20 * 256); xSheet.setColumnWidth(6, 20 * 256); CellStyle cs = xWorkbook.createCellStyle(); // 設定水平垂直居中 cs.setAlignment(CellStyle.ALIGN_CENTER); cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cs.setWrapText(true);// 是否自動換行 XSSFRow xRow0 = xSheet.createRow(0); //下發程式碼為設定你需要匯出內容的名稱,內容自行填寫 XSSFCell xCell0 = xRow0.createCell(0); xCell0.setCellStyle(cs); xCell0.setCellValue("區縣"); XSSFCell xCell1 = xRow0.createCell(1); xCell1.setCellStyle(cs); xCell1.setCellValue("小區"); XSSFCell xCell2 = xRow0.createCell(2); xCell2.setCellStyle(cs); xCell2.setCellValue("棟號"); XSSFCell xCell3 = xRow0.createCell(3); xCell3.setCellStyle(cs); xCell3.setCellValue("單元號"); XSSFCell xCell4 = xRow0.createCell(4); xCell4.setCellStyle(cs); xCell4.setCellValue("被叫電話號碼"); XSSFCell xCell5 = xRow0.createCell(5); xCell5.setCellStyle(cs); xCell5.setCellValue("通話時長(-1被叫未接通,0主叫未接通,大於0:通話時長)"); XSSFCell xCell6 = xRow0.createCell(6); xCell6.setCellStyle(cs); xCell6.setCellValue("接通時間"); } private void setSheetContent(XSSFWorkbook xWorkbook, XSSFSheet xSheet, List<CloudRecord> clo) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); CellStyle cs = xWorkbook.createCellStyle(); cs.setWrapText(true); if (null != clo && clo.size() > 0) { for (int i = 0; i < clo.size(); i++) { XSSFRow xRow = xSheet.createRow(i + 1); CloudRecord cloudRecord = clo.get(i); for (int j = 0; j < 8; j++) { XSSFCell xCell = xRow.createCell(j); xCell.setCellStyle(cs); switch (j) { //switch 為取得你要匯出的內容,注意case 後面的數字與上方你設定的標題名稱對於 case 0: xCell.setCellValue(cloudRecord.getAreaName()); break; case 1: xCell.setCellValue(cloudRecord.getAddressName()); break; case 2: xCell.setCellValue(cloudRecord.getBuildingno()); break; case 3: xCell.setCellValue(cloudRecord.getUnit()); break; case 4: xCell.setCellValue(cloudRecord.getTelPhone()); break; case 5: xCell.setCellValue(cloudRecord.getTelduration()); break; case 6: if (cloudRecord.getTime() != null && !cloudRecord.getTime().equals("")) xCell.setCellValue(sdf.format(cloudRecord.getTime())); break; default: break; } } } } }

這樣一個完成的Excl表格匯出功能就結束了。