解析excel
package com.util;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelReader {
private final String filePath;
private final String sheetName;
private Sheet sheet;
public ExcelReader(String filePath, String sheetName) {
this.filePath = filePath;
this.sheetName = sheetName;
this.load();
}
@SuppressWarnings({ "MethodCanBeVariableArityMethod", "Annotation" })
public static void main(String[] args) {
}
public List<Map<Integer, Object>> getSheetDataToList() {
List<Map<Integer, Object>> list = new ArrayList<Map<Integer, Object>>();
int numOfRows = sheet.getLastRowNum() + 1;
for (int i = 0; i < numOfRows; i++) {
Row row = sheet.getRow(i);
Map<Integer, Object> maps = new HashMap<Integer, Object>();
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
maps.put(j, this.getCellValue(cell));
}
}
list.add(maps);
}
return list;
}
public Map<Integer, Map<String, Object>> getSheetDataToMap() {
Map<Integer, Map<String, Object>> maplist = new HashMap<Integer, Map<String, Object>>();
Map<Integer, Object> map = new HashMap<Integer, Object>();
int numOfRows = sheet.getLastRowNum() + 1;
for (int i = 0; i < numOfRows; i++) {
Row row = sheet.getRow(i);
Map<String, Object> maps = new HashMap<String, Object>();
if (row != null) {
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (i == 0) {
map.put(j, this.getCellValue(cell));
} else {
maps.put((String) map.get(j), this.getCellValue(cell));
}
}
}
if (i > 0) {
maplist.put(i - 1, maps);
}
}
return maplist;
}
private void load() {
FileInputStream inStream = null;
try {
inStream = new FileInputStream(new File(filePath));
Workbook workBook = WorkbookFactory.create(inStream);
if (sheetName == null) {
sheet = workBook.getSheetAt(0);
} else {
sheet = workBook.getSheet(sheetName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String getCellValue(Cell cell) {
String cellValue = "";
DataFormatter formatter = new DataFormatter();
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
cellValue = formatter.formatCellValue(cell);
} else {
double value = cell.getNumericCellValue();
int intValue = (int) value;
cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
}
break;
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellValue = String.valueOf(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
default:
cellValue = cell.toString().trim();
break;
}
}
return cellValue.trim();
}
}