Apache的POI XSSF读取Excel文件 [英] Apache POI XSSF reading in excel files
问题描述
我只对如何使用从Apache XSSF格式的XLSX文件中读取一个快速的问题。
现在我的code是这样的:
的InputStream FS =新的FileInputStream(文件名); //(1)
XSSFWorkbook WB =新XSSFWorkbook(FS); //(2)
XSSFSheet片= wb.getSheetAt(0); //(3)
...进口所有相关的东西。我的问题是,当我打来看,它卡住在行(2),几乎在一个无限循环。 文件名
只是一个字符串。
如果有人可以给我如何解决这个问题我真的AP preciate它的一些样本code。所有我现在想的是从XLSX文件的单个单元格阅读;我使用的XLS文件HSSF并没有任何问题。
感谢您的帮助,
安德鲁
的InputStream INP = NULL;
尝试{
INP =新的FileInputStream(E:/sample_poi.xls); 工作簿WB = WorkbookFactory.create(INP);
苫布苫布= wb.getSheetAt(0);
标题标题= sheet.getHeader(); INT rowsCount = sheet.getLastRowNum();
的System.out.println(行总数:+(rowsCount + 1));
的for(int i = 0; I< = rowsCount;我++){
鳞次栉比= sheet.getRow(I)
INT colCounts = row.getLastCellNum();
的System.out.println(COLs的总人数:+ colCounts);
对于(INT J = 0; J< colCounts; J ++){
电池单元= row.getCell(J);
的System.out.println([+ I +,+ J +] =+ cell.getStringCellValue());
}
} }赶上(例外前){
。java.util.logging.Logger.getLogger(FieldController.class.getName())日志(Level.SEVERE,空,前);
} {最后
尝试{
inp.close();
}赶上(IOException异常前){
。java.util.logging.Logger.getLogger(FieldController.class.getName())日志(Level.SEVERE,空,前);
}
}
I just have a quick question about how to read in an xlsx file using the XSSF format from Apache.
Right now my code looks like this:
InputStream fs = new FileInputStream(filename); // (1)
XSSFWorkbook wb = new XSSFWorkbook(fs); // (2)
XSSFSheet sheet = wb.getSheetAt(0); // (3)
...with all the relevant things imported. My problem is that when I hit run, it gets stuck at line (2), in almost an infinite loop. filename
is just a string.
If anybody could give me some sample code on how to fix this I would really appreciate it. All i want right now is to read in a single cell from an xlsx file; I was using HSSF for xls files and had no problems.
Thanks for your help, Andrew
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);
}
}
这篇关于Apache的POI XSSF读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!