无法使用Java

时间:2017-09-13 16:39:26

标签: java export-to-excel poi-hssf

我想从我的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所示的内容。我无法得到,我做错了什么。请帮忙。

1 个答案:

答案 0 :(得分:0)

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

使用此方法:

 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); 它奏效了。