Solved

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

Posted on 2013-07-01
3
456 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
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now