转https://blog.51cto.com/wangzhichao/1730100
mvn:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
读取xlsx:
public static void testPoiRead(String[] args) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try (Workbook wb = WorkbookFactory.create(new File("example.xlsx"))) {
for (Sheet sheet : wb) {
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(
sdf.format(cell.getDateCellValue()));
}
else {
System.out.print(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.print(cell.getCellFormula());
break;
default:
System.out.print("");
break;
}
System.out.print("\t");
}
System.out.println();
}
}
}
catch (EncryptedDocumentException | InvalidFormatException
| IOException e) {
LOGGER.error("读取文件出错", e);
}
}
问题:读一个20MB的大excel时内存溢出