Apache POI XSSF 读取 excel 文件 [英] Apache POI XSSF reading in excel files

查看:65
本文介绍了Apache POI XSSF 读取 excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个关于如何使用 Apache 的 XSSF 格式读取 xlsx 文件的快速问题.

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)

...导入所有相关的东西.我的问题是,当我点击运行时,它卡在第 (2) 行,几乎是无限循环.filename 只是一个字符串.

...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.

如果有人能给我一些关于如何解决这个问题的示例代码,我将不胜感激.我现在想要的只是从 xlsx 文件中读取单个单元格;我对 xls 文件使用 HSSF,没有任何问题.

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.

感谢您的帮助,安德鲁

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆