Solved

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

Posted on 2013-07-01
3
459 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
highlight duplicate entry 16 27
SQL Exceptions 3 35
Copy value from a certain cell 5 24
Subtotal clarification and between two sheets comparison 9 8
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

932 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

10 Experts available now in Live!

Get 1:1 Help Now