在Java中阅读Excel中的特定列

时间:2018-10-08 11:59:08

标签: 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();

1 个答案:

答案 0 :(得分:1)

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

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