1

I want to write to data which contains ArrayList of String to excel using java. When i am doing only the first data only writing to excel.remaining data is not.

public class ExcelGenerator {
    public static void main(String args[]) throws FileNotFoundException, HPSFException, IOException, SQLException, NestableException
    {
        ArrayList<String> arrlist = new ArrayList<String>();
          arrlist.add("14");
          arrlist.add("7");
          arrlist.add("39");
          arrlist.add("40");

          /* For Loop for iterating ArrayList */
          System.out.println("For Loop");
          for (int counter = 0; counter < arrlist.size(); counter++) {            
              System.out.println(arrlist.get(counter));         
          }   


        HSSFWorkbook workbook=new HSSFWorkbook();
        File f=new File("D:/Test/test.xls");
        GeericExcelGenerator gl=new GeericExcelGenerator("Test",workbook);
        FileOutputStream outputStream=new FileOutputStream(f);
        gl.generate(outputStream,arrlist);

    }

    public void generate(OutputStream outputStream,ArrayList arrlist) throws SQLException, IOException, NestableException 
    {
        try 
        {
            int currentRow = 0;
            for (int counter = 0; counter < arrlist.size(); counter++) {    

                HSSFRow row = sheet.createRow(currentRow);
                System.out.println("Description is"+arrlist.get(counter));

                String c=(String) arrlist.get(counter);
                int i=0;
                HSSFCell cell = HSSFCellUtil.createCell(row, i, null);
                cell.setCellValue(c);

                 i++;
                workbook.write(outputStream);
                currentRow++;
                currentRow++;
            }
        }catch(IOException  e)
        {}
        finally {
            outputStream.close();
        }
    }
}
3
  • 1
    Can you use the formatter in your IDE? It will make your code easier to read. Commented Oct 29, 2018 at 10:21
  • 2
    If you program isn't working, I would not ignore exceptions, but rather print them as they might be trying to tell you something. Commented Oct 29, 2018 at 10:22
  • Where does the sheet variable suddely come from? I don't see it initialized anywhere. This cant' compile. Commented Oct 29, 2018 at 11:39

1 Answer 1

3

Try below solution may due to your initialization "i" variable

public void generate(OutputStream outputStream, ArrayList arrlist)
        throws SQLException, IOException, NestableException {
    try {
        int currentRow = 0;
        int i = 0;
        for (int counter = 0; counter < arrlist.size(); counter++) {

            HSSFRow row = sheet.createRow(currentRow);
            System.out.println("Description is" + arrlist.get(counter));

            String c = (String) arrlist.get(counter);

            HSSFCell cell = HSSFCellUtil.createCell(row, i, null);
            cell.setCellValue(c);

            i++;
            workbook.write(outputStream);
            currentRow++;
            currentRow++;

        }
    } catch (IOException e) {

    } finally {
        outputStream.close();
    }
}

Or you can check your implmentation with below example https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java

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

Comments

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.