前注:來源於網絡
描述:客戶端通過HTTP的GET方式請求,在后台處理並從數據庫中查詢到數據列表,最終將數據列表生成HTTP相應流返回到客戶端,客戶端即實現對數據的Excel導出。
1. 依賴
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.6</version> </dependency>
2. 工具類
package com.xfc.util; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * @Auther: xfc * @Date: 2019/5/20 0020 14:21 * @Description: */ public class ExcelUtil { /** * 導出Excel * * @param sheetName sheet名稱 * @param title 標題 * @param values 內容 * @param wb HSSFWorkbook對象 * @return */ public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) { // 第一步,創建一個HSSFWorkbook,對應一個Excel文件 if (wb == null) wb = new HSSFWorkbook(); // 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet HSSFSheet sheet = wb.createSheet(sheetName); // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制 HSSFRow row = sheet.createRow(0); // 第四步,創建單元格,並設置值表頭 設置表頭居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式 //聲明列對象 HSSFCell cell = null; //創建標題 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); } //創建內容 for (int i = 0; i < values.length; i++) { row = sheet.createRow(i + 1); for (int j = 0; j < values[i].length; j++) { //將內容按順序賦給對應的列對象 row.createCell(j).setCellValue(values[i][j]); } } return wb; } }
3. 調用
因為業務簡單,我這里在controller層就直接處理了
/** * 根據條件將數據導出為Excel * 如果需要瀏覽器發送請求時即下載Excel,就不能用ajax進行傳輸,所以這里用GET方式進行提交 * * @return */ @ResponseBody @RequestMapping(value = "/exportExcel", method = RequestMethod.GET) public void exportExcel(HttpServletRequest request, HttpServletResponse response) { QRRecord obj = new QRRecord(); obj.setPageStart(Integer.valueOf(request.getParameter("pageStart"))); obj.setPageSize(Integer.valueOf(request.getParameter("pageSize"))); obj.setTerm(request.getParameter("term")); List<QRRecord> list = qrRecordService.findQRRecordList(obj); String[] title = {"ID", "業主姓名", "識別時間", "設備名稱", "識別位置", "目的樓層", "備注信息"}; String filename = "QRCodeRecord.xls"; String sheetName = "sheet1"; String[][] content = new String[list.size()][7]; try { for (int i = 0; i < list.size(); i++) { content[i][0] = String.valueOf(list.get(i).getRid()); content[i][1] = list.get(i).getOwnerName(); content[i][2] = list.get(i).getAddTime(); content[i][3] = list.get(i).getDeviceName(); content[i][4] = list.get(i).getLocation(); content[i][5] = String.valueOf(list.get(i).getAimFloor()); content[i][6] = list.get(i).getDescription(); } } catch (Exception e) { e.printStackTrace(); } HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null); try { // 響應到客戶端 this.setResponseHeader(response, filename); OutputStream os = response.getOutputStream(); wb.write(os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 向客戶端發送響應流方法 * * @param response * @param fileName */ public void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); } catch (Exception ex) { ex.printStackTrace(); } }
4. JS調用
let url = "localhost:808//.../.../exportExcel?pageStart=0&pageSize=50";
location.href = url;
5. 成功后,即通過瀏覽器進行下載
此
處
留
白