<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
package com.gllic.workweixin.utils;
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 javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* @program: workweixin-mobile-api
* @ClassName ExcelUtil
* @description:
* @author: Marlo
* @create: 2021-07-29 10:06
* @Version 1.0
**/
public class ExcelUtil {
/**
* 導出類
*
* @param response 響應
* @param fileName 文件名
* @param columnList 每列的標題名
* @param dataList 導出的數據
*/
public static void uploadExcelAboutUser(HttpServletResponse response, String fileName, List<String> columnList, List<List<String>> dataList) {
//聲明輸出流
FileOutputStream output = null;
//設置響應頭
//setResponseHeader(response, fileName);
try {
//獲取輸出流
output = new FileOutputStream("C:\\myselefzxq\\image\\testFile.xls");
//內存中保留1000條數據,以免內存溢出,其余寫入硬盤
SXSSFWorkbook wb = new SXSSFWorkbook(1000);
//獲取該工作區的第一個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) {
//序號是從1開始的
//int count =0;
//外層for循環創建行
for (int i = 0; i < dataList.size(); i++) {
Row dataRow = sheet1.createRow(excelRow++);
//內層for循環創建每行對應的列,並賦值
for (int j = 0; j < dataList.get(0).size(); j++) {//由於多了一列序號列所以內層循環從-1開始
Cell cell = dataRow.createCell(j);
cell.setCellValue(dataList.get(i).get(j));
/*if(j==0){//第一列是序號列,不是在數據庫中讀取的數據,因此手動遞增賦值
//cell.setCellValue(count++);
}else{//其余列是數據列,將數據庫中讀取到的數據依次賦值
cell.setCellValue(dataList.get(i).get(j));
}*/
}
}
}/*
//准備將Excel的輸出流通過response輸出到頁面下載
//八進制輸出流
response.setContentType("application/octet-stream");
//這后面可以設置導出Excel的名稱,此例中名為student.xls
response.setHeader("Content-disposition", "attachment;filename=employee.xls");
//刷新緩沖
response.flushBuffer();*/
//workbook將Excel保存到指定路徑
wb.write(output);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 關閉輸出流
if (output != null) {
output.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();
}
}
}
測試用例:
List<String> columnList = new ArrayList<>();
columnList.add("title1");
columnList.add("title2");
columnList.add("title3");
columnList.add("title4");
columnList.add("title5");
//數據
List<List<String>> dataList = new ArrayList<>();
List<String> columnList1 = new ArrayList<>();
columnList1.add("data11");
columnList1.add("data12");
columnList1.add("data13");
columnList1.add("data14");
columnList1.add("data15");
List<String> columnList2 = new ArrayList<>();
columnList2.add("data21");
columnList2.add("data22");
columnList2.add("data23");
columnList2.add("data24");
columnList2.add("data25");
dataList.add(columnList1);
dataList.add(columnList2);
//轉存到excel
//ExcelUtil.uploadExcelAboutUser(response,"測試exportExcel",columnList,dataList);