Nads707
Nads707

Reputation: 1

Read particular columns in excel in java

I have an excel and i need to read only first and third column from it. I have wrote the following code to read the whole file. Kindly suggest how to read paticular columns from this.

        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();

Upvotes: 0

Views: 62

Answers (1)

Rahim Dastar
Rahim Dastar

Reputation: 1269

You can use this method to get the specific cell in each row:

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

Upvotes: 1

Related Questions