日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WEB下载数据量大的EXCEL解决方案

發布時間:2024/3/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WEB下载数据量大的EXCEL解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

思路:需要2次請求,第一次ajax請求把數據查詢出來,按照1000條數據一個sheet,在后臺生成臨時文件,并把文件名稱返回給web,第二次請求直接下載臨時文件下載完成后,刪除臨時文件

代碼如下web端:

// 導出 function cmd_toExcel() {$.messager.progress({ title: '請等待', msg: "<span style='color:green'>人生若只如初見</span>", text: '努力下載中.......' });$.ajax({type : "post",url : cmd.toexport,data : formToObject('.searchBox'),dataType : "json",success : function(data) {location.href = _ctx + "/rest/download/getfile?name=" + data + "&originFileName=" + "車輛統計.xls";$.messager.progress('close');}}); }

?后臺服務端,生成臨時文件接口:

/*** 導出* * @throws Exception*/@RequestMapping(value = "/toexport", method = { RequestMethod.POST, RequestMethod.GET })@ResponseBodypublic String export(HttpServletRequest request, String area, Integer trans, String org, String no,AnalysisDate analysisdate, @RequestParam(required = false, defaultValue = "VEH_ID") String sort,@RequestParam(required = false, defaultValue = "asc") String order) throws Exception {Pageable pageable = new PageRequest(0, 1000000, new Sort("asc".equalsIgnoreCase(order) ? Direction.ASC: Direction.DESC, sort, "VEH_ID"));List<Map<String, Object>> list = manager.statistics(area, trans, org, no, analysisdate,SpringSecurityUtils.getCurrentUserArea(), pageable).getContent();String title = "車輛統計表";String[] rowsName = new String[] {"序號", "道路運輸證號", "車牌", "企業名稱", "所屬區域", "運輸行業分類", "車輛類型", "上線時長", "斷開時長", "報警次數"};List<Object[]> dataList = new ArrayList<Object[]>();Object[] objs = null;for (int i = 0; i < list.size(); i++) {Map<String, Object> man = list.get(i);objs = new Object[rowsName.length];objs[0] = i;objs[1] = man.get("RTPN");objs[2] = man.get("VEH_ID") == null ? "" : man.get("VEH_ID");objs[3] = man.get("ORG_NAME") == null ? "" : man.get("ORG_NAME");objs[4] = man.get("AREANAME");objs[5] = man.get("TRANS_TYPE");objs[6] = man.get("VTYPE");objs[7] = durationFormatter(((BigDecimal)man.get("ONLINE_TIME")).intValue());objs[8] = durationFormatter(((BigDecimal)man.get("OFF_TIME")).intValue());objs[9] = man.get("WARN_AMOUNT");dataList.add(objs);}ExportExcel ex = new ExportExcel(title, rowsName, dataList, request);return ex.export();} package com.wttech.gnss.utils;import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID;import javax.servlet.http.HttpServletRequest;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.hssf.util.HSSFColor;/*** 導出Excel公共方法* **/ public class ExportExcel {// 顯示的導出表的標題private String title;// 導出表的列名private String[] rowName;private List<Object[]> dataList = new ArrayList<Object[]>();HttpServletRequest request;// 構造方法,傳入要導出的數據public ExportExcel(String title, String[] rowName, List<Object[]> dataList, HttpServletRequest request) {this.request = request;this.dataList = dataList;this.rowName = rowName;this.title = title;}/** 導出數據*/@SuppressWarnings("deprecation")public String export() throws Exception {String uuid = "/temp_" + UUID.randomUUID().toString() + ".xls";String filename = Properties.UPLOAD_ADDRESS + uuid;try {HSSFWorkbook workbook = new HSSFWorkbook(); // 創建工作簿對象int sheets = dataList.size() / 50000 + 1;for (int m = 0; m < sheets; m++) {HSSFSheet sheet = workbook.createSheet(title + m); // 創建工作表// 產生表格標題行HSSFRow rowm = sheet.createRow(0);HSSFCell cellTiltle = rowm.createCell(0);// sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴展】HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);// 獲取列頭樣式對象HSSFCellStyle style = this.getStyle(workbook); // 單元格樣式對象HSSFCellStyle headerStyle = getHeaderStyle(workbook);sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));cellTiltle.setCellStyle(headerStyle);cellTiltle.setCellValue(title);// 定義所需列數int columnNum = rowName.length;HSSFRow rowRowName = sheet.createRow(2); // 在索引3的位置創建行(最頂端的行開始的第3行)// 將列頭設置到sheet的單元格中for (int n = 0; n < columnNum; n++) {HSSFCell cellRowName = rowRowName.createCell(n); // 創建列頭對應個數的單元格cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 設置列頭單元格的數據類型HSSFRichTextString text = new HSSFRichTextString(rowName[n]);cellRowName.setCellValue(text); // 設置列頭單元格的值cellRowName.setCellStyle(columnTopStyle); // 設置列頭單元格樣式}// 將查詢出的數據設置到sheet對應的單元格中List<Object[]> sub = dataList.subList(m * 50000, (m + 1) * 50000 < dataList.size() ? (m + 1) * 50000: dataList.size());for (int i = 0; i < sub.size(); i++) {Object[] obj = sub.get(i);// 遍歷每個對象HSSFRow row = sheet.createRow(i + 3);// 創建所需的行數for (int j = 0; j < obj.length; j++) {HSSFCell cell = null; // 設置單元格的數據類型if (j == 0) {cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);cell.setCellValue(i + 1);} else {cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);if (obj[j] != null) {cell.setCellValue(obj[j].toString()); // 設置單元格的值} else {cell.setCellValue(""); // 設置單元格的值}}cell.setCellStyle(style); // 設置單元格樣式}}// 讓列寬隨著導出的列長自動適應for (int colNum = 0; colNum < columnNum; colNum++) {switch (colNum) {case 0:sheet.setColumnWidth(colNum, 6 * 256);break;default:sheet.setColumnWidth(colNum, 20 * 256);break;}}}if (workbook != null) {try {File file = new File(filename);// 以流的形式下載文件。BufferedOutputStream fis = new BufferedOutputStream(new FileOutputStream(file));workbook.write(fis);fis.flush();// 刷新流fis.close();// 關閉流} catch (IOException e) {e.printStackTrace();}}} catch (Exception e) {e.printStackTrace();}return uuid;}/** 表頭單元格樣式*/public HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook) {// 設置字體HSSFFont font = workbook.createFont();// 設置字體大小font.setFontHeightInPoints((short) 11);// 字體加粗font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 設置字體名字font.setFontName("Courier New");// 設置樣式;HSSFCellStyle style = workbook.createCellStyle();// 在樣式用應用設置的字體;style.setFont(font);// 設置自動換行;style.setWrapText(false);// 設置水平對齊的樣式為居中對齊;style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 設置垂直對齊的樣式為居中對齊;style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);return style;}/** 表頭單元格樣式*/public HSSFCellStyle getRightStyle(HSSFWorkbook workbook) {// 設置字體HSSFFont font = workbook.createFont();// 設置字體大小font.setFontHeightInPoints((short) 11);// 字體加粗font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 設置字體名字font.setFontName("Courier New");// 設置樣式;HSSFCellStyle style = workbook.createCellStyle();// 在樣式用應用設置的字體;style.setFont(font);// 設置自動換行;style.setWrapText(false);// 設置水平對齊的樣式為居中對齊;style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 設置垂直對齊的樣式為居中對齊;style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);return style;}/** 列頭單元格樣式*/public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {// 設置字體HSSFFont font = workbook.createFont();// 設置字體大小font.setFontHeightInPoints((short) 11);// 字體加粗font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 設置字體名字font.setFontName("Courier New");// 設置樣式;HSSFCellStyle style = workbook.createCellStyle();// 設置底邊框;style.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 設置底邊框顏色;style.setBottomBorderColor(HSSFColor.BLACK.index);// 設置左邊框;style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 設置左邊框顏色;style.setLeftBorderColor(HSSFColor.BLACK.index);// 設置右邊框;style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 設置右邊框顏色;style.setRightBorderColor(HSSFColor.BLACK.index);// 設置頂邊框;style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 設置頂邊框顏色;style.setTopBorderColor(HSSFColor.BLACK.index);// 在樣式用應用設置的字體;style.setFont(font);// 設置自動換行;style.setWrapText(false);// 設置水平對齊的樣式為居中對齊;style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 設置垂直對齊的樣式為居中對齊;style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);return style;}/** 表頭單元格樣式*/public HSSFCellStyle getFootStyle(HSSFWorkbook workbook) {// 設置字體HSSFFont font = workbook.createFont();// 設置字體大小font.setFontHeightInPoints((short) 11);// 字體加粗font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 設置字體名字font.setFontName("Courier New");// 設置樣式;HSSFCellStyle style = workbook.createCellStyle();// 在樣式用應用設置的字體;style.setFont(font);// 設置自動換行;style.setWrapText(false);// 設置水平對齊的樣式為居中對齊;style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 設置垂直對齊的樣式為居中對齊;style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);return style;}/** 列數據信息單元格樣式*/public HSSFCellStyle getStyle(HSSFWorkbook workbook) {// 設置字體HSSFFont font = workbook.createFont();// 設置字體大小// font.setFontHeightInPoints((short)10);// 字體加粗// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 設置字體名字font.setFontName("Courier New");// 設置樣式;HSSFCellStyle style = workbook.createCellStyle();// 設置底邊框;style.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 設置底邊框顏色;style.setBottomBorderColor(HSSFColor.BLACK.index);// 設置左邊框;style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 設置左邊框顏色;style.setLeftBorderColor(HSSFColor.BLACK.index);// 設置右邊框;style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 設置右邊框顏色;style.setRightBorderColor(HSSFColor.BLACK.index);// 設置頂邊框;style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 設置頂邊框顏色;style.setTopBorderColor(HSSFColor.BLACK.index);// 在樣式用應用設置的字體;style.setFont(font);// 設置自動換行;style.setWrapText(false);// 設置水平對齊的樣式為居中對齊;style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 設置垂直對齊的樣式為居中對齊;style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);return style;} }

接口2下載臨時文件,并刪除

@RequestMapping(value = "/getfile")@ResponseBodypublic void getfile(HttpServletRequest request,HttpServletResponse response,String name,String originFileName) throws ParseException {String path = Properties.UPLOAD_ADDRESS + name ;File file = new File(path);// 以流的形式下載文件。InputStream fis;try {fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.addHeader("Content-Disposition", "attachment;filename=" +URLEncoder.encode(originFileName, "UTF-8"));response.setCharacterEncoding("utf-8");response.addHeader("Content-Length", "" + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.close();if (name.contains("temp")) {file.delete();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

?

轉載于:https://my.oschina.net/chuibilong/blog/845053

總結

以上是生活随笔為你收集整理的WEB下载数据量大的EXCEL解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。