【发布时间】:2026-02-11 10:25:01
【问题描述】:
我有一个 ArrayList,其中包含有关员工的信息,例如 fName、sName 和地址。我正在使用 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
【问题讨论】:
标签: java excel arraylist listiterator