Java的POI Excel中创建新列,新行 [英] Java POI Excel creating new column and new rows
问题描述
好了,所以林遍历列表,而不是值插入细胞水平,即时把值单元格垂直。
它工作正常,第一次通过列表,但是当我在列表去第二次它吹走了一个列表,并在第2列替换它。
如果我删除行= 0
在循环结束时,它看起来像:
VAL 1
VAL 2
VAL 1
VAL 2
=========
INT行= 0;
INT K = 1;
对于(名单DataList控件:someList){
行myRow = sheet.createRow((短)行); myRow.createCell(K).setCellValue(dataList.getVal()));
myRow = sheet.createRow((短)行++); myRow.createCell(K).setCellValue(dataList.getSecVal()));
myRow = sheet.createRow((短)行++);
ķ++;
排= 0;
}
在你的循环,你在索引0&放重现行的每个迭代; 1.当您重新创建这些行,你会吹走所有已经存在的数据。
尝试是这样的:
时int k = 1;
行myRow1 = sheet.createRow(0); //文档的第一行
行myRow2 = sheet.createRow(1);
对于(名单DataList控件:someList){
myRow1.createCell(K).setCellValue(dataList.getVal()));
myRow2.createCell(K).setCellValue(dataList.getSecVal()));
ķ++;
}
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.
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;
}
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.
Try something like this:
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屋!