Function to read selective columns by providing column position POI

apache poi
how to read excel by column name in java
xssfworkbook
how to read cell value in excel using java poi
read particular column from excel in java
apache poi tutorial
apache poi maven
how to read excel column wise in java

Sample data in excel file, here first row is header.

Demo1           Demo2           Demo3           Demo4           Demo5           Demo6
DummyText1      DummyText2      DummyText3      DummyText4      DummyText5      DummyText6
DummyText11     DummyText21     DummyText31     DummyText41     DummyText51     DummyText61

Below code to include selective column is working fine,however to another column, again I have to add cell1.getColumnIndex() in if condition and so on. :

for (Cell cell1 : row) {
    if (cell1.getColumnIndex() != 1 && cell1.getColumnIndex() != 3) {
        continue;
    }
}

So I created below function to skipCells(); however it is not working as expected.

for (Cell cell : row) {
    skipCells(cell,1,3,6) // this should read only column 1,3 and 6, but it is not working.
    String result = getCellData(xssfWorkbook, cell);
    System.out.println(result);
}

static void skipCells(Cell cell,int ...cellPosition)
{
    for (int i: cellPosition) {
        if (cell.getColumnIndex() != i) {
            continue;
        }
    }
}

You're just iterating over the cells and you do nothing at all, change skipCells to

static void skipCells(Cell cell,int ...cellPosition) {
    for (int i: cellPosition) {
        if (cell.getColumnIndex() == i) {
            break;
        }
    }
}

Or just create a list of the columns you want to read and check if the column in the list or not.

List<Integer> list = Arrays.asList(1, 3, 6);
for (Cell cell : row) {
    if (list.contains(cell.getColumnIndex())) {
        String result = getCellData(xssfWorkbook, cell);
        System.out.println(result);
    }
}

Using Apache POI how to read a specific excel column, heikkim is right, here is some sample code adapted from some code I have: import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel. Sheet� Number of columns in the column set. Width of each column - determined by the format mask and expected size of numbers to be displayed in the column. Starting position and width of previous columns. Currency profile options - determine whether you are using thousands separators, as well as positive and negative number formats. If these options

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

        while (cells.hasNext())
        {colNumber++;
            cell=(HSSFCell) cells.next();

            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
            {
                System.out.print(cell.getStringCellValue()+" ");
            }
            else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
            {
                System.out.print(cell.getNumericCellValue()+" ");
            }
            else
            {
                //U Can Handel Boolean, Formula, Errors
            }



             switch(colNumber){
                                        case 1:
                                            singleRecord.setXX1(cellValue);
                                            break;
                                        case 2:
                                            singleRecord.setXX2(cellValue);
                                            break;
                                        case 3:
                                            singleRecord.setXX3(cellValue);
                                            break;
                                        case 4:
                                            singleRecord.setXX4(cellValue);
                                            break;
                                        case 5:
                                            singleRecord.setXX5(cellValue);
                                            break;
                                        case 6:
                                            singleRecord.setXX6(cellValue);
                                            break;
                                        default:
                                            System.out.println("--> Invalid column");
                            }   





        }
        System.out.println();
    }
     /////////////////////////////////////
     create bean class and add getter/setter properties inside switch case 
     you can later add this object to LinkList also and process as you wish

Busy Developers' Guide to HSSF and XSSF Features, Want to use HSSF and XSSF read and write spreadsheets in a hurry? Shift rows; Set a sheet as selected; Set the zoom magnification for a sheet; Create split and @param column the column number to create the cell in The convenience functions provide utility features such as setting borders around� The resolution, R, [NOTE— All terms and symbols are defined in the Glossary of Symbols] is a function of column efficiency, N, and is specified to ensure that closely eluting compounds are resolved from each other, to establish the general resolving power of the system, and to ensure that internal standards are resolved from the drug. Column

Changed the return type of skipCells function to Cell

static Cell skipCells(Cell cell, int... cellPosition){
    Cell c = null;
    for (int i: cellPosition) {
        if (cell.getColumnIndex() != i) {
            continue;
        }
        c = cell;
    }
    return c;
}

for (Cell cell : row) {
    Cell readCell = skipCells(cell, 1, 3, 6)
     if(readCell != null ) { // remove null value if any
        System.out.println(getCellData(xssfWorkbook, readCell));               
    }
}

XSSFSheet (POI API Documentation), public class XSSFSheet extends POIXMLDocumentPart implements Sheet Create a pivot table using the AreaReference range, at the given position. Provide access to the CTWorksheet bean holding this sheet's data when a cell with a 'master' shared formula is removed, the next cell in the range becomes the master. Surprisingly, we observed that the sequence of this limb enhancer is conserved throughout nearly all examined species in the snake lineage. In basal snakes, which retain vestigial limbs, it is highly conserved, whereas it underwent a rapid increase in substitution rate in advanced snakes, in which all skeletal limb structures have disappeared.

Java: Read / Write Excel file in Java with Apache POI, To read an excel file, Apache POI provides certain easy-to-use APIs. In order to work with newer xls format viz .xlsx, you need to see newer POI classes Before reading a cell content, we need to first determine its type using method cell. If user clicks on the topmost checkbox, then all rows get selected. Providing a concrete global food guarantee has been the vision of scientists, world leaders, and nations. With a constantly growing per capita income in India, the demand for fish is on the rise.

How to Read Excel File in Java, The Apache POI library provides two implementations for reading excel files: HSSFCell: It is a class representing a cell in a row of XLS file. Right-click on the project ->Build Path ->Add External JARs -> select all the above jar files numeric cell type; //getting the value of the cell as a number; System.out.print(cell. We would like to show you a description here but the site won’t allow us.

Dynamically select any column in an Excel Table, However, this creates a problem when we want to provide the user with the ability The “B4” in the formula above is not a Cell reference; it is surrounded by double INDEX / MATCH can also be used to dynamically select a column from a Read other blogs, or watch YouTube videos on the same topic. Selective inhibitors of falcipain 1 identified from this screen impaired the ability of P. falciparum to invade erythrocytes, providing an exciting proof of concept for this protease as an antimalarial drug target. Notably, this research shows that ABPs can drive the discovery of enzyme function from the earliest stages by providing initial

Comments
  • second conditions works perfectly. Thank you so much.
  • Thanks for the reply @Prasad, in case of having more than 6 column, again I have to add case. which I don't want.
  • pass the HSSFRow to some function and try some logical hooks/crooks you will achieve, because rest is just brain-storming you are going right