導出excel表格util
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
* @author kk
* @date 2018/8/25 09:25
* @description 導出excel文件工具類
*/
@Slf4j
public class ExcelUtils {
/**
* 導出excel
*
* @param sheetName sheet名稱
* @param title 標題
* @param values 內容
* @param wb HSSWorkbook對象
* @return HSSFWorkbook對象
*/
public static void getHSSWorkbook(HttpServletResponse response,String sheetName,String fileName,
//創建一個HSSWorkbook,對應一個Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
//在workbook中添加一個sheet,對應Excel中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
//在sheet中添加表頭第0行,註意老版本POI對Excel的行數列數有限制
//創建單元格,並設置值表頭,設置表頭居中
HSSFCellStyle cellStyle = wb.createCellStyle();
//創建居中格式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//聲明列對象
HSSFCell cell = null;
//起始行號,終止行號,其實列號,終止列號
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, title.length - 1));
cell = row.createCell(1);
cell.setCellValue("圖文統計詳細數據111111");
cell.setCellStyle(cellStyle);
//創建標題
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(i, 3766);
}
//創建內容
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < values[i].length; j++) {
//將內容按順序賦給對應的列對象
HSSFCell hCell = row.createCell(j);
hCell.setCellValue(values[i][j]);
hCell.setCellStyle(cellStyle);
}
}
setResponseHeader(response,wb,fileName);
}
/**
* 列頭單元格樣式
*/
private static HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
font.setFontHeightInPoints((short) 11);
//字體加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式為居中對齊;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/**
* 列數據信息單元格樣式
*/
private static HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
//font.setFontHeightInPoints((short)10);
//字體加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式為居中對齊;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
/**
* 生成excel表格帶標題
* @param response 響應
* @param sheetName sheet名稱
* @param fileName 文件名稱
* @param title 標題
* @param rowName
* @param dataList
*/
public static void getHSSWorkbookAndTitle(HttpServletResponse response,String sheetName,String fileName,
String title,String[] rowName, List<Object[]> dataList) {
//創建一個HSSWorkbook,對應一個Excel文件
// 創建工作簿對象
HSSFWorkbook wb = new HSSFWorkbook();
// 創建工作表
HSSFSheet sheet = wb.createSheet(sheetName);
// 產生表格標題行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);
//sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴展】
//獲取列頭樣式對象
HSSFCellStyle columnTopStyle = getColumnTopStyle(wb);
//單元格樣式對象
HSSFCellStyle style = getStyle(wb);
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);
// 定義所需列數
int columnNum = rowName.length;
// 在索引2的位置創建行(最頂端的行開始的第二行)
HSSFRow rowRowName = sheet.createRow(2);
// 將列頭設置到sheet的單元格中
for (int n = 0; n < columnNum; n++) {
//創建列頭對應個數的單元格
HSSFCell cellRowName = rowRowName.createCell(n);
//設置列頭單元格的數據類型
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
//設置列頭單元格的值
cellRowName.setCellValue(text);
//設置列頭單元格樣式
cellRowName.setCellStyle(columnTopStyle);
}
//將查詢出的數據設置到sheet對應的單元格中
for (int i = 0; i < dataList.size(); i++) {
//遍歷每個對象
Object[] obj = dataList.get(i);
//創建所需的行數
HSSFRow row = sheet.createRow(i + 3);
for (int j = 0; j < obj.length; j++) {
//設置單元格的數據類型
HSSFCell cell = null;
cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
if (!"".equals(obj[j]) && obj[j] != null) {
//設置單元格的值
cell.setCellValue(obj[j].toString());
}
//設置單元格樣式
cell.setCellStyle(style);
}
}
//讓列寬隨著導出的列長自動適應
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//當前行未被使用過
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if (colNum == 0) {
sheet.setColumnWidth(colNum, (columnWidth + 4) * 256 );
} else {
sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
}
}
setResponseHeader(response,wb,fileName);
}
/**
* 響應到客戶端
*
* @param response 響應
* @param fileName 文件名稱
*/
private static void setResponseHeader(HttpServletResponse response, HSSFWorkbook hssWorkbook, String fileName) {
//響應到客戶端
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Param", "no-cache");
response.addHeader("Cache-Control", "no-cache");
OutputStream outputStream = response.getOutputStream();
hssWorkbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
log.info("響應到客戶端出錯:" + e.getMessage());
}
}
}
導出excel表格util