POI是一个apache开源的jar包,可以通过搜索 java POI找到官网,并下载开发包.
包含的功能:
可以读取excel2003,2007,2010等。
读取excel2007/2010的代码:
public static boolean isNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
} public static List<List<String>> readXlsx(String path) throws Exception {
InputStream is = new FileInputStream(path);
@SuppressWarnings("resource")
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
List<List<String>> result = new ArrayList<>(); for (int sheetIx = 0; sheetIx < xssfWorkbook.getNumberOfSheets(); sheetIx++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(sheetIx);
if (xssfSheet == null)
continue; for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum); int minColIx = xssfRow.getFirstCellNum();
int maxColIx = xssfRow.getLastCellNum();
List<String> rowList = new ArrayList<>();
for (int colIx = minColIx; colIx < maxColIx; colIx++) {
XSSFCell cell = xssfRow.getCell(colIx);
if (cell == null)
continue;
rowList.add(cell.toString());
}
result.add(rowList);
}
}
return result;
}