473,609 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11579
Hi,

Dim oExcel As Microsoft.Offic e.Interop.Excel .Application

Dim oBook, oBook1 As Microsoft.Offic e.Interop.Excel .Workbook

Dim oSheet As Microsoft.Offic e.Interop.Excel .Worksheet

'Start a new workbook in Excel.

oExcel = New Microsoft.Offic e.Interop.Excel .Application

oBook = oExcel.Workbook s.Add

oBook1 = oExcel.Workbook s.Add

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

oSheet = CType(oBook.Wor ksheets(1),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

oSheet = CType(oBook.Wor ksheets(2),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

'Save the Workbook and quit Excel.

oExcel.DisplayA lerts = False

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

oSheet = Nothing

oBook = Nothing

oExcel.Quit()

oExcel = Nothing

GC.Collect()

Ken

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

"KC" <yo*@dontneed.t his> wrote in message
news:e2******** ******@TK2MSFTN GP11.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*@dont need.this]
Sent: Wednesday, August 25, 2004 4:55 PM
To: microsoft.publi c.dotnet.langua ges.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.t his> 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******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #4
KC
This is directly from a datatable. Nothing fancy really.

Ken
"Paul Clement" <Us************ ***********@sws pectrum.com> wrote in message
news:st******** *************** *********@4ax.c om...
On Wed, 25 Aug 2004 16:55:22 -0500, "KC" <yo*@dontneed.t his> 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******@amerit ech.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***@bellsout h.net> wrote in message
news:ex******** ******@TK2MSFTN GP15.phx.gbl...
Hi,

Dim oExcel As Microsoft.Offic e.Interop.Excel .Application

Dim oBook, oBook1 As Microsoft.Offic e.Interop.Excel .Workbook

Dim oSheet As Microsoft.Offic e.Interop.Excel .Worksheet

'Start a new workbook in Excel.

oExcel = New Microsoft.Offic e.Interop.Excel .Application

oBook = oExcel.Workbook s.Add

oBook1 = oExcel.Workbook s.Add

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

oSheet = CType(oBook.Wor ksheets(1),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

oSheet = CType(oBook.Wor ksheets(2),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

'Save the Workbook and quit Excel.

oExcel.DisplayA lerts = False

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

oSheet = Nothing

oBook = Nothing

oExcel.Quit()

oExcel = Nothing

GC.Collect()

Ken

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

"KC" <yo*@dontneed.t his> wrote in message
news:e2******** ******@TK2MSFTN GP11.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.t his> 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******@amerit ech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #7
i 've pasted your code but it doesn't define the 'library' microsoft.offic e
......(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.Offic e.Interop.Excel .Application

Dim oBook, oBook1 As Microsoft.Offic e.Interop.Excel .Workbook

Dim oSheet As Microsoft.Offic e.Interop.Excel .Worksheet

'Start a new workbook in Excel.

oExcel = New Microsoft.Offic e.Interop.Excel .Application

oBook = oExcel.Workbook s.Add

oBook1 = oExcel.Workbook s.Add

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

oSheet = CType(oBook.Wor ksheets(1),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

oSheet = CType(oBook.Wor ksheets(2),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

'Save the Workbook and quit Excel.

oExcel.DisplayA lerts = False

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

oSheet = Nothing

oBook = Nothing

oExcel.Quit()

oExcel = Nothing

GC.Collect()

Ken

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

"KC" <yo*@dontneed.t his> wrote in message
news:e2******** ******@TK2MSFTN GP11.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
18792
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 would like to add a button to the page that would allow the user to create an .XLS file that would contain the current contents of the table. I realize that I could create it on the server and allow him to download it but I'd rather let him create...
1
11634
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 to create a new output files (tables with defined fields) in other 3 formats, and to fill them with data from DataSet. I created Excel output with ComponentOne's trial Excel component, but it is a temporary solution. Also I have unusual CP (1250...
27
27360
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
2228
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 Query. . Does anyone know how to do this . On a quick and dirty basis I was able to "cut and paste" the query results into an Excel Spread sheet. but thats a little too crude.
1
1685
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 need to do calculations using managed code (C#). Most of the time is consumed by interacting with excel like displaying values in the workbook or inserting cells based on my requirement. And adding some excel names into the work book etc...
2
11186
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'); header("Content-Disposition: attachment; filename=Emp_Skillgap.xls"); header("Pragma: no-cache"); header("Expires: 0");
0
1855
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 new document. I want to do this until the end of the Excel file. So... Open Excel1.
8
1790
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 excel sheet this is my query any one know abt create excel
0
1252
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 on this, how do start my work . Either create client side table using javascript or Server side table using Gridview or Server Table. If anyone have same experience on this, Please suggest me how do approach.
4
15415
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 save any more documents because there is not enough available memory or disk space. • To make more memory available, close workbooks or programs you no longer need.
0
8130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8573
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8406
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5510
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4021
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.