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

可能的使用场景

如果您将无效的自定义数字格式分配给风格.定制属性,则 Aspose.Cells 不会抛出任何异常。但是如果你想让 Aspose.Cells 检查指定的自定义号码格式是否有效,那么请设置工作簿.设置.CheckCustomNumberFormat财产给真的.

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

以下示例代码将无效的自定义数字格式分配给风格.定制财产。因为,我们已经设定工作簿.设置.CheckCustomNumberFormat财产给真的,因此它会抛出异常,例如无效的数字格式。请阅读代码中的注释以获得更多帮助。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Create an instance of Workbook class
Workbook book = 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
book.Settings.CheckCustomNumberFormat = true;
// Access first worksheet
Worksheet sheet = book.Worksheets[0];
// Access cell A1 and put some number to it
Cell cell = sheet.Cells["A1"];
cell.PutValue(2347);
// Access cell's style and set its Style.Custom property
Style style = cell.GetStyle();
// This line will throw exception if Workbook.Settings.CheckCustomNumberFormat is set to true
style.Custom = "ggg @ fff"; //Invalid custom number format