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

可能的使用场景

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

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

以下示例代码向 Style.Custom 属性分配了一个无效的自定义数字格式。由于我们已将 Workbook.Settings.CheckCustomNumberFormat 属性设置为 true,因此会抛出异常,例如无效的数字格式。请阅读代码内的注释以获取更多帮助。

示例代码

// 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