[]
        
(Showing Draft Content)

创建日期格式的单元格

该例子展示了GcExcel与POI创建worksheet的代码


GcExcel 创建日期格式的单元格

Workbook wb = new Workbook();
IWorksheet sheet1 = wb.getWorksheets().get(0);
sheet1.getRange(0, 0).setValue(new Date());
sheet1.getRange(0, 1).setValue(new Date());
sheet1.getRange(0, 1).setNumberFormat("m/d/yy h:mm");
sheet1.getRange(0, 2).setValue(Calendar.getInstance());
sheet1.getRange(0, 2).setNumberFormat("m/d/yy h:mm");
wb.save("TestOutput/GcExcel/CreatingDateCell.xlsx");


POI 创建日期格式的单元格

Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet1 = wb.createSheet("sheet1");

Row row = sheet1.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(new Date());

CellStyle style = wb.createCellStyle();
style.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(style);

cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(style);

try (OutputStream fileOut = new FileOutputStream("TestOutput/Poi/CreatingDateCell.xlsx")) {
    wb.write(fileOut);
} catch (IOException e) {
    throw new RuntimeException(e);