1. 程式人生 > 程式設計 >Springboot POI匯出Excel(瀏覽器)

Springboot POI匯出Excel(瀏覽器)

本文例項為大家分享了Springboot POI匯出Excel的具體程式碼,供大家參考,具體內容如下

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

由於本次匯出資料量較大,這裡採用XSSFWorkbook多執行緒進行匯出,注:XSSFWorkbook匯出excel檔案結尾為:“.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 endTime) throws Exception{
   //list自己查詢得出
    List<StateDetailsResult> list = queryStateDetails(deviceId,endTime);
    String time = TimeUtils.YEAR_DAY_SECOND_FORMAT.format(new Date());
    //sheet名稱
    String sheetName = deviceDtls.getName() + "狀態"+time;
    //檔名稱
    String excelName = deviceDtls.getName() + "狀態"+time+".xlsx";
    //檔案頭
    String[] strings = {"狀態名稱","開始時間","結束時間","狀態時長"};
    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 {
    // 總資料條數
    int dataSize = list.size();
    // 執行緒數
    int threadNum = 2;
    int threadSize = dataSize / threadNum;
    ExecutorService exec = Executors.newFixedThreadPool(threadNum);
    //cutList 和輸入list型別保持一致
    List<StateDetailsResult> cutList = null;

    // 第一步,建立一個webbook,對應一個Excel檔案
    XSSFWorkbook wb = new XSSFWorkbook();
    // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet
    XSSFSheet sheet = wb.createSheet(sheetName);
    sheet.setDefaultColumnWidth(20);// 預設列寬
    // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short
    XSSFRow row = sheet.createRow((int) 0);
    // 第四步,建立單元格,並設定值表頭 設定表頭居中
    XSSFCellStyle style = wb.createCellStyle();
    // 建立一個居中格式
    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;
    // 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,list中字串的順序必須和陣列strArray中的順序一致
    int startNum ;
    System.out.println("任務開始,總數:"+list.size());
    // 開始時間
    long start = System.currentTimeMillis();
    System.out.println("執行緒任務執行");
    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;
        @Override
        public Integer call() throws Exception {
          for(int j=0;j<listStr.size();j++){
            XSSFRow row =getRow(sheet,s+j);
            //設定每一列展示內容自己設定
            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列表存在順序對應的關係
      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("執行緒任務執行結束");
    System.err.println("執行任務消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");
    return filePath;
  }
//執行緒同步,保證不會多插入資料
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 {
 
 /**
 * 判斷路徑是否存在不存在則建立
 * @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(瀏覽器)

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。