Poi CellType格式单元格

问题描述 投票:0回答:1

我看起来很周围,没有找到我的问题的答案。

我尝试使用自定义CellType格式化Cell,是否有人知道它是否可行。

我尝试在我的单元格上使用自定义格式'[HH]:MM'。

据我所知,只能使用预定义的CellTypes。有谁知道更多,可以打扰我?

提前致谢

java excel apache-poi
1个回答
0
投票

下面是一些代码的示例,它们将执行您要实现的目标。

    Workbook w = new XSSFWorkbook();
    CreationHelper createHelper = w.getCreationHelper();
    CellStyle cs = w.createCellStyle();
    cs.setDataFormat(createHelper.createDataFormat().getFormat("HH:mm"));
    Sheet sheet = w.createSheet(String.valueOf("Format Test"));
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellStyle(cs);
    cell.setCellValue(new Date());
    File outFile = new File("Test.xlsx");
    try {
        FileOutputStream fileOut = new FileOutputStream(outFile);
        w.write(fileOut);
    } catch (IOException ex) {
        // do something with exception data
    }
© www.soinside.com 2019 - 2024. All rights reserved.