create a new excel workbook and add x number of worksheets using c#

using c# in vs2010.  I need to create a new excel workbook with an unknown number of worksheets.  This is what I'm currently doing:
            Application xls = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = xls.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Worksheet ws = (Worksheet)wb.Worksheets[1];

I actually need to be able to create, say 10, worksheets and then cycle among them.

Each worksheet is a single record from a dataset, hence the number of worksheets depends on the number of records.
dhenderson12Asked:
Who is Participating?
 
TheAvengerConnect With a Mentor Commented:
What about adding more sheets? You can find some examples how to do that here: http://stackoverflow.com/questions/193092/c-sharp-how-to-add-excel-worksheet-programatically-office-xp-2003.

Note that the first sheet has an index 0, not 1.
0
 
dhenderson12Author Commented:
thanks for the reply.  please note that the code I posted is already in production and works fine, so I don't think you're correct about the index.

I will try adding them individually as suggested.
0
 
TheAvengerCommented:
That is possible. However check it to be sure. When you create a new Excel file it has three sheets by default, so the sheet with index 1 exists and will not fail.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.