Free Support Forum - aspose.com

Ordering of worksheets in workbook

In the .NET version, I'm trying to control the order of worksheets inside a workbook. I want them to appear in the order in which I create them. I tried to use worksheet.Move to move the newly-created sheet to the end but I get unpredictable results. It appears that Aspose is sorting the list by name under certain conditions. How can I control this behavior?

Thanks,

--Howard

Hi Howard,

I don't find any issue with the ordering of the worksheets in a workbook. It is set the way I am creating the worksheets in the workbook. For your info, if you create a workbook from the scratch, a default worksheet named "Sheet1" is automatically created, so you have keep in mind that and add new worksheets keeping in mind that or you 'd better clear the worksheets before adding new ones.

E.g.,

Workbook workbook = new Workbook();
workbook.Worksheets.Clear();
for (int i = 0;i<20;i++)
{
workbook.Worksheets.Add();
Worksheet worksheet = workbook.Worksheets[i];
.
.
}
workbook.Save("d:\\test\\twentysheets.xls");

If you still find any issue, could you create a sample code to reproduce the unpredictability.

Thank you.

Yes, I know about the extra initial sheet and I do clear the collection. I can't easily give you a small example but try this:

int idx;
idx=workbook.Worksheets.Add();
workbook.Worksheets[idx].Name="Z";
idx=workbook.Worksheets.Add();
workbook.Worksheets[idx].Name="A";

then see what the order of sheets is. In my experience the "A" sheet will be first.

Thanks,
--Howard

Hi,

No, Z worksheet will be the first one as I tested. See the attachment and I used the following code:

Workbook workbook = new Workbook();
workbook.Worksheets.Clear();
int idx;
idx=workbook.Worksheets.Add();
workbook.Worksheets[idx].Name="Z";
idx=workbook.Worksheets.Add();
workbook.Worksheets[idx].Name="A";
workbook.Save("d:\\test\\orderingsheets.xls");
Which version you are using, kindly try the latest version 4.4.1
Thank you.

Ooops, I found the fault in my code. Aspose seems to be working just fine. I apologize for the inconvenience and appreciate the rapid support.

--Howard