Read Excel horizontally with TestNG - java

I can read an Excel sheet vertically and the pass the values to TestNG dataprovide.
This makes TestNG executes each row.
public Object[][] getData(ITestContext context) throws IOException {
Object[][] obj = null;
try {
File file = new File(context.getCurrentXmlTest().getParameter("resource"));
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
wb.close();
int rowCount= sheet.getLastRowNum();
int colCount = sheet.getRow(0).getLastCellNum();
obj = new Object[rowCount][1];
DataFormatter df = new DataFormatter();
for (int i = 0; i < rowCount; i++) {
Map<String, String> datamap= new HashMap<String, String>();
for (int j = 0; j < colCount; j++) {
String a = df.formatCellValue(sheet.getRow(0).getCell(j));
String b = df.formatCellValue(sheet.getRow(i+1).getCell(j));
if(!b.equals(""))
datamap.put(a, b);
if(b.equals("[PWD]")) {
b=PropertiesFile.getProperty("psw");
byte[] byteArray = Base64.decodeBase64(b.getBytes());
String decodedString = new String(byteArray);
b=decodedString;
datamap.put(a, b);
}
}
obj[i][0] =datamap;
}
}catch(Exception e){
e.printStackTrace();
}
return obj;
}
I'd like to read it horizontally, so that TestNG executes each column (the first column would be the keys column), how can i do?
Thanks

You can transpose your sheet and work with former columns which are now the rows: How to transpose sheet with POI SS/XSSF?
Or just change your loop nesting so that you go by columns in the outer loops. Not by rows.

I think i solved it
public Object[][] getData(ITestContext context) throws IOException {
Object[][] obj = null;
try {
File file = new File(context.getCurrentXmlTest().getParameter("resource"));
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
wb.close();
int rowCount= sheet.getLastRowNum();
int colCount = sheet.getRow(0).getLastCellNum();
obj = new Object[colCount-1][1];
DataFormatter df = new DataFormatter();
for (int i = 0; i < colCount-1; i++) {
Map<String, String> datamap= new HashMap<String, String>();
for (int j = 0; j < rowCount+1; j++) {
String a = df.formatCellValue(sheet.getRow(j).getCell(0));
String b = df.formatCellValue(sheet.getRow(j).getCell(i+1));
System.out.println(a+ " "+b);
if(!b.equals(""))
datamap.put(a, b);
if(b.equals("[PWD]")) {
b=PropertiesFile.getProperty("psw");
byte[] byteArray = Base64.decodeBase64(b.getBytes());
String decodedString = new String(byteArray);
b=decodedString;
datamap.put(a, b);
}
}
obj[i][0] =datamap;
}
}catch(Exception e){
e.printStackTrace();
}
return obj;
}

Related

Converting excel (.xslx) file to java with cell that has hyperlink text won't be recognized and result is cell with empty value

This is the code which iterate through the excel file and extract the value from the cells:
public static <R> List<R> readAdvancedStudentSearchFromCSV(String fileName, CSVParser<R> parser) throws IOException {
List<R> rowsList = new ArrayList<>();
FileInputStream fis = new FileInputStream(fileName);
//Create Workbook instance for xlsx/xls file input stream
XSSFWorkbook workbook = null;
if(fileName.toLowerCase().endsWith("xlsx")){
workbook = new XSSFWorkbook(fis);
}
int numberOfSheets = workbook.getNumberOfSheets();
XSSFSheet sheet = workbook.getSheet("data");
int rowCount = sheet.getPhysicalNumberOfRows();
StringBuilder line = new StringBuilder();
for(int i=2; i<rowCount; i++){
String cellValue = "";
for(int j=0; j<10; j++){
try {
cellValue = sheet.getRow(i).getCell(j).getStringCellValue();
System.out.println(cellValue);
}catch(NullPointerException e){
cellValue = "";
}
if(cellValue.equals("")){
line.append(" ").append(" ").append(",");
}else {
line.append(" ").append(cellValue).append(",");
}
}
String[] attributes = line.toString().split(",");
R row = parser.toRow(attributes);
rowsList.add(row);
}
//close file input stream
fis.close();
return rowsList;
}
And this is how the excel file look like:
Problem:
When I reach cell No.7 (trying to get column named "Resume") with the value but I am getting empty string instead of getting the actual hypertextlink (as string or as link it is not important).
I have tried to use getHypertextLink method from Cell class but this did not help as well.
I have looked at: How to get hyperlink address from a cell in excel by using java?
which did not solve my issue.

How to read data from excel using apache poi and store the data into String[][] array?

I want to read data from excel using apache poi and store that data into 2Dimentional String Array. Using below code I will display data but I want to store the data.
public static void main(String[] args) throws Exception {
File f = new File("C:/Users/SYKAMREDDY/Desktop/testData.xls");
FileInputStream fis = new FileInputStream(f);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sh = wb.getSheet("Data");
int rc=sh.getLastRowNum()-sh.getFirstRowNum();
for (int i = 1; i < rc; i++) {
Row r = sh.getRow(i);
for (int j = 1; j < r.getLastCellNum(); j++) {
String s = r.getCell(j).getStringCellValue();
System.out.print(s+" ");
}
System.out.println();
}
}
Try to use byteArray
simplified example:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
workbook.write(bos);
} finally {
bos.close();
}
byte[] bytes = bos.toByteArray();
also, take a look at How can I convert POI HSSFWorkbook to bytes?
if you want to use string , simpy do
String s = new String(bytes);

converting csv to excel through java

I have to convert CSV to XLS format through Java POI since I am doing some manipulations with XLS sheets through POI. Below is my code:
File file = new File("C:\\abc.csv");
FileInputStream fin = null;
fin = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(fin);
HSSFSheet firstSheet1 = workbook.getSheetAt(0);
Now I want to write a fuctions, lets say method name is convertcsvtoexcel which will accept the file obj and in return it will be give me converted XLS file that file will be stored in my C: drive with the name abcout.xls and later on I will be passing it to workbook as shown. I have tried the following code. Please advise how I can custoise it to make it fittable for my piece of code.
ArrayList arList = null;
ArrayList al = null;
String fName = "test.csv";
String thisLine;
int count = 0;
FileInputStream file = null;
file = new FileInputStream(new File("C:\\abc.csv"));
//FileInputStream fis = new FileInputStream(file);
DataInputStream myInput = new DataInputStream(file);
int i = 0;
arList = new ArrayList();
while ((thisLine = myInput.readLine()) != null) {
al = new ArrayList();
String strar[] = thisLine.split(",");
for (int j = 0; j < strar.length; j++) {
al.add(strar[j]);
}
arList.add(al);
System.out.println();
i++;
}
try {
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for (int k = 0; k < arList.size(); k++) {
ArrayList ardata = (ArrayList) arList.get(k);
HSSFRow row = sheet.createRow((short) 0 + k);
for (int p = 0; p < ardata.size(); p++) {
HSSFCell cell = row.createCell((short) p);
String data = ardata.get(p).toString();
if (data.startsWith("=")) {
cell.setCellType(Cell.CELL_TYPE_STRING);
data = data.replaceAll("\"", "");
data = data.replaceAll("=", "");
cell.setCellValue(data);
} else if (data.startsWith("\"")) {
data = data.replaceAll("\"", "");
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(data);
} else {
data = data.replaceAll("\"", "");
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(data);
}
//*/
// cell.setCellValue(ardata.get(p).toString());
}
System.out.println();
}
FileOutputStream fileOut = new FileOutputStream("C:\\abcout.xls");
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated");
} catch (Exception ex) {
ex.printStackTrace();
} //main method end
public static void csvToXLSX() {
try {
String csvFileAddress = "test.csv"; //csv file address
String xlsxFileAddress = "test.xlsx"; //xlsx file address
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("sheet1");
String currentLine=null;
int RowNum=0;
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
RowNum++;
XSSFRow currentRow=sheet.createRow(RowNum);
for(int i=0;i<str.length;i++){
currentRow.createCell(i).setCellValue(str[i]);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage()+"Exception in try");
}
}

How to recover formula cell cache result from excel file

I'm trying to retrieve the cached result of a formula cell in excel, the entire column is comprised of formula cells and i want to store the cached results of the columns' cells in an arraylist, but i get the error.
Apologies, i pasted the wrong code earlier, its fixed now.
public static void main(String[] args) throws Exception {
String filename = "C:/Users/L30902/Desktop/eclipse folder/FeaturesTest/student.xlsx";
FileInputStream fis = null;
int cellvalue = 0;
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
while (rowIter.hasNext()) {
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector<String> cellStoreVector = new Vector<String>();
while (cellIter.hasNext()) {
XSSFCell myCell = (XSSFCell) cellIter.next();
try {
cellvalue = myCell.getCachedFormulaResultType();
} catch (Exception e) {
}
cellStoreVector.addElement(Integer.toString(cellvalue));
}
String secondcolumnValue = null;
int i = 0;
secondcolumnValue = cellStoreVector.get(i).toString();
insertQuery(secondcolumnValue);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
// showExelData(sheetData);
}
private static void insertQuery(String secondcolumnvalue) {
System.out.println(secondcolumnvalue);
}
I should return values like 500, 33 but i only return 0, 0

How To handle Null Row using Apache POI?

I am using Apache POI to read xlsx file, it works well. I have question to you when row is found null, how I'm able to handle it? My file contain 500 row, but it show 105667 row, rest of row found null.
used code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
*
* #author SAMEEK
*/
public class readXLSXFile {
public int getNumberOfColumn(String fileName, int sheetIndex) throws FileNotFoundException, IOException {
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
XSSFRow row = null;
int lastRowNum = 0;
int lastCellNum = 0;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(sheetIndex);
lastRowNum = sheet.getLastRowNum();
for (int i = 0; i < lastRowNum; i++) {
row = sheet.getRow(i);
if (row != null) {
if (row.getLastCellNum() > lastCellNum) {
lastCellNum = row.getLastCellNum();
}
}
}
return lastCellNum;
}
public int getNumberOfRow(String fileName, int sheetIndex) throws FileNotFoundException, IOException {
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
int lastRowNum = 0;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(sheetIndex);
lastRowNum = sheet.getLastRowNum();
return lastRowNum;
}
public String[] getSheetName(String fileName) throws FileNotFoundException, IOException {
int totalsheet = 0;
int i = 0;
String[] sheetName = null;
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
totalsheet = workbook.getNumberOfSheets();
sheetName = new String[totalsheet];
while (i < totalsheet) {
sheetName[i] = workbook.getSheetName(i);
i++;
}
return sheetName;
}
public int getNumberOfSheet(String fileName) throws FileNotFoundException, IOException {
int totalsheet = 0;
File inputFile = null;
FileInputStream fis = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
int lastRowNum = 0;
// Open the workbook
inputFile = new File(fileName);
fis = new FileInputStream(inputFile);
workbook = new XSSFWorkbook(fis);
totalsheet = workbook.getNumberOfSheets();
return totalsheet;
}
public String[][] getSheetData(String fileName, int sheetIndex) throws FileNotFoundException, IOException, InvalidFormatException {
String[][] data = null;
int i = 0;
int j = 0;Cell cell=null;
long emptyrowcount = 0;
InputStream inputStream = new FileInputStream(
fileName);
// Create a workbook object.
Workbook wb = WorkbookFactory.create(inputStream);
wb.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
Sheet sheet = wb.getSheetAt(sheetIndex);
// Iterate over all the row and cells
int noOfColumns = getNumberOfColumn(fileName, sheetIndex);
System.out.println("noOfColumns::" + noOfColumns);
int noOfRows = getNumberOfRow(fileName, sheetIndex) + 1;
System.out.println("noOfRows::" + noOfRows);
data = new String[noOfRows][noOfColumns];
for (int k = 0; k < noOfRows; k++) {
Row row = sheet.getRow(k);
if (row == null) {
} else {
j = 0;
for (int l = 0; l < noOfColumns; l++) {
// Cell cell = cit.next();
cell = row.getCell(j);
if (cell.getCellType() == cell.CELL_TYPE_BLANK) {
cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
}
data[i][j] = getCellValueAsString(cell);
j++;
}
i++;
}
}
return data;
}
/**
* This method for the type of data in the cell, extracts the data and
* returns it as a string.
*/
public static String getCellValueAsString(Cell cell) {
String strCellValue = null;
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
strCellValue = cell.toString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy");
strCellValue = dateFormat.format(cell.getDateCellValue());
} else {
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
strCellValue = new String(longValue.toString());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
strCellValue = new String(new Boolean(
cell.getBooleanCellValue()).toString());
break;
case Cell.CELL_TYPE_BLANK:
strCellValue = "";
break;
}
}
return strCellValue;
}
public static void main(String s[]) {
try {
readXLSXFile readXLSxFile = new readXLSXFile();
String[][] sheetData = readXLSxFile.getSheetData("F:/work.xlsx", 0);
int columnLength = 0;
columnLength = readXLSxFile.getNumberOfColumn("F:/work.xlsx", 0);
int rowLength = 0;
rowLength = readXLSxFile.getNumberOfRow("F:/work.xlsx", 0);
int h = 0;
int j = 0;
while (j < rowLength) {
h = 0;
while (h < columnLength) {
System.out.print("\t " + sheetData[j][h]);
h++;
}
System.out.println("");
j++;
}
} catch (InvalidFormatException ex) {
Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Please help me how to handle null row in excel sheet?
If you fetch a row, and get back null, then that means there is no data stored in the file for that row - it's completely blank.
POI by default gives you what's in the file. With Cells, you can set a MissingCellPolicy to control how missing and blank cells are handled. There's some examples of using this in the Apache POI docs. With rows, they're either there or not, so you need to check for nulls when fetching a row.
In case your .xlsx file contains any of the formatting for the blank cells, the poi reading is not treating it as null, however if you want to print it's value, it will give NullPointerException. To understand it I have created a sheet and mark the first columns boundary with to "All Border" for 10 rows, but not given any value to it. now applying following piece of code is showing output sheet.lastRowNum() as 10, while the RowCountWithNullValue is 990, and RowCountWithoutNullValue is 10. However the sheet is completely blank. If you uncomment the print statement, it will show NullPointerException.
public class Rough {
public static void main(String args[]) throws IOException{
public static void main(String args[]) throws IOException{
FileInputStream fin = new FileInputStream(AddressOfxlsxFile);
XSSFWorkbook wb = new XSSFWorkbook(fin);
XSSFSheet sheet = wb.getSheetAt(1);
int RowCountWithNullValue=0, RowCountWithoutNullValue=0;
for (int i=0;i<1000;i++){
if (sheet.getRow(i)==null)
RowCountWithNullValue++;
else{
RowCountWithoutNullValue++;
// System.out.println(sheet.getRow(0).getCell(0));
}
}
System.out.println(sheet.getLastRowNum());
System.out.println(RowCountWithNullValue+","+RowCountWithoutNullValue);
}
}
I am not sure if the same is happening on your end or not, but if you are saying your file contain 500 row, but it show 105667 row, this may be one of the cause.

Categories