1. 程式人生 > 其它 >springboot+vue 匯出excel

springboot+vue 匯出excel

package com.tianwen.springcloud.microservice.base.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import com.tianwen.springcloud.microservice.base.dto.course.CourseExcelDTO; public class CourseExcelUtil { /** * 課程匯出類 * @param response 響應 * @param
fileName 檔名 * @param columnList 每列的標題名 * @param dataList 匯出的資料 */ public static void exportCourseExcel(HttpServletResponse response, String fileName, List<CourseExcelDTO> dataList) { List<String> columnList = new ArrayList<String>(); columnList.add(
"課程名稱"); columnList.add("主講人"); columnList.add("檢視許可權"); columnList.add("課程方式"); columnList.add("課程分類"); columnList.add("提交人"); columnList.add("參與人數"); columnList.add("瀏覽次數"); columnList.add("課時數"); columnList.add("課時總時長"); //宣告輸出流 OutputStream os = null; //設定響應頭 setResponseHeader(response,fileName); try { //獲取輸出流 os = response.getOutputStream(); //記憶體中保留5000條資料,以免記憶體溢位,其餘寫入硬碟 SXSSFWorkbook wb = new SXSSFWorkbook(5000); //獲取該工作區的第一個sheet Sheet sheet1 = wb.createSheet("sheet1"); int excelRow = 0; //建立標題行 Row titleRow = sheet1.createRow(excelRow++); for(int i = 0;i<columnList.size();i++) { //建立該行下的每一列,並寫入標題資料 Cell cell = titleRow.createCell(i); cell.setCellValue(columnList.get(i)); } //設定內容行 if(dataList != null && dataList.size() > 0) { //外層for迴圈建立行 for(int i = 0;i<dataList.size();i++) { Row dataRow = sheet1.createRow(excelRow++); //內層for迴圈建立每行對應的列,並賦值 CourseExcelDTO courseObj = dataList.get(i); for(int j = 0; j < columnList.size(); j++) { Cell cell = dataRow.createCell(j); if(j == 0) { cell.setCellValue(courseObj.getCourseName()); } else if(j == 1) { cell.setCellValue(courseObj.getTeachernames()); } else if(j == 2) { cell.setCellValue(courseObj.getViewRole()); } else if(j == 3) { cell.setCellValue(courseObj.getLessonType()); } else if(j == 4) { cell.setCellValue(courseObj.getCourseTypeName()); } else if(j == 5) { cell.setCellValue(courseObj.getCreateUserName()); } else if(j == 6) { cell.setCellValue(courseObj.getJoins()); } else if(j == 7) { cell.setCellValue(courseObj.getViews()); } else if(j == 8) { cell.setCellValue(courseObj.getLessons()); } else if(j == 9) { cell.setCellValue(courseObj.getCourseLessonTotalTime()); } } } } response.setHeader("Content-disposition", "attachment;fileName=" + fileName + ".xls"); response.setContentType("application/octet-stream;charset=utf-8"); //將整理好的excel資料寫入流中 wb.write(os); } catch (IOException e) { e.printStackTrace(); } finally { try { // 關閉輸出流 if (os != null) { os.flush(); os.close(); } } catch (IOException e) { e.printStackTrace(); } } } /* 設定瀏覽器下載響應頭 */ private static void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(),"ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename="+ fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); } } }
vm.$axios
        .post(this.$app.jhEduUrl + '/course/exportCourseList', param, { responseType: 'blob' })
        .then((resp) => {
          if (!resp) {
            console.log('下載資料返回空')
            return
          }
          const link = document.createElement('a');
          let blob = new Blob([resp.data], {type: 'application/vnd.ms-excel'});
          link.style.display = 'none';
          link.href = URL.createObjectURL(blob);

          link.setAttribute('download', '課程列表資訊' + '.xls');
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })