在實際項目中,用到的導入導出excel太多了,並且對於web管理系統更爲常見,所以封裝了導入導出的工具類。代碼中依賴了slf4j日誌包,commons-io包的IOUtils關閉流,commons-lang和commons-collections包等包。java
1. 導出Excel封裝的工具類:ExcelExporter
在實際中導出excel很是常見,因而本身封裝了一個導出數據到excel的工具類,先附上代碼,最後會寫出實例和解釋。支持03和07兩個版本的 excel。web
HSSF導出的是xls的excel,XSSF導出的是xlsx的excel,SXSSF導出的也是xlsx的excel,只不過這個用於處理數據量大的狀況,生成文件以後數據不會留在內存中。數據庫
package cn.qs.utils.export; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ArrayUtils; 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.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ExcelExporter { public static enum OfficeVersion { OFFICE_03, OFFICE_07; } private static final Logger LOGGER = LoggerFactory.getLogger(ExcelExporter.class); private String[] headerNames; private Workbook workBook; private Sheet sheet; /** * * @param headerNames * 表頭 * @param sheetName * sheet的名稱 * @param excelVerson * excel的版本 */ public ExcelExporter(String[] headerNames, String sheetName, OfficeVersion officeVersion) { this.headerNames = headerNames; // 建立一個工做簿 if (OfficeVersion.OFFICE_07.equals(officeVersion)) { // workBook = new XSSFWorkbook();//處理07版本excel workBook = new SXSSFWorkbook();// 處理07版本,可是適用於大數據量,導出以後數據不會佔用內存 } else if (OfficeVersion.OFFICE_03.equals(officeVersion)) { workBook = new HSSFWorkbook(); } // 建立一個工做表sheet sheet = workBook.createSheet(sheetName); initHeader(); } /** * 初始化表頭信息 */ private void initHeader() { // 建立第一行 Row row = sheet.createRow(0); Cell cell = null; // 建立表頭 for (int i = 0; i < headerNames.length; i++) { cell = row.createCell(i); cell.setCellValue(headerNames[i]); setCellStyle(cell); } } /** * 設置單元格樣式 * * @param cell * 單元格 */ public void setCellStyle(Cell cell) { // 設置樣式 CellStyle cellStyle = workBook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 設置字體居中 // 設置字體 Font font = workBook.createFont(); font.setFontName("宋體"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字體加粗 font.setFontHeightInPoints((short) 13); cellStyle.setFont(font); cell.setCellStyle(cellStyle); } /** * 建立行內容(每一行的數據裝在list中) * * @param datas * 每一行的數據 * @param rowIndex * 行號(從1開始) */ public void createTableRow(List<String> datas, int rowIndex) { // 建立第i行 Row row = sheet.createRow(rowIndex); Cell cell = null; // 寫入數據 for (int index = 0, length = datas.size(); index < length; index++) { // 參數表明第幾列 cell = row.createCell(index); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(datas.get(index)); } } /** * * @param datas * 數據,每個map都是一行 * @param keys * key[i]表明從map中獲取keys[i]的值做爲第i列的值,若是傳的是null默認取表頭 */ public void createTableRows(List<Map<String, Object>> datas, String[] keys) { for (int i = 0, length_1 = datas.size(); i < length_1; i++) { if (ArrayUtils.isEmpty(keys)) { keys = headerNames; } // 建立行(從第二行開始) Map<String, Object> data = datas.get(i); Row row = sheet.createRow(i + 1); Cell cell = null; for (int j = 0, length_2 = keys.length; j < length_2; j++) { // 單元格獲取map中的key String key = keys[j]; String value = MapUtils.getString(data, key, ""); cell = row.createCell(j); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(value); } } } /** * 根據表頭自動調整列寬度 */ public void autoAllSizeColumn() { if (sheet instanceof SXSSFSheet) {// 若是是SXSSFSheet,須要調用trackAllColumnsForAutoSizing方法一次 SXSSFSheet tmpSheet = (SXSSFSheet) sheet; tmpSheet.trackAllColumnsForAutoSizing(); } for (int i = 0, length = headerNames.length; i < length; i++) { sheet.autoSizeColumn(i); } } /** * 將數據寫出到excel中 * * @param outputStream */ public void exportExcel(OutputStream outputStream) { // 導出以前先自動設置列寬 this.autoAllSizeColumn(); try { workBook.write(outputStream); } catch (IOException e) { LOGGER.error(" exportExcel error", e); } finally { IOUtils.closeQuietly(outputStream); } } /** * 合併單元格(起始行列都包括在裏面) * * @param startRow * 起始行 * @param endRow * 結束行 * @param startCol * 起始列 * @param endCol * 結束列 */ public void mergeCell(int startRow, int endRow, int startCol, int endCol) { CellRangeAddress region = new CellRangeAddress(startRow, endRow, startCol, endCol); sheet.addMergedRegion(region); } /** * 將數據寫出到excel中 * * @param outputFilePath */ public void exportExcel(String outputFilePath) { // 導出以前先自動設置列寬 this.autoAllSizeColumn(); FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(outputFilePath); workBook.write(outputStream); } catch (IOException e) { LOGGER.error(" exportExcel error", e); } finally { IOUtils.closeQuietly(outputStream); } } public static void main(String[] args) { test2(); } private static void test2() { ExcelExporter hssfWorkExcel = new ExcelExporter(new String[] { "姓名", "年齡" }, "人員基本信息", OfficeVersion.OFFICE_03); List<Map<String, Object>> datas = new ArrayList<>(); for (int i = 0; i < 10; i++) { Map data = new HashMap<>(); data.put("name", "tttttttttttttt" + i); data.put("age", "age" + i); datas.add(data); } hssfWorkExcel.createTableRows(datas, new String[] { "name", "age" }); hssfWorkExcel.mergeCell(1, 2, 0, 1); try { hssfWorkExcel.exportExcel(new FileOutputStream(new File("e:/test1.xls"))); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
上面的代碼邏輯很是簡單,建立實例的時候就初始化表頭信息和建立sheet。apache
向excel中填充數據的方式有兩種,就是上面的createTableRow方法和createTableRows方法。數組
createTableRow(List,int)方法就是屢次調用此方法,list中數據,int是行號。list中數據依次做爲第i行的列數據。xss
createTableRows(List<Map>,String[])這個方法應該是很是經常使用的一種方式。咱們從數據庫查詢到的數據大多數映射爲Map放入list中,所以上面的方法就比較經常使用。List<Map>參數就是全部的數據,一個Map表明一行,String[]是Map中的key,也就是數組的第一個元素對應的key做爲第一列,第二個元素是做爲第二列。若是map數據的key正好與表頭一致咱們能夠傳一個null。(由於Map是基於數組+鏈表,且存入的是無序的,因此沒法直接經過map中的key肯定列。除非傳的數據是LinkedHashMap)。這個方法封裝的比較好,若是一條數據中沒有對應的值會將此單元格設爲空。工具
下面是本身測試代碼:測試
(1)測試List<String>寫入數據,導出03版本的excel字體
package cn.qs.utils.export; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; public class FileTest { public static void main(String[] args) { ExcelExporter hssfWorkExcel = new ExcelExporter(new String[] { "姓名", "年齡" }, "人員基本信息", ExcelExporter.OfficeVersion.OFFICE_03); for (int i = 0; i < 10; i++) { List<String> data = new ArrayList<>(); data.add("namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss" + i); data.add("" + (i + 20)); hssfWorkExcel.createTableRow(data, i + 1); } try { hssfWorkExcel.exportExcel(new FileOutputStream(new File("e:/test.xls"))); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
結果:大數據
(2)測試List<Map>寫入數據,導出07版本的excel
package cn.qs.utils.export; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; public class FileTest { public static void main(String[] args) { ExcelExporter hssfWorkExcel = new ExcelExporter(new String[] { "姓名", "年齡" }, "人員基本信息", ExcelExporter.OfficeVersion.OFFICE_07); for (int i = 0; i < 10; i++) { List<String> data = new ArrayList<>(); data.add("namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss" + i); data.add("" + (i + 20)); hssfWorkExcel.createTableRow(data, i + 1); } try { hssfWorkExcel.exportExcel(new FileOutputStream(new File("e:/test.xlsx"))); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
結果:
2.讀取Excel封裝的工具類:ExcelReader
此工具類能夠讀取指定sheet的數據,能夠指定每一列對應的header,若是不傳默認以第一行做爲head;也能夠讀取全部sheet的數據,以全部sheet的第一行做爲head。最終的返回值是List<Map>類型,map的key是表頭header。
代碼:
package cn.qs.utils.export; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.text.NumberFormat; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ExcelReader { private static final Logger LOGGER = LoggerFactory.getLogger(ExcelReader.class); private Workbook workBook; private Map<Sheet, String[]> sheetHeaders; public ExcelReader(String filePath) { this(new File(filePath)); } public ExcelReader(File file) { // 解決版本問題,HSSFWorkbook是97-03版本的xls版本,XSSFWorkbook是07版本的xlsx try { workBook = new XSSFWorkbook(new FileInputStream(file)); LOGGER.debug("是03版本的excel,採用XSSFWorkbook讀取"); } catch (Exception e) { try { workBook = new HSSFWorkbook(new FileInputStream(file)); LOGGER.debug("是07版本的excel,採用HSSFWorkbook讀取"); } catch (Exception e1) { LOGGER.error("Excel格式不正確", e1); throw new RuntimeException(e1); } } } public ExcelReader(InputStream inputStream) { // 解決版本問題,HSSFWorkbook是97-03版本的xls版本,XSSFWorkbook是07版本的xlsx try { workBook = new XSSFWorkbook(inputStream); LOGGER.debug("是03版本的excel,採用XSSFWorkbook讀取"); } catch (Exception e) { try { workBook = new HSSFWorkbook(inputStream); LOGGER.debug("是07版本的excel,採用HSSFWorkbook讀取"); } catch (Exception e1) { LOGGER.error("Excel格式不正確", e1); throw new RuntimeException(e1); } } } /** * 初始化sheet和表頭信息,默認以每一個sheet的第一行做爲表頭 */ private void initDefaultSheetHeaders() { sheetHeaders = new LinkedHashMap<>(); if (workBook == null) { return; } int numberOfSheets = workBook.getNumberOfSheets(); for (int i = 0; i < numberOfSheets; i++) { Sheet sheet = workBook.getSheetAt(i); String sheetName = workBook.getSheetName(i); LOGGER.debug("sheetName[{}]: {}", i, sheetName); // 默認以第一行做爲表頭 Row row = sheet.getRow(0); if (row == null) { sheetHeaders.put(sheet, new String[] {}); continue; } String[] headers = new String[] {}; short lastCellNum = row.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { String cellValue = getCellValue(row.getCell(j)); headers = (String[]) ArrayUtils.add(headers, cellValue); } sheetHeaders.put(sheet, headers); } } public List<Map<String, Object>> readAllSheetDatas() { List<Map<String, Object>> result = new LinkedList<>(); int numberOfSheets = workBook.getNumberOfSheets(); for (int i = 0; i < numberOfSheets; i++) { List<Map<String, Object>> datas = readSheetDatas(i); if (CollectionUtils.isNotEmpty(datas)) { result.addAll(datas); } } return result; } public List<Map<String, Object>> readSheetDatas(int sheetIndex) { if (sheetHeaders == null) { initDefaultSheetHeaders(); } Sheet sheet = workBook.getSheetAt(sheetIndex); return readSheetDatas(sheetIndex, sheetHeaders.get(sheet), 1); } public List<Map<String, Object>> readSheetDatas(int sheetIndex, String[] headers) { return readSheetDatas(sheetIndex, headers, 0); } /** * 讀取指定sheet數據 * * @param sheetIndex * sheet的下標 * @param headers * 表頭 * @param startRow * 起始行數 * @return */ public List<Map<String, Object>> readSheetDatas(int sheetIndex, String[] headers, int startRow) { List<Map<String, Object>> result = new LinkedList<>(); if (ArrayUtils.isEmpty(headers)) { return result; } Sheet sheet = workBook.getSheetAt(sheetIndex); if (sheet == null) { return result; } int lastRowNum = sheet.getLastRowNum(); for (int i = startRow; i <= lastRowNum; i++) { Row row = sheet.getRow(i); if (row == null) { continue; } Map<String, Object> rowMap = new LinkedHashMap<>(); result.add(rowMap); for (int j = 0, length_1 = headers.length; j < length_1; j++) { String cellValue = getCellStringValue(row.getCell(j)); String header = headers[j]; rowMap.put(header, cellValue); } } return result; } /** * 讀取cell的值 * * @param cell * 須要讀取的cell * @param defaultValue * 默認值 * @return */ private String getCellValue(Cell cell) { if (cell == null) { return ""; } cell.setCellType(CellType.STRING); return StringUtils.defaultIfBlank(cell.getStringCellValue(), ""); } /** * POI3.15以後的讀取方法(建議用這個) * * @param cell * @return */ private String getCellStringValue(Cell cell) { if (cell == null) { return ""; } String cellValue = null; if (cell.getCellTypeEnum() == CellType.NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { cellValue = DateFormatUtils.format(cell.getDateCellValue(), "yyyy-MM-dd"); } else { NumberFormat nf = NumberFormat.getInstance(); cellValue = String.valueOf(nf.format(cell.getNumericCellValue())).replace(",", ""); } } else if (cell.getCellTypeEnum() == CellType.STRING) { cellValue = cell.getStringCellValue(); } else if (cell.getCellTypeEnum() == CellType.BOOLEAN) { cellValue = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellTypeEnum() == CellType.ERROR) { cellValue = "錯誤類型"; } else { cellValue = ""; } return cellValue; } }
測試代碼:
package cn.qs.utils.export; import java.util.List; import java.util.Map; public class FileTest { public static void main(String[] args) { ExcelReader excelExporter = new ExcelReader("e:/test.xls"); System.out.println("==========讀取全部sheet數據,默認以第一行做爲header=========="); List<Map<String, Object>> readAllSheetDatas2 = excelExporter.readAllSheetDatas(); System.out.println(readAllSheetDatas2); } }
結果:
19:44:44.003 [main] DEBUG cn.qs.utils.export.ExcelReader - 是07版本的excel,採用HSSFWorkbook讀取==========讀取第一個sheet(指定表頭和起始行)==========[{name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss0, age=20}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss1, age=21}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss2, age=22}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss3, age=23}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss4, age=24}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss5, age=25}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss6, age=26}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss7, age=27}, {name=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss8, age=28}]==========讀取全部sheet數據,默認以第一行做爲header==========19:44:44.013 [main] DEBUG cn.qs.utils.export.ExcelReader - sheetName[0]: 人員基本信息[{姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss0, 年齡=20}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss1, 年齡=21}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss2, 年齡=22}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss3, 年齡=23}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss4, 年齡=24}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss5, 年齡=25}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss6, 年齡=26}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss7, 年齡=27}, {姓名=namesssssssssssssss水水水水水水水水水水水水水水水水水水水ssssssssssssssss8, 年齡=28}]