java - 用java读取excel中的特定列

标签 java excel apache-poi

我有一个 Excel,我只需要读取其中的第一列和第三列。我编写了以下代码来读取整个文件。请建议如何阅读其中的特定专栏。

        FileInputStream file = new FileInputStream(new File("D:/FinacleCredit/ecpix.xlsx"));
                XSSFWorkbook workbook = new XSSFWorkbook(file);
                Iterator<Row> rowIterator = sheet.iterator();
                DataFormatter dataFormatter = new DataFormatter();
                ArrayList<String> columndata = new ArrayList<String>();

                while (rowIterator.hasNext())
                {
                    Row row = rowIterator.next();
                    Iterator<Cell> cellIterator = row.cellIterator();

                    while (cellIterator.hasNext())
                    {
                         Cell cell = cellIterator.next();

                        String cellValue = dataFormatter.formatCellValue(cell);
                        System.out.print(cellValue + "\t");

                            switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_NUMERIC:
                                columndata.add(cell.getNumericCellValue()+"");
                                break;
                            case Cell.CELL_TYPE_STRING:
                                columndata.add(cell.getStringCellValue());
                                break;
                         }
                    System.out.println("");
                  }
               }            
              file.close();

最佳答案

您可以使用此方法获取每行中的特定单元格:

Cell firstCell = row.getCell(0);   // read first column
Cell thirdCell = row.getCell(2);   // read third column

关于java - 用java读取excel中的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52701783/

相关文章:

java - 使用apache poi 3.9迭代写入xlsx文档时出现异常

python - 从现有列计算新列

excel - 使用条件显示最常出现的文本

excel - 对于每个循环在 else 部分失败

java - Apache POI - JAVA - 遍历 excel 中的列

Java 保护访问不起作用

java - Sybase 中的 JDBC 事务控制

java - 无法使用 POI 应用特定的单元格数据格式

JAVA-EE7/javax.ws.rs : Injection of EJB in REST-Resource

java - Apache POI 散点图创建