1. 程式人生 > 實用技巧 >Excel匯出工具類封裝

Excel匯出工具類封裝

1.呼叫方式

ExcelUtils.exportExcel(resultOrderList, fieldLinkedMap, response.getOutputStream(), fileName, suffix);

2.exportExcel(List<Map<String, Object>> resultOrderList, Map<String, Object> headOrderMap, OutputStream os, String sheetTitle, String version)方法引數介紹

  resultOrderList:從資料庫獲取到的Map型別的資料集合。集合示例:[{"file_name":"hadoop規劃表","dept_name":"資料部"},{"file_name":"財年資料運營統計表","dept_name":"運營部"}]

  headOrderMap:標題有序集合,可以使用LinkedHashMap。集合示例:{"file_name":"檔名稱","dept_name":"所屬部門"}

  sheetTitle:sheet頁的名稱。

  version:excel的版本,可以是xls或者xlsx。

3.工具類程式碼(放到java檔案中可以直接使用)

package com.zkdj.huishu.crm.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import
org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Excel工具類 * Author zhangjifu * Date 2020年11月12日-下午6:00:50 */ public class ExcelUtils { /** * 匯出Excel表格 * @param resultOrderList 結果集列表 * @param headOrderMap 頭部有序集合 * @param sheetTitle sheet頁標題 * @param os 輸出流 * @param version Excel版本:xls或者xlsx */ public static void exportExcel(List<Map<String, Object>> resultOrderList, Map<String, Object> headOrderMap, OutputStream os, String sheetTitle, String version) { if (StringUtils.isBlank(version) || "xls".equals(version.trim())) { exportHSExcel(resultOrderList, headOrderMap, sheetTitle, os); } else { exportXSExcel(resultOrderList, headOrderMap, sheetTitle, os); } } /** * 2003版Excel表格匯出,字尾為xls * @param resultOrderList 結果集列表 * @param headOrderMap 頭部有序集合 * @param os 輸出流 */ private static void exportHSExcel(List<Map<String, Object>> resultOrderList, Map<String, Object> headOrderMap, String sheetTitle, OutputStream os) { // 宣告一個工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個表格 HSSFSheet sheet = workbook.createSheet(sheetTitle); // 設定表格預設列寬度為15個位元組 sheet.setDefaultColumnWidth(20); sheet.setDefaultRowHeightInPoints(24); // 指定寬度 // setColumnWidth(sheet, excelComplex.getColumnWidths()); // 生成頭部樣式 HSSFCellStyle headStyle = workbook.createCellStyle(); // 設定頭部樣式 setHeadCellStyle(headStyle); // 生成頭部字型 HSSFFont headFont = workbook.createFont(); // 設定頭部字型 headFont.setFontName("宋體"); headFont.setColor(IndexedColors.WHITE.getIndex()); headFont.setFontHeightInPoints((short) 12); // 把頭部字型應用到頭部樣式 headStyle.setFont(headFont); // 生成資料行樣式 HSSFCellStyle resultStyle = workbook.createCellStyle(); setResultCellStyle(resultStyle); // 生成資料行字型 HSSFFont resultFont = workbook.createFont(); resultFont.setBold(true); // 把資料行字型應用到資料行樣式 resultStyle.setFont(resultFont); // 遍歷頭部集合,產生標題行 int headRowIndex = 0; int headCellIndex = 0; HSSFRow row = sheet.createRow(headRowIndex); HSSFCell cellHeader; Iterator<Entry<String, Object>> headIterator = headOrderMap.entrySet().iterator(); while(headIterator.hasNext()){ Entry<String, Object> nextEntry = headIterator.next(); cellHeader = row.createCell(headCellIndex); cellHeader.setCellStyle(headStyle); cellHeader.setCellValue(new HSSFRichTextString(StringUtils.isBlank((String) nextEntry.getValue())?"":String.valueOf(nextEntry.getValue()))); headCellIndex++; } // 遍歷集合資料,產生資料行 int resultRowIndex = headRowIndex+1; int resultCellIndex = 0; HSSFCell cell; for (Map<String, Object> resultMap : resultOrderList) { row = sheet.createRow(resultRowIndex); headIterator = headOrderMap.entrySet().iterator(); while(headIterator.hasNext()){ String cellValue = String.valueOf(resultMap.get(headIterator.next().getKey())); cell = row.createCell(resultCellIndex); cell.setCellStyle(resultStyle); if(StringUtils.isNotBlank(cellValue) && "null"!=cellValue){ cell.setCellValue(new HSSFRichTextString(cellValue)); } resultCellIndex++; } resultCellIndex=0; resultRowIndex++; } // 將Excel傳送到客戶端 workBookWrite(os, workbook); } /** * 2007版Excel匯出,字尾為xlsx * @param resultOrderList 結果集列表 * @param headOrderMap 頭部有序集合 * @param sheetTitle sheet頁標題 * @param os 輸出流 */ private static void exportXSExcel(List<Map<String, Object>> resultOrderList, Map<String, Object> headOrderMap, String sheetTitle, OutputStream os) { // 宣告一個工作薄 XSSFWorkbook workbook = new XSSFWorkbook(); // 生成一個表格 XSSFSheet sheet = workbook.createSheet(sheetTitle); // 設定表格預設列寬度為15個位元組 sheet.setDefaultColumnWidth(20); sheet.setDefaultRowHeightInPoints(24); // 指定寬度 // setColumnWidth(sheet, excelComplex.getColumnWidths()); // 生成頭部樣式 XSSFCellStyle headStyle = workbook.createCellStyle(); // 設定頭部樣式 setHeadCellStyle(headStyle); // 生成頭部字型 XSSFFont headFont = workbook.createFont(); headFont.setColor(IndexedColors.WHITE.getIndex()); headFont.setFontHeightInPoints((short) 12); headFont.setBold(true); // 把頭部字型應用到頭部樣式 headStyle.setFont(headFont); // 生成資料行樣式 XSSFCellStyle resultStyle = workbook.createCellStyle(); // 設定資料行央視 setResultCellStyle(resultStyle); // 生成資料行字型 XSSFFont resultFont = workbook.createFont(); // 設定資料行字型 resultFont.setBold(true); // 把資料行字型應用資料行樣式 resultStyle.setFont(resultFont); // 遍歷頭部集合,產生標題行 int headRowIndex = 0; int headCellIndex = 0; XSSFRow row = sheet.createRow(headRowIndex); XSSFCell cellHeader; Iterator<Entry<String, Object>> headIterator = headOrderMap.entrySet().iterator(); while(headIterator.hasNext()){ Entry<String, Object> nextEntry = headIterator.next(); cellHeader = row.createCell(headCellIndex); cellHeader.setCellStyle(headStyle); cellHeader.setCellValue(new XSSFRichTextString(StringUtils.isBlank((String) nextEntry.getValue())?"":String.valueOf(nextEntry.getValue()))); headCellIndex++; } // 遍歷集合資料,產生資料行 int resultRowIndex = headRowIndex+1; int resultCellIndex = 0; XSSFCell cell; for (Map<String, Object> resultMap : resultOrderList) { row = sheet.createRow(resultRowIndex); headIterator = headOrderMap.entrySet().iterator(); while(headIterator.hasNext()){ String cellValue = String.valueOf(resultMap.get(headIterator.next().getKey())); cell = row.createCell(resultCellIndex); cell.setCellStyle(resultStyle); if(StringUtils.isNotBlank(cellValue) && "null"!=cellValue){ cell.setCellValue(new XSSFRichTextString(cellValue)); } resultCellIndex++; } resultCellIndex=0; resultRowIndex++; } // 將Excel傳送到客戶端 workBookWrite(os, workbook); } private static void setColumnWidth(Sheet sheet, Integer[] columnWidths) { for (int i = 0; i < columnWidths.length; i++) { sheet.setColumnWidth(i, 256 * columnWidths[i] + 184); } } /** * 設定頭部樣式 * @param headStyle 頭部樣式 */ private static void setHeadCellStyle(CellStyle headStyle) { headStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headStyle.setBorderBottom(BorderStyle.THIN); headStyle.setBorderLeft(BorderStyle.THIN); headStyle.setBorderRight(BorderStyle.THIN); headStyle.setBorderTop(BorderStyle.THIN); headStyle.setAlignment(HorizontalAlignment.CENTER); headStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直 } /** * 設定資料行樣式 * @param resultStyle 資料行樣式 */ private static void setResultCellStyle(CellStyle resultStyle) { resultStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); resultStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); resultStyle.setBorderBottom(BorderStyle.THIN); resultStyle.setBorderLeft(BorderStyle.THIN); resultStyle.setBorderRight(BorderStyle.THIN); resultStyle.setBorderTop(BorderStyle.THIN); resultStyle.setAlignment(HorizontalAlignment.CENTER); resultStyle.setVerticalAlignment(VerticalAlignment.CENTER); } /** * 將Excel輸出到客戶端 * @param os 輸出流 * @param workbook 工作簿 */ private static void workBookWrite(OutputStream os, Workbook workbook) { try { workbook.write(os); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(workbook); IOUtils.closeQuietly(os); } } }