java实现excel表格读取数据
时间: 2023-06-04 15:08:50 浏览: 130
java读取excel数据!
4星 · 用户满意度95%
Java可以通过Apache POI库来实现Excel表格读取数据的功能。具体可以参考以下代码:
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader {
public static void main(String[] args) {
String filePath = "test.xlsx";
try (InputStream in = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(in)) {
Sheet sheet = workbook.getSheetAt(0); // 读取第一个Sheet
for (Row row : sheet) { // 遍历每一行
for (Cell cell : row) { // 遍历每一个单元格
System.out.print(cell.toString() + "\t");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文