java hssfworkbook 读取excel
时间: 2023-07-07 19:23:26 浏览: 113
可以使用以下代码来读取 Excel 文件:
```java
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class ReadExcelFile {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream("excelFile.xls");
// Create Workbook instance holding reference to .xls file
HSSFWorkbook workbook = new HSSFWorkbook(file);
// Get first/desired sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
// Iterate through each rows one by one
for (Row row : sheet) {
// Iterate through each cells one by one
for (Cell cell : row) {
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + " ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + " ");
break;
}
}
System.out.println();
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个示例代码使用了 Apache POI 库来读取 Excel 文件。请确保将 `excelFile.xls` 替换为您想要读取的实际文件名。
阅读全文