[英]Apache POI XSSF reading in excel files
我只是有一個關於如何使用 Apache 的 XSSF 格式讀取 xlsx 文件的快速問題。
現在我的代碼是這樣的:
InputStream fs = new FileInputStream(filename); // (1)
XSSFWorkbook wb = new XSSFWorkbook(fs); // (2)
XSSFSheet sheet = wb.getSheetAt(0); // (3)
...導入了所有相關的東西。 我的問題是,當我點擊運行時,它卡在第 (2) 行,幾乎是無限循環。 filename
只是一個字符串。
如果有人能給我一些關於如何解決這個問題的示例代碼,我將不勝感激。 我現在想要的只是從 xlsx 文件中讀取單個單元格; 我對 xls 文件使用 HSSF,沒有任何問題。
謝謝你的幫助,安德魯
InputStream inp = null;
try {
inp = new FileInputStream("E:/sample_poi.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Header header = sheet.getHeader();
int rowsCount = sheet.getLastRowNum();
System.out.println("Total Number of Rows: " + (rowsCount + 1));
for (int i = 0; i <= rowsCount; i++) {
Row row = sheet.getRow(i);
int colCounts = row.getLastCellNum();
System.out.println("Total Number of Cols: " + colCounts);
for (int j = 0; j < colCounts; j++) {
Cell cell = row.getCell(j);
System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());
}
}
} catch (Exception ex) {
java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
inp.close();
} catch (IOException ex) {
java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
}
}
我相信這會回答你的問題: http ://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook
簡而言之,您的代碼應如下所示:
InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
為什么要將文件分解為 InputStream? XSSFWorkbook 有一個構造函數,它只是將路徑作為字符串。 只需對字符串的路徑進行硬編碼。創建工作簿后,您可以從中創建 XSSFSheets。 然后是 XSSFCells,它最終將允許您讀取單個單元格的內容(單元格基本上基於 x,y 位置)
您可以嘗試以下操作。
private static void readXLSX(String path) throws IOException {
File myFile = new File(path);
FileInputStream fis = new FileInputStream(myFile);
// Finds the workbook instance for XLSX file
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
// Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
// Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = mySheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default :
}
}
System.out.println("");
}
}
這很好用:試試吧
File filename = new File("E:/Test.xlsx");
FileInputStream isr= new FileInputStream(filename);
Workbook book1 = new XSSFWorkbook(isr);
Sheet sheet = book1.getSheetAt(0);
Iterator<Row> rowItr = sheet.rowIterator();
public class ExcelReader{
public String path;
public static FileInputStream fis;
public ExcelReader(){
System.out.println("hello");
}
public ExcelReader(String path){
this.path=path;
fis=new FileInputStream(path);
XSSFWorkbook workbook=new XSSFWorkbook(fis);
XSSFSheet sheet=workbook.getSheet("Sheet1");//name of the sheet
System.out.println(sheet.getSheetName());
System.out.println(sheet.getLastRowNum());
System.out.println(sheet.getRow(2).getCell(3));
}
public static void main(String[] args) throws IOException {
ExcelReader excel=new ExcelReader("path of xlsx");
}
}
我有同樣的錯誤,我剛剛用相同的版本更新了 pom 依賴項。 有效。
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
// Load sheet- Here we are loading first sheet only
XSSFSheet xlSheet = wb.getSheetAt(0);
這里的代碼僅識別第一張工作表 - 在我的 Excel 中,有多張工作表存在因此面臨將sheet2
更改為sheet1(getSheetAt(0))
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.