Java POI Excel 创建新列和新行 [英] Java POI Excel creating new column and new rows
问题描述
好的,所以我迭代了一个列表,而不是水平地将值插入到单元格中,我将值垂直地放在单元格中.
Ok so Im iterating over a list and instead of inserting values into cells horizontally, im putting the values in the cells vertically.
它第一次通过列表工作正常,但是当我第二次进入列表时,它吹走了第一个列表并将其替换在第二列中.
It works fine for the first time through the list but when I go in the list the 2nd time it blows away the first list and replaces it in the 2nd column.
如果我删除循环末尾的 row=0
,它看起来像:
if i remove the row= 0
at the end of the loop, it look like:
val 1
val 2
val 1
val 2
==========
int row = 0;
int k = 1;
for (List dataList: someList) {
Row myRow = sheet.createRow ((short)row);
myRow.createCell(k).setCellValue (dataList.getVal()));
myRow = sheet.createRow ((short)row++);
myRow.createCell(k).setCellValue (dataList.getSecVal()));
myRow = sheet.createRow ((short)row++);
k++;
row = 0;
}
推荐答案
在循环的每次迭代中,您都在索引 0 & 处重新创建行.1. 当您重新创建这些行时,您将清除所有现有数据.
On each iteration of your loop, you're recreating the rows at index 0 & 1. When you re-create these rows, you're going to blow away all of your already existing data.
尝试这样的事情:
int k = 1;
Row myRow1 = sheet.createRow(0); //first row of the document
Row myRow2 = sheet.createRow(1);
for (List dataList: someList) {
myRow1.createCell(k).setCellValue (dataList.getVal()));
myRow2.createCell(k).setCellValue (dataList.getSecVal()));
k++;
}
这篇关于Java POI Excel 创建新列和新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!