java生成日历格式excel

在这里插入图片描述


```java
public void calenderTest(HttpServletResponse res, IntegrationPlan integrationPlan) throws IOException {
        BufferedOutputStream outputStream = null;
        String downloadFileName = URLEncoder.encode("notes", "UTF-8");
        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/x-execl");
        res.setHeader("Content-Disposition",
                "attachment;filename=" + new String((downloadFileName).getBytes(), "UTF-8") + ".xlsx");
        // 创建新的Excel工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表(sheet)
        Sheet sheet = workbook.createSheet("Calendar");

        // 创建一个单元格样式(CellStyle)
        // 星期格式
        CellStyle weekStyle = getCellStyle(workbook, true, 16, "黑体", BorderStyle.MEDIUM);
        // 日期格式
        CellStyle dateStyle = getCellStyle(workbook, true, 16, "宋体", BorderStyle.THIN);
        // 内容格式
        CellStyle infoStyle = getCellStyle(workbook, false, 16, "宋体", BorderStyle.MEDIUM);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        // 固定星期表头
        for (int i = 0; i < 7; i++) {
            headerRow.createCell(i).setCellValue(WeekDateEnum.of(i + 1).getName());
            headerRow.getCell(i).setCellStyle(weekStyle);
            headerRow.setHeightInPoints(40);
            sheet.setColumnWidth(i, 40 * 256);
        }

        String checkMonth = integrationPlan.getCheckMonth();
        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");

        // 解析字符串为Date类型
        Date date = null;
        try {
            date = dateFormat.parse(checkMonth);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        // 将Date转换为Calendar
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 填充日历数据
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zone).toLocalDate();
        // 获取当前是星期几,以数字表示
        int startCell = localDate.getDayOfWeek().getValue() - 1;
        // 日期行
        Row currentRow = sheet.createRow(1);
        // 内容行
        Row nextRow = sheet.createRow(2);
        // 当前月份
        int month = calendar.get(Calendar.MONTH) + 1;
        // 当月天数
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int dayCount = 1;
        while (dayCount <= day) {
            currentRow.createCell(startCell).setCellValue(month + "月" + dayCount + "日");
            currentRow.getCell(startCell).setCellStyle(dateStyle);
            // 企业数据填充
            nextRow.createCell(startCell).setCellValue("111");
            nextRow.getCell(startCell).setCellStyle(infoStyle);
            nextRow.setHeightInPoints(250);

            startCell++;
            dayCount++;
            if (startCell == 7) {
                currentRow = sheet.createRow(currentRow.getRowNum() + 2);
                nextRow = sheet.createRow(nextRow.getRowNum() + 2);
                startCell = 0;
            }
        }
        // 导出到文件
        outputStream = new BufferedOutputStream(res.getOutputStream());
        workbook.write(outputStream);
        outputStream.flush();

        // 关闭工作簿资源
        workbook.close();

    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值