0

I have used POI to read from / write to excel sheets.When writing to a new sheet using POI,it worked.But, when i am trying to write in a existing excel workbook using POI, it doesnt worked.how can i correct this issue?

              workBook =  getWorkbookSheet(workBookName);
    sheet1 = workBook.getSheetAt(1);
    sheet2 = workBook.getSheetAt(2);

    while(sheetStart<sheet1.getLastRowNum() + 1)
    {
        HSSFRow rowSheet1 = sheet1.getRow(sheetStart);
        HSSFCell cellSheet1 = rowSheet1.getCell(4);
        if(cellSheet1.getStringCellValue().trim().equals(valY))
        {                                           cellSheet1.setCellValue("N");
        }
        else
            //do nothing 
            sheetStart++;
    }
    fileOutSheet1 = new FileOutputStream(sheet1.getSheetName());
    workBook.write(fileOutSheet1);
    fileOutSheet1.flush();
    fileOutSheet1.close();

1 Answers1

0

fileOutSheet1 = new FileOutputStream(sheet1.getSheetName()); seems not correct. you should give the path of workbook instead of sheet1.getSheetName. because you are writing the whole workbook even after a single cell content change.

for example it should be like

    FileOutputStream fileOut = new FileOutputStream("C:\\MyWorkbook.xlsx");
    wb.write(fileOut);
    fileOut.close();

for detail visit here

Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74