无法使用 Java 正确地将数据从 ArrayList 导出到 Excel 个单元格

not able to export data from ArrayList To Excel cells correctly using Java

我想从我的 2d ArrayList 导出数据,如代码所示:

ArrayList<ArrayList<Double>> bpDataAllLists = messtisch.getMeasDataBrushP();
    for (ArrayList<Double> bpDataList : bpDataAllLists) {
        int bp = ((bpDataAllLists.indexOf(bpDataList)) + 1);
        int measPerP = messtisch.getBpMeasPerPoint();

        HSSFWorkbook bprofileWb = new HSSFWorkbook();
        HSSFSheet mainsheet = bprofileWb.createSheet("Meting");

        HSSFRow bprofileRow = mainsheet.createRow(5);
        HSSFCell bprofilecell = bprofileRow.createCell(3);
        time = messtisch.getMeastime();
        formater = new SimpleDateFormat("yyyy-MM-dd");
        String date = formater.format(time.getTime());
        bprofilecell.setCellValue(date);

        int sCol = 7;
        for (int j = 1; j < measPerP + 1; j++) {
            // TODO create measperpoint colmn
            int sRow = 15;
            int bpPointsSize = bpDataList.size();
            for (int k = j - 1; k < bpPointsSize; k += measPerP) {
                bprofileRow = mainsheet.createRow(sRow);
                bprofilecell = bprofileRow.createCell(sCol);
                bprofilecell.setCellValue(bpDataList.get(k));
                System.out.printf("Row: %d, Column: %d, Value: %f\n",sRow,sCol, bpDataList.get(k));
                sRow+=1;
            }

            sCol+=1;
        }
    }
}

通过打印功能,数据正确如下:

行:15,列:7,值:12,000000

行:16,列:7,值:12,000000

行:17,列:7,值:12,000000

行:18,列:7,值:12,000000

行:19,列:7,值:12,000000

行:20,列:7,值:12,000000

行:21,列:7,值:12,000000

行:22,列:7,值:12,000000

行:23,列:7,值:12,000000

行:24,列:7,值:12,000000

行:25,列:7,值:12,000000

行:26,列:7,值:12,000000

行:27,列:7,值:12,000000

行:28,列:7,值:12,000000

行:29,列:7,值:12,000000

行:30,列:7,值:12,000000

行:31,列:7,值:12,000000

行:32,列:7,值:12,000000

行:33,列:7,值:12,000000

行:34,列:7,值:12,000000

行:35,列:7,值:12,000000

行:36,列:7,值:12,000000

行:37,列:7,值:12,000000

行:38,列:7,值:12,000000

行:15,列:8,值:14,000000

行:16,列:8,值:14,000000

行:17,列:8,值:14,000000

行:18,列:8,值:14,000000

行:19,列:8,值:14,000000

行:20,列:8,值:14,000000

行:21,列:8,值:14,000000

行:22,列:8,值:14,000000

行:23,列:8,值:14,000000

行:24,列:8,值:14,000000

行:25,列:8,值:14,000000

行:26,列:8,值:14,000000

行:27,列:8,值:14,000000

行:28,列:8,值:14,000000

行:29,列:8,值:14,000000

行:30,列:8,值:14,000000

行:31,列:8,值:14,000000

行:32,列:8,值:14,000000

行:33,列:8,值:14,000000

行:34,列:8,值:14,000000

行:35,列:8,值:14,000000

行:36,列:8,值:14,000000

行:37,列:8,值:14,000000

行:38,列:8,值:14,000000

行:15,列:9,值:16,000000

行:16,列:9,值:16,000000

行:17,列:9,值:16,000000

行:18,列:9,值:16,000000

行:19,列:9,值:16,000000

行:20,列:9,值:16,000000

行:21,列:9,值:16,000000

行:22,列:9,值:16,000000

行:23,列:9,值:16,000000

行:24,列:9,值:16,000000

行:25,列:9,值:16,000000

行:26,列:9,值:16,000000

行:27,列:9,值:16,000000

行:28,列:9,值:16,000000

行:29,列:9,值:16,000000

行:30,列:9,值:16,000000

行:31,列:9,值:16,000000

行:32,列:9,值:16,000000

行:33,列:9,值:16,000000

行:34,列:9,值:16,000000

行:35,列:9,值:16,000000

行:36,列:9,值:16,000000

行:37,列:9,值:16,000000

行:38,列:9,值:16,000000

但是在 Excel 导出中,我得到了如图 excel screenshot 所示的结果。我无法得到,我做错了什么。请帮助。

每次我创建一行时,都会创建一个新行并覆盖以前的数据。所以我只需要创建一次行。如果该行存在,则写入同一行。

通过使用此方法:

 private HSSFRow getRow(HSSFSheet sheet, int rowNum) {
 HSSFRow sheetRow = null;
 sheetRow = sheet.getRow(rowNum);
  if (null == sheetRow) {
    sheetRow = sheet.createRow(rowNum);
 }  
 return sheetRow;
 }

所以现在,我将 mainsheet.createRow(sRow); 更改为 getRow(mainsheet, sRow); 成功了。