ArgumentOutOfRangeException in OfficeOpenXml.ExcelStyles.CreateNamedStyle()

May 11, 2011 at 5:23 AM

I'm calling OfficeOpenXml.ExcelStyles.CreateNamedStyle() with the template overload and running into an ArgumentOutOfRange exception.

My code:

ExcelRange styleRng = worksheet.Cells["A" + emptyRow + ":" + atlaswebCol + emptyRow];
ExcelStyle tempStyle = styleRng.Style;
var namedStyle = xlPackage.Workbook.Styles.CreateNamedStyle("HyperLink", tempStyle);  

It is ultimately blowing up here:

public class ExcelStyleCollection<T> : IEnumerable<T>
{
/*
 ...
*/
public T this[int PositionID]
{
    get
    {
        return _list[PositionID]; <---
    }
}
this.PositionID = 8, whereas this.Count = 2;
EPPlus.dll!OfficeOpenXml.Style.ExcelStyle.ExcelStyle(OfficeOpenXml.ExcelStyles styles = {OfficeOpenXml.ExcelStyles}, OfficeOpenXml.XmlHelper.ChangedEventHandler ChangedEvent = {Method = {System.Reflection.RuntimeMethodInfo}}, int PositionID = -1, string Address = "HyperLink", int xfsId = 8)
I wasn't able to dig too deeply, but it looks like the we are passing the wrong value to the ctor. In the ExcelStyle ctor styles._styleXml.InnerXml contains, amongst other things:
<cellStyles count="2">
  <cellStyle name="Hyperlink" xfId="1" builtinId="8" /> 
  <cellStyle name="Normal" xfId="0" builtinId="0" /> 
  </cellStyles>

Are we possibly swapping the style ID for the index here?
Coordinator
May 12, 2011 at 6:48 PM

Hi

Currently templates only works if you have another namedstyle, not a style from a cell.

This is a bug, so I'll add a fix for it.

 

Feb 7, 2013 at 8:33 AM
Hi,

Is this bug getting fix now?

If it's not, how can I make a cell's original style into a named style?

Thank you.