1

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

1 Answer 1

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I am trying to avoid doing what you say there. I am using two counters; rowIndex and cellIndex. After adding the first name of all objects in the employeeList in cell 0, row 1,2,3 etc.I want to moce to cell 1 row,1,2,3 etc and add sure-name. This I want to do without writing create cell/row, but use the counters (rodIndex and cellIndex).
Could you paste the code you have tried but is not working as expected? Otherwise it's hard for me to understand why you need two counters.
Does it make sense now? I updated my question. I need one counter for rows (vertcal) and one counter for cells (horizontal). I first write the firstname of all objects in the employeeList to the excel workbook. Then I wanna move one cell horizontally and write the sur-names of all objects in the list. Maybe my whole approch is wrong?
It cannot be simply done using two counters, because amount of columns is dependent of employee attributes (fName, SName, Address) which are not enumerable values. What's more while first iteration you are creating rows and while every other iteration you need to reuse previously created rows.
It is doable to first write only fNames to first column and later write another columns but your code will be more complicated than one posted above.
|

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.