java - 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));
}
解决方案
这是您正在使用的代码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));
}
因为你没有输入你想要的输出,我猜这可能是你想要的
推荐阅读
- amazon-web-services - 无法在 Elastic Beanstalk 上升级平台版本
- c - 如何在使用自由函数之前清理 C 中分配的内存?
- git - 从远程存储库中删除文件。留下删除的行?天蓝色?
- python - '@error: Solution Not Found' 用于估计方程组
- reactjs - 将 DevExtreme Dialog / Popup 组件重写为 MaterialUI 等效项(react / javascript)
- php - 如何按 Laravel 中有很多的最新 created_at 列按关系排序
- python - Elastic Search Python:必须包含所有单词并按单词顺序排序到结果
- powershell - Azure 分析服务数据模型的 Azure Datalake Gen1 连接因通过最终用户身份验证实施而失败
- c# - 将 MVC 身份验证传递给 DirectoryEntry
- javascript - Tampermonkey 不使用 localhost HTTPS 静态文件获取 @require