成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

Springboot POI導出Excel(瀏覽器)

瀏覽:131日期:2022-06-17 18:04:30

本文實例為大家分享了Springboot POI導出Excel的具體代碼,供大家參考,具體內(nèi)容如下

需求:頁面根據(jù)查詢條件導出(瀏覽器)

由于本次導出數(shù)據(jù)量較大,這里采用XSSFWorkbook多線程進行導出,注:XSSFWorkbook導出excel文件結(jié)尾為:“.xlsx”。

導出不需要返回,如有返回則會報異常!

//Controller@RequestMapping('/stateExport') public void stateExport(HttpServletResponse response,@RequestParam('deviceId') Long deviceId, String startTime,String endTime) { try { deviceMonitorService.stateExport(response, deviceId, startTime,endTime); LOG.info('導出成功'); } catch (Exception e) { LOG.error('導出異常:',e.getMessage()); } }

//Service @Override public void stateExport(HttpServletResponse response, Long deviceId, String startTime, String endTime) throws Exception{ //list自己查詢得出 List<StateDetailsResult> list = queryStateDetails(deviceId, startTime, endTime); String time = TimeUtils.YEAR_DAY_SECOND_FORMAT.format(new Date()); //sheet名稱 String sheetName = deviceDtls.getName() + '狀態(tài)'+time; //文件名稱 String excelName = deviceDtls.getName() + '狀態(tài)'+time+'.xlsx'; //文件頭 String[] strings = {'狀態(tài)名稱','開始時間','結(jié)束時間','狀態(tài)時長'}; String path = this.getClass().getResource('').getPath() + 'excel'; DownloadFileUtil.createDirs(path); String filePath = path + '/' + sheetName + '.xls'; stateCreateExcel(list,strings,sheetName,excelName,filePath); DownloadFileUtil.download(filePath, response); }

public String stateCreateExcel(List<StateDetailsResult> list, String[] strArray,String sheetName,String excelName,String filePath)throws Exception { // 總數(shù)據(jù)條數(shù) int dataSize = list.size(); // 線程數(shù) int threadNum = 2; int threadSize = dataSize / threadNum; ExecutorService exec = Executors.newFixedThreadPool(threadNum); //cutList 和輸入list類型保持一致 List<StateDetailsResult> cutList = null; // 第一步,創(chuàng)建一個webbook,對應一個Excel文件 XSSFWorkbook wb = new XSSFWorkbook(); // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet XSSFSheet sheet = wb.createSheet(sheetName); sheet.setDefaultColumnWidth(20);// 默認列寬 // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數(shù)列數(shù)有限制short XSSFRow row = sheet.createRow((int) 0); // 第四步,創(chuàng)建單元格,并設置值表頭 設置表頭居中 XSSFCellStyle style = wb.createCellStyle(); // 創(chuàng)建一個居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 添加excel title XSSFCell cell = null; for (int i = 0; i < strArray.length; i++) { cell = row.createCell((short) i); cell.setCellValue(strArray[i]); cell.setCellStyle(style); } List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); Callable<Integer> task = null; // 第五步,寫入實體數(shù)據(jù) 實際應用中這些數(shù)據(jù)從數(shù)據(jù)庫得到,list中字符串的順序必須和數(shù)組strArray中的順序一致 int startNum ; System.out.println('任務開始,總數(shù):'+list.size()); // 開始時間 long start = System.currentTimeMillis(); System.out.println('線程任務執(zhí)行'); for (int i = 0; i < threadNum; i++) { startNum = threadSize * i; if (i == threadNum - 1) {cutList = list.subList(threadSize * i, dataSize); } else {cutList = list.subList(threadSize * i, threadSize * (i + 1)); } //listStr 和輸入list類型保持一致 final List<StateDetailsResult> listStr = cutList; int finalStartNum = startNum; task = new Callable<Integer>() {final int s= finalStartNum;@Overridepublic Integer call() throws Exception { for(int j=0;j<listStr.size();j++){ XSSFRow row =getRow(sheet,s+j); //設置每一列展示內(nèi)容自己設置 row.createCell(0).setCellValue(listStr.get(j).getName()); row.createCell(1).setCellValue(listStr.get(j).getStartDateTime()); row.createCell(2).setCellValue(listStr.get(j).getEndDateTime()); row.createCell(3).setCellValue(listStr.get(j).getDateTime()); } return 1;} }; // 這里提交的任務容器列表和返回的Future列表存在順序?qū)年P系 tasks.add(task); } try { List<Future<Integer>> results = exec.invokeAll(tasks); } catch (Exception e) { e.printStackTrace(); } // 關閉線程池 exec.shutdown(); DownloadFileUtil.delfile(filePath); // 第六步,將文件存到指定位置 try { FileOutputStream fout = new FileOutputStream(filePath); wb.write(fout); fout.flush(); fout.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println('線程任務執(zhí)行結(jié)束'); System.err.println('執(zhí)行任務消耗了 :' + (System.currentTimeMillis() - start) + '毫秒'); return filePath; }

//線程同步,保證不會多插入數(shù)據(jù)private synchronized XSSFRow getRow(XSSFSheet sheet, int rownum) { //如果不包含列頭,+1去掉即可 return sheet.createRow(rownum+1); }

最后附上使用的工具類

package com.sec.deviceband.utils;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.net.URLEncoder;public class DownloadFileUtil { /** * 判斷路徑是否存在不存在則創(chuàng)建 * @param path */ public static void createDirs(String path) { File file = new File(path); if (!file.exists()) { file.mkdirs(); } } /** * 下載 * * @param path * @param response */ public static void download(String path, HttpServletResponse response) { try { // path是指欲下載的文件的路徑。 File file = new File(path); // 取得文件名。 String filename = file.getName(); // 以流的形式下載文件。 InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 設置response的Header response.addHeader('Content-Disposition', 'attachment;filename=' + URLEncoder.encode(filename, 'utf-8')); response.addHeader('Content-Length', '' + file.length()); OutputStream toClient = new BufferedOutputStream( response.getOutputStream()); response.setContentType('application/vnd.ms-excel;charset=utf-8'); toClient.write(buffer); toClient.flush(); toClient.close(); delfile(path); } catch (IOException ex) { ex.printStackTrace(); } } /** * 判斷文件是否存在則刪除 * * @param filepath */ public static void delfile(String filepath) { File file = new File(filepath); if (file.exists()) { file.delete(); } }}

測試方式:瀏覽器輸入請求路徑

Springboot POI導出Excel(瀏覽器)

測試效果:

Springboot POI導出Excel(瀏覽器)

Springboot POI導出Excel(瀏覽器)

由于水平有限,博客中難免會有一些錯誤,有紕漏之處懇請各位大佬不吝賜教!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: excel
相關文章:
主站蜘蛛池模板: 最新亚洲情黄在线网站无广告 | 欧美另类视频一区二区三区 | 中文字幕亚洲精品第一区 | 成人国产网站 | 亚洲黄色在线播放 | 干综合网| 亚洲国产欧美目韩成人综合 | 免费女人18毛片a级毛片视频 | 中文字幕区 | 欧美三级网站在线观看 | 在线免费观看国产 | 特级aaa片毛片免费观看 | 国产成人性色视频 | 国产日韩线路一线路二 | 久热精品免费视频 | 男人操女人逼逼视频 | 2020久久国产最新免费观看 | 中文成人在线 | 亚洲一区二区三区免费看 | 国产综合精品在线 | 一级做性色a爰片久久毛片 一级做性色a爰片久久毛片免费 | 国内精品一区二区三区最新 | 国产一级二级三级视频 | 久草在线在线观看 | 久久大胆视频 | 国产99精品在线观看 | 日韩一区二区精品久久高清 | 欧美一区二区三区久久综合 | 午夜精品网 | 一级毛片国产 | 久久久久久免费视频 | 久久99精品久久久久久野外 | 久久国产精品-久久精品 | 国产黄色片在线免费观看 | gayxxxxgay中国老头| 欧美色老头oldvideo | 欧美综合成人网 | 成年午夜性视频免费播放 | 成年免费大片黄在线观看一 | 国产精品人成 | 美女精品永久福利在线 |