从ArrayList写入Excel工作表ListIterator

时间:2014-04-12 15:08:47

标签: java excel arraylist listiterator

我有一个ArrayList,它保存有关员工的信息,例如fName,sName和adress。我正在使用apache poi将其写入Excel工作簿。

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

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

以下是我的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());

//尝试在正确的单元格中添加员工的姓氏。

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

这是我在这里发表的第一篇文章,所以如果我写得不好或做得不好,请告诉我该做什么,这样你们就很容易理解我的问题。

如果您需要查看更多代码,请参阅项目链接:http://1drv.ms/1kSGfDm

1 个答案:

答案 0 :(得分:1)

您没有发布如何添加其他单元格。尝试这样的事情:

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);
    }
}
相关问题