Java 读取excel无规则数据
时间: 2023-09-24 16:05:35 浏览: 93
Java读取Excel无规则数据可以使用Apache POI库。首先,需要导入以下依赖:
```
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
```
然后,可以使用以下代码来读取Excel文件:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadExcel {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("data.xlsx");
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
CellType cellType = cell.getCellType();
if (cellType == CellType.STRING) {
System.out.print(cell.getStringCellValue() + "\t");
} else if (cellType == CellType.NUMERIC) {
System.out.print(cell.getNumericCellValue() + "\t");
} else if (cellType == CellType.BOOLEAN) {
System.out.print(cell.getBooleanCellValue() + "\t");
} else if (cellType == CellType.BLANK) {
System.out.print(" " + "\t");
}
}
System.out.println();
}
workbook.close();
inputStream.close();
}
}
```
这个代码可以读取Excel表格中的所有数据,无论是有规则还是无规则的数据。
阅读全文