Java POI Excel creating new column and new rows

后端 未结 5 895
慢半拍i
慢半拍i 2021-01-16 05:09

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 ti

5条回答
  •  无人及你
    2021-01-16 06:01

    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.

      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.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题