Skip to content

Instantly share code, notes, and snippets.

@jpukg
Last active June 10, 2022 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpukg/3d5a934c9e26598428124056795f5102 to your computer and use it in GitHub Desktop.
Save jpukg/3d5a934c9e26598428124056795f5102 to your computer and use it in GitHub Desktop.
Read a particular column values from excel - Apache POI
public class ReadExcelColumnValues {
public static void main(String[] args) throws IOException {
String sheetName = "Sheet2";
String columnName = "Status";
String[][] sheetValues = getSheetData(App.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "\\Test-excel.xls", sheetName);
List<String> columnValues = getColumnValues(getColumnPosition(columnName, sheetValues), sheetValues);
System.out.println(columnValues);
}
private static List<String> getColumnValues(int columnPosition, String[][] sheetValues) {
List<String> columnValues = new ArrayList<>();
for (int i = 1; i < sheetValues.length; i++) {
if(sheetValues[i][columnPosition] != null) {
columnValues.add(sheetValues[i][columnPosition]);
}
}
return columnValues;
}
private static int getColumnPosition(String columnName, String[][] sheetValues) {
int columnPosition = -1;
for (int i = 0; i < sheetValues[0].length; i++) {
if (columnName.equalsIgnoreCase(sheetValues[0][i])) {
return i;
}
}
return columnPosition;
}
// Dependencies: POI | HSSF Workbook/Sheet/Row/Cell
// This method will read and return Excel data into a double array
public static String[][] getSheetData(String filename, String sheetName) {
String[][] dataTable = null;
File file = new File(filename);
try {
// Create a file input stream to read Excel workbook and worksheet
FileInputStream xlfile = new FileInputStream(file);
HSSFWorkbook xlwb = new HSSFWorkbook(xlfile);
HSSFSheet xlSheet = xlwb.getSheet(sheetName);
// Get the number of rows and columns
int numRows = xlSheet.getLastRowNum() + 1;
int numCols = xlSheet.getRow(0).getLastCellNum();
// Create double array data table - rows x cols
// We will return this data table
dataTable = new String[numRows][numCols];
// For each row, create a HSSFRow, then iterate through the "columns"
// For each "column" create an HSSFCell to grab the value at the specified cell (i,j)
for (int i = 0; i < numRows; i++) {
HSSFRow xlRow = xlSheet.getRow(i);
for (int j = 0; j < numCols; j++) {
HSSFCell xlCell = xlRow.getCell(j);
if(xlCell != null) {
dataTable[i][j] = xlCell.toString();
}
}
}
} catch (IOException e) {
System.out.println("ERROR FILE HANDLING " + e.toString());
}
return dataTable;
}
}
@jpukg
Copy link
Author

jpukg commented Jun 2, 2022

Test data:

User Role Column3
ricky martin admin value1
oh pa moderator value2
maria user value3

@jpukg
Copy link
Author

jpukg commented Jun 10, 2022

package test;

public class TestMain {
    public static void main(String[] args) {

        //Expected
        Object data1[][] = {
                            {"NOT_STARTED"},
                            {"DRAFT"}
                          };
        System.out.println(data1);

        // Convert "data" as per import org.testng.annotations.DataProvider;
        String[] data = new String[]{"NOT_STARTED", "DRAFT"};
        Object[][] finalObjectToDataProvider = new Object[data.length][1];
        int i = 0;
        for (int k = 0; k < data.length; k++) {
            finalObjectToDataProvider[k][i] = data[k];
        }

        System.out.println(finalObjectToDataProvider);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment