使用poi讀取xls、xlsx檔案
阿新 • • 發佈:2019-01-08
首先匯入jar包:
commons-collections 4-4.1jar
poi 3.9.jar --核心jar包;
poi-ooxml.3.9.jar --支援xlsx讀取
poi-ooxml-schemas 3.9.jar
xmlbeans-2.6.0.jar
maven配置:
讀取excel工具類:
package com.feosa.common.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Header; 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; public class ExcelPoiReaderUtils { /** * 讀取excel 第1張sheet (xls和xlsx) * * @param filePath excel路徑 * @param columns 列名(表頭) * @author lizixiang ,2018-05-08 * @return */ public List<Map<String, String>> readExcel(String filePath,String columns[]) { Sheet sheet = null; Row row = null; Row rowHeader = null; List<Map<String, String>> list = null; String cellData = null; Workbook wb = null; if (filePath == null) { return null; } String extString = filePath.substring(filePath.lastIndexOf(".")); InputStream is = null; try { is = new FileInputStream(filePath); if (".xls".equals(extString)) { wb = new HSSFWorkbook(is); } else if (".xlsx".equals(extString)) { wb = new XSSFWorkbook(is); } else { wb = null; } if (wb != null) { // 用來存放表中資料 list = new ArrayList<Map<String, String>>(); // 獲取第一個sheet sheet = wb.getSheetAt(0); // 獲取最大行數 int rownum = sheet.getPhysicalNumberOfRows(); // 獲取第一行 rowHeader = sheet.getRow(0); row = sheet.getRow(0); // 獲取最大列數 int colnum = row.getPhysicalNumberOfCells(); for (int i = 1; i < rownum; i++) { Map<String, String> map = new LinkedHashMap<String, String>(); row = sheet.getRow(i); if (row != null) { for (int j = 0; j < colnum; j++) { if(columns[j].equals(getCellFormatValue(rowHeader.getCell(j)))){ cellData = (String) getCellFormatValue(row .getCell(j)); map.put(columns[j], cellData); } } } else { break; } list.add(map); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return list; } /** 獲取單個單元格資料 * @param cell * @return * @author lizixiang ,2018-05-08 */ public Object getCellFormatValue(Cell cell) { Object cellValue = null; if (cell != null) { // 判斷cell型別 switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: { cellValue = String.valueOf(cell.getNumericCellValue()); break; } case Cell.CELL_TYPE_FORMULA: { // 判斷cell是否為日期格式 if (DateUtil.isCellDateFormatted(cell)) { // 轉換為日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); } else { // 數字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING: { cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } } else { cellValue = ""; } return cellValue; } }
測試類:
public static void main(String[] args) { String filePath = "D:/tools/託管檔案.xls"; String columns[] = {"賬戶名稱","開戶時間","證券帳號","開戶機構"}; List<Map<String, String>> list = new ExcelPoiReaderUtils().readExcel(filePath, columns); //遍歷解析出來的list for (Map<String,String> map : list) { for (Entry<String,String> entry : map.entrySet()) { System.out.print(entry.getKey()+":"+entry.getValue()+","); } System.out.println(); } }
excel檔案: