Java 从 ArrayList 写入 Excel 表格 ListIterator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23032121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:09:59  来源:igfitidea点击:

Writing from ArrayList to Excel sheet ListIterator

javaexcelarraylistlistiterator

提问by MelbyJim

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.

我得到了一个 ArrayList,其中包含有关员工的信息,例如 fName、sName 和地址。我正在使用 apache poi 将其写入 Excel 工作簿。

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.

在 Excel 工作表中,我希望一位员工的所有信息都在同一行中。我设法在第 1 行和第 0 单元格(第 0 行包含标题)中写出名字。一切都在垂直良好的秩序。当尝试添加姓氏时,一切都开始在这里和那里跳跃。

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

如何将 sName 写在正确的位置?

Here is part of my excel code

这是我的excel代码的一部分

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

如果您需要查看更多代码,这里是该项目的链接:http: //1drv.ms/1kSGfDm

采纳答案by enterbios

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);
    }
}