0

I am reading a value from excel and then after some logic writing some values into an excel file. The problem is only the last value is being written in the excel.

I am calling writeExcel method each time I have to write a row in the excel.

Below is the code.

FileOutputStream fileOut = new FileOutputStream("poc-test.xls");
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet worksheet = workbook.createSheet("POC Worksheet");
    public void writeExcel(String str) throws Exception{
        HSSFRow row1 = worksheet.createRow((short) i++);

        HSSFCell cellA1 = row1.createCell((short) 0);
        cellA1.setCellValue(str);

        workbook.write(fileOut);            
    }

1 Answer 1

1
            while (cellIterator.hasNext()) 
            {
                ...
                ReadMyExcel object= new ReadMyExcel();
                object.sendGet(url);
                ...
            }

You are creating a new ReadMyExcel object on each pass through the loop, which causes i fileout, workbook and worksheet to be newly initialized member variables each time.

But your code has other severe problems. It needs some serious restructuring to eliminate dependencies on "global" variables that make it very confusing to read and very hard to maintain.

Sign up to request clarification or add additional context in comments.

2 Comments

Jim Thanks for your response. but I think i value is getting updated after every call to the method. I dont know why HSSFRow row1 = worksheet.createRow((short) i++); does not make the output value to be in another row of the excel worksheet.
Because each time you execute that, i has the value 0. It's a different instance of i each time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.