ArgumentOutOfRange exception with ExcelStyles.CreateNamedStyle()

Aug 25, 2015 at 4:07 PM
EPPlus is fantastically useful, so many thanks to everyone concerned to start.

I have a problem with named styles in 4.0.4, though. I pulled the default source branch today and built to do some digging. Using some of the code from EPPlus\Worksheet.cs, I've boiled it down to this fragment:
using ( ExcelPackage _pck = new ExcelPackage())
{
  var wsSheet = _pck.Workbook.Worksheets.Add("NamedStyles");
  var firstNamedStyle = _pck.Workbook.Styles.CreateNamedStyle("templateFirst");
  var s = firstNamedStyle.Style;
  s.HorizontalAlignment = ExcelHorizontalAlignment.CenterContinuous;

  var secondNamedStyle _pck.Workbook.Styles.CreateNamedStyle("first",firstNamedStyle.Style).Style;
}
Run this, and the final call to CreateNamedStyle() throws ArgumentOutOfRangeException.

However, if you omit setting the HorizontalAlignment property, so the first style is unmodified, it works:
using ( ExcelPackage _pck = new ExcelPackage())
{
  var wsSheet = _pck.Workbook.Worksheets.Add("NamedStyles");
  var firstNamedStyle = _pck.Workbook.Styles.CreateNamedStyle("templateFirst");
  var s = firstNamedStyle.Style;
 
  var secondNamedStyle _pck.Workbook.Styles.CreateNamedStyle("first",firstNamedStyle.Style).Style;
}
The exception comes from ExcelStyles.cs @ line 881:
xfs = style.CellXfs[styleID];
In the working case, styleID is 0 and the CellXfs collection contains 1 member.

In the failed case, styleID is 2 and the CellXfs collection contains 2 members.

Looking at the style objects, the properties of firstNamedStyle.Style are fine until something is modified, and then all except the one set throw ArgumentOutOfRangeException in the debugger.

I didn't find this as a known issue.

Thanks

Alan
Aug 26, 2015 at 1:21 PM
I'm having problems with styles as well. If I call:

pck.Workbook.Styles.CreateNamedStyle("TableNumber"):

and then try to save the package I get a NullreferenceException "Object reference not set to an instance of an object." somewhere in ExcelPackage.Save().