Solved

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

Posted on 2013-07-01
3
469 Views
Last Modified: 2013-07-02
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.
0
Comment
Question by:dhenderson12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 39291626
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
 

Author Comment

by:dhenderson12
ID: 39291886
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
 
LVL 20

Expert Comment

by:TheAvenger
ID: 39292251
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question