e_star_ 2020-02-06 15:27 采纳率: 0%
浏览 206

excel poi 日期头和下边指定的日期数据对应

excel poi 怎么能做到 表头是日期 下边指定日期的数据和表头的日期显示同一列下
图片说明
比如图片中的红框中的1 2 3是2019年12月份的1号,2号,3号。下边红色框中的2019-12-11 08:05:48.0 让它显示到 表头11下边的单元格上

  • 写回答

1条回答 默认 最新

  • 老僧& 2023-10-08 19:08
    关注
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class ExcelDateHeaderExample {
        public static void main(String[] args) {
            try (Workbook workbook = new XSSFWorkbook()) {
                Sheet sheet = workbook.createSheet("DataSheet");
    
                // 创建日期格式化对象,以便在表头中显示日期
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
                // 创建表头行
                Row headerRow = sheet.createRow(0);
    
                // 创建日期范围(示例:2019-12-01 到 2019-12-31)
                Date startDate = dateFormat.parse("2019-12-01");
                Date endDate = dateFormat.parse("2019-12-31");
    
                // 创建并填充表头单元格
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                int colIndex = 1; // 从第2列开始
                while (!calendar.getTime().after(endDate)) {
                    Cell headerCell = headerRow.createCell(colIndex);
                    headerCell.setCellValue(dateFormat.format(calendar.getTime()));
                    colIndex++;
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                }
    
                // 填充数据,示例数据
                // 在与日期对应的单元格中填充数据(此处假设日期格式为 "yyyy-MM-dd")
                Row dataRow = sheet.createRow(1);
                Cell dataCell = dataRow.createCell(0);
                dataCell.setCellValue("2019-12-11 08:05:48.0");
    
                // 此处您可以添加更多数据行并填充相应的日期单元格
    
                // 保存工作簿到文件
                try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) {
                    workbook.write(outputStream);
                }
    
                System.out.println("Excel文件已生成。");
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符