需要的jar包:
poi-3.7-20101029.jarpoi-ooxml-3.7-20101029.jar
poi-ooxml-schemas-3.7-20101029.jar
commons-io-2.5.jar
xmlbeans-2.3.0.jar
dom4j-1.6.1.jar
对office2003和2007分别处理(即对xls和xlsx分别处理)
// Excel 2003
private final static String XLS = "xls";
// Excel 2007
private final static String XLSX = "xlsx";
根据Excel路径获取Workbook:
public static Workbook getWorkbook(String path) throws Exception {
Workbook workbook = null;
File file = new File(path);
FileInputStream fis = null;
String extensionName = FilenameUtils.getExtension(file.getName());
fis = new FileInputStream(file);
if (extensionName.toLowerCase().equals(XLS)) {
workbook = new HSSFWorkbook(fis);
} else if (extensionName.toLowerCase().equals(XLSX)) {
workbook = new XSSFWorkbook(fis);
} else {
workbook = null;
}
return workbook;
}
获取Workbookl中的Sheet数量:
public static int getNumberOfSheets(Workbook workbook) {
int count = 0;
if (null != workbook) {
count = workbook.getNumberOfSheets();
}
return count;
}
获取Sheet的名称集合并存储在List集合中:
public static List<String> getNameOfSheets(Workbook workbook) {
List<String> list = new ArrayList<String>();
if (null != workbook) {
int count = getNumberOfSheets(workbook);
for (int i = 0; i < count; i++) {
list.add(workbook.getSheetName(i));
}
}
return list;
}
获取Sheet集合数组:
public static Sheet[] getSheets(Workbook workbook) {
Sheet[] sheets = null;
if (null != workbook) {
int count = getNumberOfSheets(workbook);
if (count > 0) {
sheets = new Sheet[count];
for (int i = 0; i < count; i++) {
sheets[i] = workbook.getSheetAt(i);
}
}
}
return sheets;
}
获取Sheet的行数据并以字符串的格式保存到List集合中:
public static List<String> getRowToList(Sheet sheet, int index) {
List<String> list = new ArrayList<String>();
if (null != sheet) {
// 第一行索引(从0开始)
int minNumOfRows = sheet.getFirstRowNum();
// 最后一行索引
int maxNumOfRows = sheet.getLastRowNum();
if (index >= minNumOfRows && index <= maxNumOfRows) {
Row row = sheet.getRow(index);
// 第一列索引
int minNumCells = row.getFirstCellNum();
// 最后一列索引
int maxNumCells = row.getLastCellNum();
Cell cell = null;
// 格式化日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
// 格式化数字(去除小数点后无用的0)
NumberFormat nf = NumberFormat.getInstance();
for (int i = minNumCells; i < maxNumCells; i++) {
cell = row.getCell(i);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:// 布尔类型
boolean t = cell.getBooleanCellValue();
if (t) {// true--->1
list.add(String.valueOf(1));
} else {// false--->0
list.add(String.valueOf(0));
}
break;
case Cell.CELL_TYPE_NUMERIC: // 数字类型(包括日期类型)
// 这里的日期类型会被转换为数字类型,需要判别后区分处理
if (DateUtil.isCellDateFormatted(cell)) {
list.add(String.valueOf(format.format(cell.getDateCellValue())));
} else {
list.add(String.valueOf(nf.format(cell.getNumericCellValue())));
}
break;
case Cell.CELL_TYPE_STRING: // 字符串类型
list.add(cell.getStringCellValue());
break;
default: // 其他类型
list.add("");
break;
}
}
}
}
return list;
}
获取Sheet中指定行数据到Map中(key为Sheet第一行数据):
public static Map<String, String> getRowToMap(Sheet sheet, int index) {
Map<String, String> map = null;
if (null != sheet) {
int minNumOfRows = sheet.getFirstRowNum();
int maxNumOfRows = sheet.getLastRowNum();
List<String> header = getRowToList(sheet, minNumOfRows);
int minNumCells = sheet.getRow(minNumOfRows).getFirstCellNum();
int maxNumCells = sheet.getRow(minNumOfRows).getLastCellNum();
index = minNumOfRows + index + 1;// 不算第一行
if (index >= minNumOfRows && index <= maxNumOfRows) {
map = new HashMap<String, String>();
// 格式化日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
// 格式化数字(去除小数点后无用的0)
NumberFormat nf = NumberFormat.getInstance();
Cell cell = null;
String headerCellValue = null;
// 不含标题行(minNumOfRows + 1)
Row row = sheet.getRow(index);
map = new HashMap<String, String>();
for (int j = minNumCells; j < maxNumCells; j++) {
cell = row.getCell(j);
headerCellValue = header.get(j);
if (null != cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:// 布尔类型
boolean t = cell.getBooleanCellValue();
if (t) {// true--->1
map.put(headerCellValue, "1");
} else {// false--->0
map.put(headerCellValue, "0");
}
break;
case Cell.CELL_TYPE_NUMERIC:
// 这里的日期类型会被转换为数字类型,需要判别后区分处理
if (DateUtil.isCellDateFormatted(cell)) {
map.put(headerCellValue, String.valueOf(format.format(cell.getDateCellValue())));
} else {
map.put(headerCellValue, String.valueOf(nf.format(cell.getNumericCellValue())));
}
break;
case Cell.CELL_TYPE_STRING:
map.put(headerCellValue, cell.getStringCellValue());
break;
default:
map.put(headerCellValue, "");
break;
}
}
}
}
}
return map;
}
获取Sheet数据到List<Map<String,String>>中:
public static List<Map<String, String>> getData(Sheet sheet) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
if (null != sheet) {
Map<String, String> map = null;
int minNumOfRows = sheet.getFirstRowNum();
int maxNumOfRows = sheet.getLastRowNum();
List<String> rowData = null, firstRowData = getRowToList(sheet, minNumOfRows);
int firstCellSize = firstRowData.size(), cellSize = 0;
// 不含标题行(minNumOfRows + 1)
for (int i = minNumOfRows + 1; i <= maxNumOfRows; i++) {
map = new HashMap<String, String>();
rowData = getRowToList(sheet, i);
cellSize = rowData.size();
for (int j = 0; j < cellSize && j < firstCellSize; j++) {
if (j >= cellSize) {
map.put(firstRowData.get(j), "");
} else if (j >= firstCellSize) {
map.put(String.valueOf(j), rowData.get(j));
} else {
map.put(firstRowData.get(j), rowData.get(j));
}
}
list.add(map);
}
}
return list;
}
测试主程序:
public static void main(String[] args) throws Exception {
Workbook workbook = getWorkbook("d://ceshi.xlsx");
// 获取sheet数量
int sheetNum = getNumberOfSheets(workbook);
System.out.println("工作薄中共有Sheet:" + sheetNum + "个。");
List<String> sheetNames = getNameOfSheets(workbook);
System.out.println("工作薄中Sheet名称:" + sheetNames.toString());
Sheet sheet = workbook.getSheetAt(0);
List<String> rowList = getRowToList(sheet, 10);
System.out.println("第一个Sheet中的第一行数据:" + rowList.toString());
System.out.println("第一个Sheet中的所有数据:");
List<Map<String, String>> list = getData(sheet);
for (Map<String, String> map : list) {
System.out.println(map.toString());
}
}
Excel表格中数据:
程序测试结果: