Java reads data in excel (including .xlsx and .xls)
maven dependencies
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.15.0</version>
</dependency>
Method 1: Customize the distinction between .xlsx and .xls
public static Workbook getWorkbook1(String path) throws Exception {
File file = new File(path);
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
Workbook workbook;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (".xls".equals(fileType)) {
workbook = new HSSFWorkbook(inputStream);
} else if (".xlsx".equals(fileType)) {
workbook = new XSSFWorkbook(inputStream);
} else {
throw new Exception("请上传Excel");
}
inputStream.close();
return workbook;
}
Method 2: WorkbookFactory.create + InputStream
public static Workbook getWorkbook2(String path) throws IOException, InvalidFormatException {
InputStream inputStream = new FileInputStream(path);
return WorkbookFactory.create(inputStream);
}
方式3:WorkbookFactory.create + FileInputStream
public static Workbook getWorkbook3(String path) throws IOException, InvalidFormatException {
FileInputStream inputStream = FileUtils.openInputStream(new File(path));
return WorkbookFactory.create(inputStream);
}
Read the value of Cell
public static void getCell(Workbook workbook) throws Exception {
Sheet sheet;
Row row;
Cell cell;
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
sheet = workbook.getSheetAt(i);
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
for (int k = row.getFirstCellNum(); k < row.getLastCellNum(); k++) {
cell = row.getCell(k);
System.out.print(cell + " ");
}
System.out.println();
}
}
workbook.close();
}
UT
@Test
public void getWorkbook2_1() throws Exception {
String xlsx = "D:\\projects\\tutorails_kanomoku\\business-prac\\src\\main\\resources\\input.xlsx";
Workbook workbook2 = ExcelUtils.getWorkbook2(xlsx);
ExcelUtils.getCell(workbook2);
}
@Test
public void getWorkbook2_2() throws Exception {
String xls = "D:\\projects\\tutorails_kanomoku\\business-prac\\src\\main\\resources\\input2.xls";
Workbook workbook2 = ExcelUtils.getWorkbook2(xls);
ExcelUtils.getCell(workbook2);
}
@Test
public void getWorkbook3_1() throws Exception {
String xlsx = "D:\\projects\\tutorails_kanomoku\\business-prac\\src\\main\\resources\\input.xlsx";
Workbook workbook2 = ExcelUtils.getWorkbook3(xlsx);
ExcelUtils.getCell(workbook2);
}
@Test
public void getWorkbook3_2() throws Exception {
String xls = "D:\\projects\\tutorails_kanomoku\\business-prac\\src\\main\\resources\\input2.xls";
Workbook workbook2 = ExcelUtils.getWorkbook3(xls);
ExcelUtils.getCell(workbook2);
}
@Test
public void getWorkbook4_1() throws Exception {
String xlsx = "D:\\projects\\tutorails_kanomoku\\business-prac\\src\\main\\resources\\input.xlsx";
Workbook workbook2 = ExcelUtils.getWorkbook1(xlsx);
ExcelUtils.getCell(workbook2);
}
@Test
public void getWorkbook4_2() throws Exception {
String xls = "D:\\projects\\tutorails_kanomoku\\business-prac\\src\\main\\resources\\input2.xls";
Workbook workbook2 = ExcelUtils.getWorkbook1(xls);
ExcelUtils.getCell(workbook2);
}