473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I create a sheet in Excel from VB.net?

KC
Does anybody know how to add a specific number of worksheets to an Excel
spreadsheet through VB.net? I'm trying to export some datatables to an excel
file, but I only want as many sheets in the workbook as there are tables.

Right now the routine I'm tweaking from

http://support.microsoft.com/default...b;EN-US;306022

adds the default, (3).

At this stage I can export data fine, I just don't know how to control the
number sheets.

--
Ken
Nov 21 '05 #1
7 11553
Hi,

Dim oExcel As Microsoft.Office.Interop.Excel.Application

Dim oBook, oBook1 As Microsoft.Office.Interop.Excel.Workbook

Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet

'Start a new workbook in Excel.

oExcel = New Microsoft.Office.Interop.Excel.Application

oBook = oExcel.Workbooks.Add

oBook1 = oExcel.Workbooks.Add

'Add data to cells of the first worksheet in the new workbook.

oSheet = CType(oBook.Worksheets(1),
Microsoft.Office.Interop.Excel.Worksheet)

oSheet.Range("A1").Value = "Last Name"

oSheet.Range("B1").Value = "First Name"

oSheet.Range("C1").Value = "Price"

oSheet.Range("A1:B1").Font.Bold = True

oSheet.Range("A2").Value = "Doe"

oSheet.Range("B2").Value = "John"

oSheet.Range("C2").Value = 12345.456

oSheet.Range("C2").Cells.NumberFormat = "$0.00"

oSheet = CType(oBook.Worksheets(2),
Microsoft.Office.Interop.Excel.Worksheet)

oSheet.Range("A1").Value = "Last Name"

oSheet.Range("B1").Value = "First Name"

oSheet.Range("C1").Value = "Price"

oSheet.Range("A1:B1").Font.Bold = True

oSheet.Range("A2").Value = "Doe"

oSheet.Range("B2").Value = "John"

oSheet.Range("C2").Value = 12345.456

oSheet.Range("C2").Cells.NumberFormat = "$0.00"

'Save the Workbook and quit Excel.

oExcel.DisplayAlerts = False

oBook.SaveAs("c:\Book1.xls")

oSheet = Nothing

oBook = Nothing

oExcel.Quit()

oExcel = Nothing

GC.Collect()

Ken

-------------------------------------

"KC" <yo*@dontneed.this> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
Does anybody know how to add a specific number of worksheets to an Excel
spreadsheet through VB.net? I'm trying to export some datatables to an excel
file, but I only want as many sheets in the workbook as there are tables.

Right now the routine I'm tweaking from

http://support.microsoft.com/default...b;EN-US;306022

adds the default, (3).

At this stage I can export data fine, I just don't know how to control the
number sheets.

--
Ken

Nov 21 '05 #2
http://www.kjmsolutions.com/datasetarray.htm

________________________________

From: KC [mailto:yo*@dontneed.this]
Sent: Wednesday, August 25, 2004 4:55 PM
To: microsoft.public.dotnet.languages.vb
Subject: How do I create a sheet in Excel from VB.net?

Does anybody know how to add a specific number of worksheets to an Excel
spreadsheet through VB.net? I'm trying to export some datatables to an
excel
file, but I only want as many sheets in the workbook as there are
tables.

Right now the routine I'm tweaking from

http://support.microsoft.com/default...b;EN-US;306022

adds the default, (3).

At this stage I can export data fine, I just don't know how to control
the
number sheets.

--
Ken
Nov 21 '05 #3
On Wed, 25 Aug 2004 16:55:22 -0500, "KC" <yo*@dontneed.this> wrote:

¤ Does anybody know how to add a specific number of worksheets to an Excel
¤ spreadsheet through VB.net? I'm trying to export some datatables to an excel
¤ file, but I only want as many sheets in the workbook as there are tables.
¤
¤ Right now the routine I'm tweaking from
¤
¤ http://support.microsoft.com/default...b;EN-US;306022
¤
¤ adds the default, (3).
¤
¤ At this stage I can export data fine, I just don't know how to control the
¤ number sheets.

What is the data source from which you are exporting to Excel? Are you exporting directly from .NET
DataTables or from another database?
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #4
KC
This is directly from a datatable. Nothing fancy really.

Ken
"Paul Clement" <Us***********************@swspectrum.com> wrote in message
news:st********************************@4ax.com...
On Wed, 25 Aug 2004 16:55:22 -0500, "KC" <yo*@dontneed.this> wrote:

¤ Does anybody know how to add a specific number of worksheets to an Excel
¤ spreadsheet through VB.net? I'm trying to export some datatables to an excel ¤ file, but I only want as many sheets in the workbook as there are tables. ¤
¤ Right now the routine I'm tweaking from
¤
¤ http://support.microsoft.com/default...b;EN-US;306022
¤
¤ adds the default, (3).
¤
¤ At this stage I can export data fine, I just don't know how to control the ¤ number sheets.

What is the data source from which you are exporting to Excel? Are you exporting directly from .NET DataTables or from another database?
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)

Nov 21 '05 #5
KC
This I know how to do. I'm talking about controlling the number of
worksheets added.

If I want to add one (1) datatable worth of data, I only want to add one (1)
worksheet to the workbook.

The odd thing is, after adding a workbook, I do a count of the total
worksheets and get back five (5) - why it's adding 5 instead of the default
three I'm afraid to ask. I then try to delete sheets 2 thru 5, but get an
error deleting worksheet 4. It says 'Invalid Index'!? Even though it just
said there was a total count of 5 worksheets!?

That is where I stand.

Ken
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:ex**************@TK2MSFTNGP15.phx.gbl...
Hi,

Dim oExcel As Microsoft.Office.Interop.Excel.Application

Dim oBook, oBook1 As Microsoft.Office.Interop.Excel.Workbook

Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet

'Start a new workbook in Excel.

oExcel = New Microsoft.Office.Interop.Excel.Application

oBook = oExcel.Workbooks.Add

oBook1 = oExcel.Workbooks.Add

'Add data to cells of the first worksheet in the new workbook.

oSheet = CType(oBook.Worksheets(1),
Microsoft.Office.Interop.Excel.Worksheet)

oSheet.Range("A1").Value = "Last Name"

oSheet.Range("B1").Value = "First Name"

oSheet.Range("C1").Value = "Price"

oSheet.Range("A1:B1").Font.Bold = True

oSheet.Range("A2").Value = "Doe"

oSheet.Range("B2").Value = "John"

oSheet.Range("C2").Value = 12345.456

oSheet.Range("C2").Cells.NumberFormat = "$0.00"

oSheet = CType(oBook.Worksheets(2),
Microsoft.Office.Interop.Excel.Worksheet)

oSheet.Range("A1").Value = "Last Name"

oSheet.Range("B1").Value = "First Name"

oSheet.Range("C1").Value = "Price"

oSheet.Range("A1:B1").Font.Bold = True

oSheet.Range("A2").Value = "Doe"

oSheet.Range("B2").Value = "John"

oSheet.Range("C2").Value = 12345.456

oSheet.Range("C2").Cells.NumberFormat = "$0.00"

'Save the Workbook and quit Excel.

oExcel.DisplayAlerts = False

oBook.SaveAs("c:\Book1.xls")

oSheet = Nothing

oBook = Nothing

oExcel.Quit()

oExcel = Nothing

GC.Collect()

Ken

-------------------------------------

"KC" <yo*@dontneed.this> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
Does anybody know how to add a specific number of worksheets to an Excel
spreadsheet through VB.net? I'm trying to export some datatables to an excel file, but I only want as many sheets in the workbook as there are tables.

Right now the routine I'm tweaking from

http://support.microsoft.com/default...b;EN-US;306022

adds the default, (3).

At this stage I can export data fine, I just don't know how to control the
number sheets.

--
Ken

Nov 21 '05 #6
On Thu, 26 Aug 2004 11:05:16 -0500, "KC" <yo*@dontneed.this> wrote:

¤ This is directly from a datatable. Nothing fancy really.
¤

It might help to know what code you are using to add the Worksheets.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #7
i 've pasted your code but it doesn't define the 'library' microsoft.office
......(actually i don't have this problem only in your code)
also i don't have the excel option in my .NET framework componets list.
any idea??
thank you in advance

"Ken Tucker [MVP]" wrote:
Hi,

Dim oExcel As Microsoft.Office.Interop.Excel.Application

Dim oBook, oBook1 As Microsoft.Office.Interop.Excel.Workbook

Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet

'Start a new workbook in Excel.

oExcel = New Microsoft.Office.Interop.Excel.Application

oBook = oExcel.Workbooks.Add

oBook1 = oExcel.Workbooks.Add

'Add data to cells of the first worksheet in the new workbook.

oSheet = CType(oBook.Worksheets(1),
Microsoft.Office.Interop.Excel.Worksheet)

oSheet.Range("A1").Value = "Last Name"

oSheet.Range("B1").Value = "First Name"

oSheet.Range("C1").Value = "Price"

oSheet.Range("A1:B1").Font.Bold = True

oSheet.Range("A2").Value = "Doe"

oSheet.Range("B2").Value = "John"

oSheet.Range("C2").Value = 12345.456

oSheet.Range("C2").Cells.NumberFormat = "$0.00"

oSheet = CType(oBook.Worksheets(2),
Microsoft.Office.Interop.Excel.Worksheet)

oSheet.Range("A1").Value = "Last Name"

oSheet.Range("B1").Value = "First Name"

oSheet.Range("C1").Value = "Price"

oSheet.Range("A1:B1").Font.Bold = True

oSheet.Range("A2").Value = "Doe"

oSheet.Range("B2").Value = "John"

oSheet.Range("C2").Value = 12345.456

oSheet.Range("C2").Cells.NumberFormat = "$0.00"

'Save the Workbook and quit Excel.

oExcel.DisplayAlerts = False

oBook.SaveAs("c:\Book1.xls")

oSheet = Nothing

oBook = Nothing

oExcel.Quit()

oExcel = Nothing

GC.Collect()

Ken

-------------------------------------

"KC" <yo*@dontneed.this> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
Does anybody know how to add a specific number of worksheets to an Excel
spreadsheet through VB.net? I'm trying to export some datatables to an excel
file, but I only want as many sheets in the workbook as there are tables.

Right now the routine I'm tweaking from

http://support.microsoft.com/default...b;EN-US;306022

adds the default, (3).

At this stage I can export data fine, I just don't know how to control the
number sheets.

--
Ken

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Martin | last post by:
I have a situation where I'm displaying some information in a table on a web page. I've given the user the ability to make several different "queries" and show different sub-sets of the data. I...
1
by: tkaleb | last post by:
I have to create output file in a text, MS Access, MS Excel and .dbf format from C# Win/ADO.NET application. Data are collected in DataSet and there is no problem to make text file. However, I have...
27
by: jeniffer | last post by:
I need to create an excel file through a C program and then to populate it.How can it be done?
2
by: RICHARD BROMBERG | last post by:
I wrote a small Access application that accepts a City Name and a Street Name and runs a Query based on them . I want to create an Excel Spread sheet that contains all the matches found by the...
1
by: Venkat | last post by:
Can we create an Excel Addin with VC++.net? We are developing an application in c# which interacts with Excel application. The UI will be Excel and based on the inputs given through excel I...
2
by: krissh | last post by:
I know How Excel sheet in php .But wat i want is how to create Multiple sheets in Excel . This is the code for creating one excel sheet <?php header('Content-type:application/ms-xls'); ...
0
by: kennedystephen | last post by:
For the life of me, I cannot get this ... I have 1 excel document. I want to open that document and copy the first 50 rows to a new document. Then get the next 50 rows and copy those to a brand...
8
by: yogarajan | last post by:
hi All how can i create new excel sheet through asp.net (with c#) i have create report in aspx (with c#) and i store data in excel sheet also so user can select view report through web page or...
0
by: th12345 | last post by:
Hi All, I want to create custom excel sheet page in my web application like how Google spreadsheet was created. I do not want load data into Microsoft excel sheet. Please give some idea...
4
by: =?Utf-8?B?Sm9zaW4gSm9obg==?= | last post by:
I could create MS Excel sheet using ASP.NET 2.0 with C# but it is not being created in some systems, following error occurs when the program compiles : Microsoft Office Excel cannot open or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.