在设置Style.Custom属性时检查自定义数字格式

可能的使用场景

如果将无效的自定义数字格式分配给Style.Custom属性,则Aspose.Cells不会抛出任何异常。但是,如果您想要Aspose.Cells检查分配的自定义数字格式是否有效,则请将Workbook.Settings.CheckCustomNumberFormat属性设置为true

设置Style.Custom属性时检查自定义数字格式

以下示例代码将无效的自定义数字格式分配给Style.Custom属性。由于已将Workbook.Settings.CheckCustomNumberFormat属性设置为true,因此API将抛出CellsException,例如Invalid number format

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create a workbook
Workbook wb = new Workbook();
// Setting this property to true will make Aspose.Cells to throw exception
// when invalid custom number format is assigned to Style.Custom property
wb.getSettings().setCheckCustomNumberFormat(true);
// Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
// Access cell A1 and put some number inside it
Cell c = ws.getCells().get("A1");
c.putValue(2347);
// Access cell's style and set its Style.Custom property
Style s = c.getStyle();
try {
// This line will throw exception if
// Workbook.Settings.CheckCustomNumberFormat is set to true
s.setCustom("ggg @ fff"); // Invalid custom number format
c.setStyle(s);
}
catch (Exception ex) {
System.out.println("Exception Occured");
}
System.out.println("Done");