Bug: Worksheets.AddCopy() with resized column in designer

I get a “Object must be of type Byte” error when executing the following code. Exception is thrown on the “Save” method.



string licenseFile = Server.MapPath(@"~\Licence\Aspose.Excel.lic");
Excel excel = new Excel(licenseFile, this);

// Load the template file.
string templateFile = Server.MapPath(@"~\Template.xls");
excel.Open(templateFile);

excel.Worksheets[0].Cells[0,0].PutValue(“Hello World”);
excel.Worksheets.AddCopy(0);

excel.Save(@“Test.xls”, SaveType.OpenInBrowser, FileFormatType.Default, this.Response);


Note that the code works if “Template.xls” is a brand new Excel document, but as soon as I manually adjust the width of column A (make it wider) in the template file I get the above error. The error does not appear unless I execute both PutValue("…") and AddCopy(0).

Aspose.Excel version 1.8.4.0. Excel 2002 SP-2.

The full error text follows:

_____________________________________________________________
[ArgumentException: Object must be of type Byte.]
System.Byte.CompareTo(Object value) +128
System.Collections.Comparer.Compare(Object a, Object b) +150
System.Array.BinarySearch(Array array, Int32 index, Int32 length, Object value, IComparer comparer) +261

[InvalidOperationException: Specified IComparer threw an exception.]
System.Array.BinarySearch(Array array, Int32 index, Int32 length, Object value, IComparer comparer) +322
System.Collections.SortedList.IndexOfKey(Object key) +29
System.Collections.SortedList.get_Item(Object key) +9
n.a(Int32 A_0)
Aspose.Excel.Cell.a(b4 A_0, FileFormatType A_1)
Aspose.Excel.Cells.d(b4 A_0, FileFormatType A_1)
Aspose.Excel.Worksheet.c(b4 A_0)
Aspose.Excel.Worksheets.d(b4 A_0)
Aspose.Excel.Worksheets.a(String A_0, SaveType A_1, FileFormatType A_2, HttpResponse A_3)



Please download Aspose.Cells for .NET (Latest Version) and have a try.

Laurence, thanks for the quick turnaround. It’s all working fine now.

– Josh

@Josh_Gallagher,
Aspose.Excel is deprecated now and a new version of the product is introduced known as Aspose.Cells. This new product contains all the features supported by the discontinued product as well as the features supported by the latest versions of MS Excel. Aspose.Cells supports adding copy of worksheet as demonstrated in the following sample code:

string InputPath = dataDir + "book1.xls";

// Open an existing Excel file.
Workbook wb = new Workbook(InputPath);

// Create a Worksheets object with reference to
// the sheets of the Workbook.
WorksheetCollection sheets = wb.Worksheets;

// Copy data to a new sheet from an existing
// sheet within the Workbook.
sheets.AddCopy("Sheet1");

// Save the Excel file.
wb.Save(dataDir + "CopyWithinWorkbook_out.xls");

Further details can be obtained here:
Copying and Moving Worksheets

Latest product version is available here:
Aspose.Cells for .NET (Latest Version)

A ready to run solution is available here.