public class first {
public static void main(String[] args) throws IOException {
String pathname = "C:\\Users\\1\\Desktop\\22.xlsx";// 文件路径
File file = new File(pathname);
Workbook workbook = null;
Sheet sheet = null;
if (pathname.endsWith(".xls")) {
workbook = new HSSFWorkbook(new FileInputStream(file));
}
// 2007以后版本
if (pathname.endsWith(".xlsx")) {
workbook = new XSSFWorkbook(new FileInputStream(file));
}
sheet = workbook.getSheetAt(0);
print(sheet);
}
public static void print(Sheet sheet) {
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(String.valueOf(cell.getDateCellValue()));
} else {
System.out.print(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
default:
}
}
System.out.println();
}
}
}
POI的相关jar包