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

可能的使用场景

如果您将无效的自定义数字格式分配给风格.定制property 那么 Aspose.Cells 将不会抛出任何异常。但是,如果您希望 Aspose.Cells 应该检查分配的自定义数字格式是否有效,请设置工作簿.设置.CheckCustomNumberFormat财产给真的.

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

以下示例代码将无效的自定义数字格式分配给风格.定制财产。由于我们已经设置工作簿.设置.CheckCustomNumberFormat财产给真的,因此 API 将抛出 CellsException 例如数字格式无效.

示例代码

// 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");