/**
*
- 功能描述:
- @Package: com.utils.excel.poiexcel
- @author: l
*/
package com.utils.excel.poiexcel;
import java.io.IOException;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
/**
*
- @ClassName: PoiSXSSFWorkbookExcel.java
- @Description: 導出Excel 採用SXSSFWorkbook實現方式 excel爲07之後版本,只支持.xlsx格式
- @author: l
*/
public class PoiSXSSFWorkbookExcel {
/**
* 導出到單個sheet頁中
* @param title
* 標題
* @param List<List<String>> result
* 導出數據內容
* @return wb
* SXSSFWorkbook對象
* @throws IOException
*
*/
public static SXSSFWorkbook getSxssfwbExcel(String sheetTitle,String[] title, List<List<String>> result) {
SXSSFWorkbook wb = new SXSSFWorkbook();
//int sheetNum = 0;// 記錄額外創建的sheet數量
Sheet sheet = wb.createSheet(sheetTitle);
// wb.setSheetName(sheetNum, sheetTitle+sheetNum);
int rownum = 0;
Row row = sheet.createRow(rownum);
// 設置並獲取到需要的樣式
XSSFCellStyle xssfCellStyleHeader = getAndSetXSSFCellStyleHeader(wb);
Cell cell;
// 創建標題,此時row=0,即第一行
for (int j = 0; j < title.length; j++) {
cell = row.createCell(j);
cell.setCellValue(title[j]);
cell.setCellStyle(xssfCellStyleHeader);
}
// 遍歷集合數據,創建excel內容,產生數據行
if (result != null) {
int index = 1;
List<String> m = null;
for (int i = 0; i < result.size(); i++) {
row = sheet.createRow(index);
int cellIndex = 0;
m = result.get(i);
for (String str : m) {
row.createCell((short) cellIndex).setCellValue(str);
cellIndex++;
}
index++;
}
}
return wb;
}
/**
* 每個sheet頁導出不同內容到多個sheet頁中,每個sheet頁表頭內容不相同
*
* @param wb
* SXSSFWorkbook對象
* @param sheetNum
* sheet頁
* @param sheetTitle
* sheet頁名稱
* @param title
* 標題
* @param List<List<String>> result
* 導出數據內容
* @return wb
* SXSSFWorkbook對象
* @throws IOException
*
*/
public static SXSSFWorkbook getSxssfwbManyDiffSheets(SXSSFWorkbook wb, int sheetNum, String sheetTitle,
String[] title, List<List<String>> result) {
Sheet sheet = wb.createSheet();
wb.setSheetName(sheetNum, sheetTitle);
int rownum = 0;
Row row = sheet.createRow(rownum);
// 設置並獲取到需要的樣式
XSSFCellStyle xssfCellStyleHeader = getAndSetXSSFCellStyleHeader(wb);
Cell cell;
// 創建標題,此時row=0,即第一行
for (int j = 0; j < title.length; j++) {
cell = row.createCell(j);
cell.setCellValue(title[j]);
cell.setCellStyle(xssfCellStyleHeader);
}
// 遍歷集合數據,創建excel內容,產生數據行
if (result != null) {
int index = 1;
List<String> m = null;
for (int i = 0; i < result.size(); i++) {
row = sheet.createRow(index);
int cellIndex = 0;
m = result.get(i);
for (String str : m) {
row.createCell((short) cellIndex).setCellValue(str);
cellIndex++;
}
index++;
}
}
return wb;
}
/**
* 每個sheet頁導出相同內容到多個sheet頁中,每個sheet頁表頭內容相同
* 每個sheet頁目前總數設置爲n,這裏設置n=60000
* @param title
* 標題
* @param List<List<String>> result
* 導出數據內容
* @return wb
* SXSSFWorkbook對象
* @throws IOException
*
*/
public static SXSSFWorkbook getSxssfwbManySameSheets(String sheetTitle,String[] title, List<List<String>> result) {
SXSSFWorkbook wb = new SXSSFWorkbook();
int sheetNum = 0;// 記錄額外創建的sheet數量
Sheet sheet = wb.createSheet(sheetTitle + sheetNum);
// wb.setSheetName(sheetNum, sheetTitle+sheetNum);
int rownum = 0;
Row row = sheet.createRow(rownum);
// 設置並獲取到需要的樣式
XSSFCellStyle xssfCellStyleHeader = getAndSetXSSFCellStyleHeader(wb);
Cell cell;
// 創建標題,此時row=0,即第一行
for (int j = 0; j < title.length; j++) {
cell = row.createCell(j);
cell.setCellValue(title[j]);
cell.setCellStyle(xssfCellStyleHeader);
}
// 遍歷集合數據,創建excel內容,產生數據行
if (result != null) {
List<String> m = null;
for (int i = 0; i < result.size(); i++) {
if ((i + 1) % 60000 == 0) {
sheetNum++;
sheet = wb.createSheet(sheetTitle + sheetNum);
row = sheet.createRow(0);
// 聲明列對象,參數爲列索引,可以是0~255之間的任何一個
// 創建標題,此時row=0,即第一行
for (int j = 0; j < title.length; j++) {
cell = row.createCell(j);
cell.setCellValue(title[j]);
cell.setCellStyle(xssfCellStyleHeader);
}
}
row = sheet.createRow((i + 1) - (sheetNum * 60000)+sheetNum);
int cellIndex = 0;
m = result.get(i);
for (String str : m) {
row.createCell((short) cellIndex).setCellValue(str);
cellIndex++;
}
}
}
return wb;
}
/**
* 獲取並設置header樣式
*/
private static XSSFCellStyle getAndSetXSSFCellStyleHeader(SXSSFWorkbook sxssfWorkbook) {
XSSFCellStyle xssfCellStyle = (XSSFCellStyle) sxssfWorkbook.createCellStyle();
Font font = sxssfWorkbook.createFont();
// 字體大小
font.setFontHeightInPoints((short) 14);
// 字體粗細
font.setBoldweight((short) 20);
font.setFontName("楷體");
// 將字體應用到樣式上面
xssfCellStyle.setFont(font);
// 是否自動換行
xssfCellStyle.setWrapText(false);
// 水平居中
xssfCellStyle.setAlignment(HorizontalAlignment.CENTER);
// 垂直居中
xssfCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return xssfCellStyle;
}
}
上邊是工具類,調用如下:
Date dateBefore = new Date();
String[] title =null;
title = new String[]{"機構","姓名"};
// excel文件名 .xlsx .xls
String fileName = "業務清單" + System.currentTimeMillis() + ".xlsx";
// sheet頁名稱
String sheetName = "業務清單";
List<List
// 內容列表 行、列
int size = list.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
Business respDto = list.get(i);
List
rowData.add(respDto.getComcname()); //歸屬機構
rowData.add(respDto.getname()); //姓名
data1.add(rowData);
}
}
SXSSFWorkbook wb = PoiSXSSFWorkbookExcel.getSxssfwbManySameSheets(sheetName,title, data1);
try {
ServletOutputStream output = response.getOutputStream();
wb.write(output);
output.flush();
Date dateAfter = new Date();
logger.info("共"+list.size()+"條數據,導出列表共執行"+(dateAfter.getTime()-dateBefore.getTime())+"ms");
output.close();
} catch (Exception e) {
logger.info("導出excel時報錯:"+e.getStackTrace());
}