2

I have an Excel sheet in which i have close to 150 columns. I am writing an utility where in it will fetch a column where the column name is say X. In another thread i saw how to read the XL workbook and the sheet.. Have written following code..

        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator rows = sheet.rowIterator();


        while (rows.hasNext()) 
        {
            HSSFRow row = (HSSFRow) rows.next();
            Iterator cells = row.cellIterator();

            List data = new ArrayList();
            while (cells.hasNext()) 
            {
                HSSFCell cell = (HSSFCell) cells.next();
                data.add(cell);
            }

            sheetData.add(data);
        }
    } 

So how to fetch the exact column from the XL sheet given the column name.. ?

4
  • How do i get data under column X only. i dont want to read through all the columns in XL sheet.. Is there an API to do so? Commented Oct 6, 2012 at 15:52
  • Any reason for Negative value ? what are the rules here? Commented Oct 6, 2012 at 16:02
  • You probably got a down vote for not actually asking a question Commented Oct 6, 2012 at 16:47
  • Please refer to stackoverflow.com/a/60199942/2149194 Commented Feb 13, 2020 at 3:20

4 Answers 4

8

Apache POI API HSSFSheet is row based you need to extract column data by itteration, below link might answer your question:

Extracting data in spreadsheet columns in Apache POI API

code modified to search for a string in row 1 of first worksheet

package projectTest.test;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class Poi {


    public static void main(String[] args) throws Exception {  
    //test file is located in your project path         
    FileInputStream fileIn = new FileInputStream("test.xls");
    //read file 
    POIFSFileSystem fs = new POIFSFileSystem(fileIn); 
    HSSFWorkbook filename = new HSSFWorkbook(fs);
    //open sheet 0 which is first sheet of your worksheet
    HSSFSheet sheet = filename.getSheetAt(0);

    //we will search for column index containing string "Your Column Name" in the row 0 (which is first row of a worksheet
    String columnWanted = "Your Column Name";
    Integer columnNo = null;
    //output all not null values to the list
    List<Cell> cells = new ArrayList<Cell>();

    Row firstRow = sheet.getRow(0);

    for(Cell cell:firstRow){
        if (cell.getStringCellValue().equals(columnWanted)){
            columnNo = cell.getColumnIndex();
        }
    }


    if (columnNo != null){
    for (Row row : sheet) {
       Cell c = row.getCell(columnNo);
       if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
          // Nothing in the cell in this row, skip it
       } else {
          cells.add(c);
       }
    }
    }else{
        System.out.println("could not find column " + columnWanted + " in first row of " + fileIn.toString());
    }

    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes already checked that.But that gives the result given a column number. But how to do it based on column name?
@user1122891 I have modified code to search for a string name of a column (in a 1 row of a 1 worksheet in a workbook)
@MarcinWasiluk : I am getting below exception while executing this piece of code. org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) P.S.=> I am having my data in excel sheet of MS-OFFICE 2013 Any help would be appreciated
0

This is the very simple and efficient code and Working as expected

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class TestExcelFile {

    public static void main(String[] args) {
        String envFilePath = System.getenv("AZURE_FILE_PATH");

        // upload list of files/directory to blob storage
        File folder = new File(envFilePath);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                System.out.println("File " + listOfFiles[i].getName());

                Workbook workbook;
                //int masterSheetColumnIndex = 0;
                try {
                        workbook = WorkbookFactory.create(new FileInputStream(envFilePath + "\\"+ listOfFiles[i].getName()));
                        // Get the first sheet.
                        Sheet sheet = workbook.getSheetAt(0);
                        // Get the first cell.
                        Row row = sheet.getRow(0);
                        //Cell cell = row.getCell(0);
                        for (Cell cell1 : row) {
                            // Show what is being read.
                            System.out.println(cell1.toString());   
                            //masterSheetColumnIndex++;
                        }
                        //we will search for column index containing string "Your Column Name" in the row 0 (which is first row of a worksheet
                        String columnWanted = "Modality Name";
                        Integer columnNo = null;
                        //output all not null values to the list
                        List<Cell> cells = new ArrayList<Cell>();

                        Row firstRow = sheet.getRow(0);

                        for(Cell cell1:firstRow){
                            if (cell1.getStringCellValue().equals(columnWanted)){
                                columnNo = cell1.getColumnIndex();
                            }
                        }

                        if (columnNo != null){
                            for (Row row1 : sheet) {
                               Cell c = row1.getCell(columnNo);
                               if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
                                  // Nothing in the cell in this row, skip it
                               } else {
                                  cells.add(c);
                               }
                            }
                            System.out.println("");
                        }else{
                            System.out.println("could not find column " + columnWanted + " in first row of " + listOfFiles[i].getName());
                        }

                } catch (InvalidFormatException | IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Comments

0

Reading a particular column

   File myFile = new File(path);
    FileInputStream fis = new FileInputStream(myFile);

    // Finds the workbook instance for XLSX file
    XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);

    //XSSFWorkbook workBook = new XSSFWorkbook();
    //Reading sheet at number 0 in spreadsheet(image attached for reference
    Sheet sheet = myWorkBook.getSheetAt(0);

    //creating a Sheet object to retrieve object  
    Iterator<Row> itr = sheet.iterator();//iterating over excel file  

    while (itr.hasNext())                 
    {  
        Row row = itr.next();
        Iterator<Cell> cellIterator = row.cellIterator();//iterating over each column
        //Reading cell in my case column name is ppm
        Cell ppmEx= row.getCell(0);

        //Cell cell = cellIterator.next();
        while (cellIterator.hasNext())
        {  
            Cell cell = cellIterator.next();
            //Check the cell type and format accordingly
            switch (cell.getCellType()) 
            {
                case Cell.CELL_TYPE_NUMERIC:
                    //System.out.println(cell.getNumericCellValue() + "    ");
                    al.add(cell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_STRING:
                    //System.out.println(cell.getStringCellValue()+" ");
                    al.add(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    //System.out.println(cell.getBooleanCellValue()+" ");
                    al.add(cell.getBooleanCellValue());
                case Cell.CELL_TYPE_BLANK:
                    //System.out.println("blank");
                    al.add("blank");

            }  
        }  
        System.out.println("-");
    }

Comments

0

You can also try it this way. You can iterate through header row.

Row headerRow = sheet.get(0);

for (int cellIndex = 0; cellIndex < 150; cellIndex++) {
    Cell cell = row.getCell(cellIndex);
    if (columnName.equals(cell.getStringCellValue())) {
        // do your thing
    }
}

Note: If there's a case where your cell can be empty then use:

Cell cell = row.getCell(cellIndex);

if (cell!=null) {
    if (columnName.equals(cell.getStringCellValue())) {
        // do your thing
    }
}

I hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.