Writing to xlsx based on read condition from same file is fa
Writing to xlsx based on read condition from same file is fa
M
Posted on 04/10/2016
Hi,
I have created a java code and function to write the Pass/Fail Criteria to file from where i am searching for one column for Y and N. If Column 2 is Y then set column 3 to PASS else set to FAIL.
Issue - If i access the function to set single row then its working fine. BUT when i am calling same function in For loop its not updating the PASS/FAIL column per condition.
Please find below the code
[b:17z44d5r][color=#000080:17z44d5r]MAIN CLASS FILE[/color:17z44d5r][/b:17z44d5r]
package Lib;
public class OperateExcel {
public static void main(String[] args) throws Exception {
ReadExcelFunctions ref = new ReadExcelFunctions("C://Users//Dinesh//Desktop//ReadWrite.xlsx"); ref.getWholeSheetData(0); //Function for reading whole data from Sheet 0 int rowcount = ref.getRowCount(0); //Function to get the row Count System.out.println("Total # of Rows in sheet are-->"+rowcount);
//ref.setData(0, 1, 3, "Pass"); for(int i = 1;i<rowcount;i++) { String celldata = ref.getCellDataAt(0,i,2); //Function to get data at particular cell String str = "Y"; if(celldata==str) { ref.setData(0, i, 3, "Pass"); } }
ref.getWholeSheetData(0); //Function for reading whole data from Sheet 0 //ref.addSheet("Dinesh"); }
}
[b:17z44d5r][color=#000080:17z44d5r]CLASS FILE CONTAIN FUNCTION SETDATA();[/color:17z44d5r][/b:17z44d5r]
public class ReadExcelFunctions { File src; FileInputStream fis; FileOutputStream fout; //FileOutputStream fos XSSFWorkbook wb; Sheet sheet1; int lastrow=0; int lastcol=0; Row row;
public ReadExcelFunctions(String path) { try { src = new File(path); fis = new FileInputStream(src); //fout = new FileOutputStream(src); wb = new XSSFWorkbook(fis); wb.getSheetAt(0); } catch (Exception e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); }
}
//Method to get the data from whole sheet public String getWholeSheetData(int sheetindex) { sheet1 = wb.getSheetAt(sheetindex); lastrow = sheet1.getLastRowNum(); row = sheet1.getRow(0); lastcol = row.getLastCellNum(); String data = null; for(int i = 0;i<lastrow;i++) { for(int j=0;j<lastcol;j++) { data = sheet1.getRow(i).getCell(j).getStringCellValue(); System.out.print(data); System.out.print(" || "); } System.out.println(""); }
return data; }
//Get count of rows public int getRowCount(int sheetindex) { sheet1 = wb.getSheetAt(sheetindex); lastrow = sheet1.getLastRowNum(); row = sheet1.getRow(0); //lastcol = row.getLastCellNum(); //lastcol = row.getLastCellNum(); //System.out.println("Total # of Roes in sheet are-->"+lastrow); return lastrow; //return lastrow; }
//Method to get the data from whole sheet public String getCellDataAt(int sheetindex, int rownum, int colnum) { sheet1 = wb.getSheetAt(sheetindex); lastrow = sheet1.getLastRowNum(); row = sheet1.getRow(0); lastcol = row.getLastCellNum(); String data = null; data = sheet1.getRow(rownum).getCell(colnum).getStringCellValue(); return data; }
public void setData(int sheetindex, int rownum, int colnum, String PassFail) { sheet1 = wb.getSheetAt(sheetindex); //sheet1.getRow(1).createCell(4).setCellValue("Fail"); sheet1.getRow(rownum).createCell(colnum).setCellValue(PassFail); FileOutputStream fout; try { fout = new FileOutputStream(src); wb.write(fout); //wb.close(); } catch (Exception e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); }
}
//Delete sheet //create column //create row //add data to sheet //delete data from sheet //find if sheet exists }
M
Replied on 05/10/2016
why are you not using the excel reader(xls_reader.java) that we provide you?
try to debug your code on eclipse.
M
Replied on 05/10/2016
Problem is solved. I was not comparing the strings values correctly.. Thanks for the reply