Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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