MelbyJim
MelbyJim

Reputation: 35

Writing from ArrayList to Excel sheet ListIterator

I got an ArrayList which holds information on employees such as fName, sName and adress. I am writing this to an Excel workbook using apache poi.

In the excel sheet I want all the information on one employee to come in the same row. I manage to write the first name in row 1 and cell 0 (row 0 holds headings). Everything is in vertically good order. When trying to add surname everything starts to jump here and there.

How do I get the sName written in the right place?

Here is part of my excel code

private void writeEmployeeInfo() {

    ListIterator<Employee> employeeListIterator = Employee
            .getEmployeeList().listIterator();

    int rowIndex = 1;
    int cellIndex = 0;


    while (employeeListIterator.hasNext()) {

        Employee employee = employeeListIterator.next();

        Row row = sheet.createRow(rowIndex++);

        Cell cell = row.createCell(cellIndex);

        cell.setCellValue(employee.getfName());

// trying to add surname of employees in the right cell.

            while(employeeListIterator.hasNext()){
            Employee emp = employeeListIterator.next();
            row = sheet.createRow(rowIndex++);
            cell = row.createCell(cellIndex++);
            cell.setCellValue(emp.getsName());
        }
    }
}

This is my first post here, so if I have written or done things i a poor manner, please tell me what to do so it will be easy for you people to understand my question.

Here is a link to the project if you need to see more code: http://1drv.ms/1kSGfDm

Upvotes: 1

Views: 12658

Answers (1)

enterbios
enterbios

Reputation: 1757

You didn't post how are you adding another cells. Try something like this:

private void writeEmployeeInfo() {

    ListIterator<Employee> employeeListIterator = Employee
            .getEmployeeList().listIterator();

    int rowIndex = 1;

    while (employeeListIterator.hasNext()) {

        Employee employee = employeeListIterator.next();

        Row row = sheet.createRow(rowIndex++);

        row.createCell(0).setCellValue(employee.getfName());
        row.createCell(1).setCellValue(employee.getSName());
        row.createCell(2).setCellValue(employee.getAddress);
    }
}

Upvotes: 2

Related Questions