We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Removeat( ) cause error when trying to remove sheets

I am having issues with the removeat() function. I have 3 sheets in my excel designer file. I create an excel sheet and name it “Work Activity Analysis.” When I try to delete the other two sheets using the Removeat() function in a loop I get odd results. The excel file downloads what’s on the current web page instead of what I specify in the create excel function. It almost appears as if the sheet that gets created is also getting removed. When I removed the code below, the excel import works fine, but of course it leaves all 3 sheets in. My only other option is to create an excel designer file for each sheet.


For i = 0 To excel.Worksheets.Count - 1
If (i >= excel.Worksheets.Count) Then
Exit For
End If
If excel.Worksheets(i).Name <> “Work Activity Analysis” Then
excel.Worksheets.RemoveAt(i)
End If
Next

We don’t recommend to use RemoveAt() method for there may be some weird contents in designer files. If you have to use it, please email me your designer file and more of your code. I will investigate it. Thanks for your cooperation.

@apd8x,
Aspose.Cells has replaced Aspose.Excel which is deprecated now. This new product contains exciting features and supports all the versions of MS Excel. Using this new product you can remove worksheets using index and name both without any error. Here is a sample code for your reference:

// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);

// Removing a worksheet using its sheet index
workbook.Worksheets.RemoveAt(0);

// Save workbook
workbook.Save(dataDir + "output.out.xls");

Aspose.Cells supports managing worksheets in a variety of ways as follows:
Manage Worksheets

For testing purpose you may download the latest version of this product here:
Aspose.Cells for .NET (Latest Version)

A ready to run solution is prepared for user ease which can be downloaded here.