Java POI Excel 创建新列和新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18213657/
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-11 23:22:09  来源:igfitidea点击:

Java POI Excel creating new column and new rows

javaapache-poi

提问by Doc Holiday

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= 0at the end of the loop, it look like:

如果我删除row= 0循环末尾的 ,它看起来像:

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

回答by Joseph Helfert

You are incrementing the row index wrong. row++ will increment after you create the second row. So you are creating two rows at index 0. If you change all your row++ to ++row it should work.

您错误地增加了行索引。创建第二行后,row++ 将增加。因此,您在索引 0 处创建了两行。如果将所有 row++ 更改为 ++row,它应该可以工作。

回答by Jonathan Drapeau

You're creating a row each time, you want to check if the row exists first. Something like this will work.

您每次都创建一行,您想先检查该行是否存在。像这样的事情会起作用。

      myRow = sheet.getRow((short)row);
      if (myRow == null) {
        myRow = sheet.createRow((short)row);
      }

Also, in your current code, each time you're recreating the row at 0 twice.

此外,在您当前的代码中,每次您在 0 处重新创建行两次。

  Row myRow = sheet.createRow ((short)row);

  myRow.createCell(k).setCellValue (dataList.getVal())); 
  myRow = sheet.createRow ((short)row++); // Here, you're creating a new row at index 0

akokskis answer is good, actually better, either create them as he did before iterating or check if it exists first.

akokskis 的回答很好,实际上更好,要么像他在迭代之前那样创建它们,要么首先检查它是否存在。

回答by akokskis

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.

在循环的每次迭代中,您都在索引 0 和 1 处重新创建行。重新创建这些行时,您将清除所有现有数据。

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

回答by rozar

When you iterate through the first list. You have created the rows. The next time when you iterate you have to update the rows instead of creating it again. When you create new row, its going to add at the end.

当您遍历第一个列表时。您已经创建了行。下次迭代时,您必须更新行而不是再次创建行。当您创建新行时,它会在最后添加。

When iterating through List1, when you reached at the end you have already created three new rows. If you now make row = 0 and iterate through the second list List2. It is going to add a new row at 0 and so on till it reaches the end of List2.

在遍历 List1 时,当您到达最后时,您已经创建了三个新行。如果您现在使 row = 0 并遍历第二个列表 List2。它将在 0 处添加一个新行,依此类推,直到到达 List2 的末尾。

If you have not made rows = 0 at the end of the iteration. New rows will be added at the end.

如果在迭代结束时还没有使行 = 0。最后将添加新行。

int row = 0;
int k = 1; 

ArrayList<Row> rows = new ArrayList<Row>();
for(int i = 0; i<someList.size();i++){
    Row myRow = sheet.createRow(i);
    rows.add(myRow);
    myRow = null;
}

for (List dataList: someList) {
    for(Row row : rows){
        row.createCell(k).setCellValue(dataList.getVal()));         
    }

    k++;
}


     Col1    Col2    Col3    Col4
Row1   1
Row2   2
Row3          3 
Row4          4

or 

     Col1    Col2    Col3    Col4
Row1   1       3
Row2   2       4
Row3           
Row4          

回答by Doc Holiday

this is the solution that worked:

这是有效的解决方案:

myRow = sheet.getRow(row);

if(null == myRow)
{

    myRow=sheet.createRow(row);
}