首页 > 解决方案 > apache poi - 如何在同一个单元格中编写并继续循环行

问题描述

我的循环有一些问题。我想做的是我想写下整个 12 月的日期,然后是 2019 年的总日期。但是我现在遇到的问题是 2019 年的总日期没有显示在与 2018 年 12 月总天数相同的列的行下

我当前的输出(2018 年 12 月 31 日的行号落在第 34 行:

MONTH
------
2018-12-01
2018-12-02
2018-12-03
2018-12-04
...
2018-12-31

我认为我的问题在于 getRow 和循环。请帮我。

// month

    int maxDay = cal1.getActualMaximum(Calendar.DAY_OF_MONTH);

    for (int dec = 1; dec <= maxDay; dec++) {

        Row row3 = sheet.getRow(dec + 2);

        Cell cell1 = row3 .createCell(2);

        cal1.set(Calendar.YEAR, 2018);
        cal1.set(Calendar.MONTH, 11);
        cal1.set(Calendar.DAY_OF_MONTH, 1);

        cal1.set(Calendar.DAY_OF_MONTH, dec);

        java.util.Date date1 = cal1.getTime();

        cell1.setCellValue(formatter.format(date1));
    }

    // for total date of a year 2018

    for (int notdec = 1; notdec <= 365; notdec++) {

        Row row3 = sheet.getRow(maxDay+3);  

        Cell cell1 = row3.getCell(2);

        cal.set(Calendar.YEAR, 2019);

        cal.set(Calendar.DAY_OF_YEAR, notdec);

        java.util.Date date = cal.getTime();

        cell1.setCellValue(formatter.format(date));

    }

标签: javaapache-poi

解决方案


这是您正在使用的代码2018

for (int dec = 1; dec <= maxDay; dec++) {
    Row row3 = sheet.getRow(dec + 2);
    Cell cell1 = row3 .createCell(2);
    cal1.set(Calendar.YEAR, 2018);
    cal1.set(Calendar.MONTH, 11);
    cal1.set(Calendar.DAY_OF_MONTH, 1);
    cal1.set(Calendar.DAY_OF_MONTH, dec);
    java.util.Date date1 = cal1.getTime();

    cell1.setCellValue(formatter.format(date1));
}

因此,要在此继续,首先使用相同的代码并更改需要更改的内容是合乎逻辑的

for (int notdec = 1; notdec <= 365; notdec++) {

    Row row3 = sheet.getRow(maxDay +3 + notdec);  // THIS

    Cell cell1 = row3.createCell(2);      // THIS

    cal1.set(Calendar.YEAR, 2019);       // THIS
    cal1.set(Calendar.DAY_OF_YEAR, notdec);   // THIS
    java.util.Date date1 = cal1.getTime();

    cell1.setCellValue(formatter.format(date1));
}

因为你没有输入你想要的输出,我猜这可能是你想要的


推荐阅读