0

I have data in a excel sheet with 10+ columns. I wants to copy particular column data into new sheet or display it on console. i have taken one column name into a variable and checking that with each cell in Headers row if it matches copy that that column data to new sheet or display it on cursor. I tried with following code but not getting expected output. Please help me.

Code:

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Read {
     public static void main(String[] args) throws Exception{
         Read read = new Read();
         String x ="Address";
        int columnIndex = 0;
        read.extractExcelContentByColumnIndex(columnIndex,x);
     }


     ArrayList<String> extractExcelContentByColumnIndex(int columnIndex, String colName)
     {
            ArrayList<String> columndata = null;
            String x ="Address";
            try {
                File f = new File("abc.xlsx");
                FileInputStream ios = new FileInputStream(f);
                XSSFWorkbook workbook = new XSSFWorkbook(ios);
                XSSFSheet sheet = workbook.getSheetAt(0);
                Iterator<Row> rowIterator = sheet.iterator();
                columndata =  new ArrayList<String>();



                while (rowIterator.hasNext()) {

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

                          Cell cell = cellIterator.next();

                          /*String  temp=cell.getStringCellValue();
                          System.out.println(temp);*/

                        if(row.getRowNum() > 0){ //To filter column headings
                            if(cell.getColumnIndex() == columnIndex){// To match column index
                                switch (cell.getCellType()) {
                                case Cell.CELL_TYPE_NUMERIC:

                                    columndata.add(cell.getNumericCellValue()+"");
                                    break;
                                case Cell.CELL_TYPE_STRING:

                                    columndata.add(cell.getStringCellValue());
                                    break;

                                }

                            }
                        }
                    }
                }
                ios.close();
                System.out.println(columndata);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return columndata;
        }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
aaa
  • 69
  • 2
  • 14

1 Answers1

0

The code is fine but you are resolving the desired column by index, not name; Either make sure you pass the right column index in parameter or use the column name to resolve column index and keep same code.

Elgayed
  • 1,129
  • 9
  • 16