473,397 Members | 2,056 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,397 software developers and data experts.

How to create and access Excel in VB.NET?

Dear all,

I have an old VB6 application which can create and access Excel object. The
basic definition statements are as follows:

Dim appExcel As Object
Dim wkb1 As Excel.Workbook
Dim wks1 As Excel.Worksheet

Set appExcel = New [_ExcelApplication]
Set wkb1 = appExcel.Workbooks.Open(strFileName)
Set wks1 = wkbSalesSumy.Worksheets("One")

However, in VB.NET 2003, I got problem to do the same thing:

Dim appExcel As Excel.Application
Dim wkb1 As Excel.Workbook
Dim wks1 As Excel.Worksheet

appExcel = New Excel.Application ' since Excel.Application is
interface, this statement will fail due to "New" is not applicable to
interface

Besides, how can I create new worksheet using VB code?

Thanks for your attention and kindly help!

Regards,
James Wong
Nov 21 '05 #1
3 10688
Hi,

I added a reference to the excel interop assembly. Here is an
example on creating as worksheet. Try to avoid using latebinding.

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
---------------------

"James Wong" <cp*****@commercialpress.com.hk.NO_SPAM> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Dear all,

I have an old VB6 application which can create and access Excel object. The
basic definition statements are as follows:

Dim appExcel As Object
Dim wkb1 As Excel.Workbook
Dim wks1 As Excel.Worksheet

Set appExcel = New [_ExcelApplication]
Set wkb1 = appExcel.Workbooks.Open(strFileName)
Set wks1 = wkbSalesSumy.Worksheets("One")

However, in VB.NET 2003, I got problem to do the same thing:

Dim appExcel As Excel.Application
Dim wkb1 As Excel.Workbook
Dim wks1 As Excel.Worksheet

appExcel = New Excel.Application ' since Excel.Application is
interface, this statement will fail due to "New" is not applicable to
interface

Besides, how can I create new worksheet using VB code?

Thanks for your attention and kindly help!

Regards,
James Wong

Nov 21 '05 #2
"James Wong" <cp*****@commercialpress.com.hk.NO_SPAM> schrieb:
[Working with Excel]


General:

INFO: Develop Microsoft Office Solutions with Visual Studio .NET
<URL:http://support.microsoft.com/?kbid=311452>

HOWTO: Automate Microsoft Excel from Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;301982>

How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With
Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;316934>

Samples:

How to handle events for Excel 2003 by using Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;822750>

HOW TO: Handle Events for Excel by Using Visual Basic .NET
<URL:http://support.microsoft.com/default.aspx?scid=kb;en-us;302814>

HOWTO: Automate Microsoft Excel from Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;301982>

HOW TO: Transfer Data to an Excel Workbook by Using Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;306022>

HOW TO: Handle Events for Excel by Using Visual C# .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;823981>

Related:

BUG: "Old Format or Invalid Type Library" Error When Automating Excel 2002
<URL:http://support.microsoft.com/?scid=kb;EN-US;320369>

PRB: Office Application Does Not Quit After Automation from Visual Studio
..NET Client
<URL:http://support.microsoft.com/?scid=kb;EN-US;317109>

HOWTO: Use the WebBrowser Control to Open an Office Document in Visual Basic
..NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;304643>

INFO: Considerations for Server-Side Automation of Office
<URL:http://support.microsoft.com/?scid=kb;EN-US;257757>

More information:

<URL:http://support.microsoft.com/search/?query=Excel+.NET&catalog=LCID%3D1033>

There are loads of information about how to use Office applications together
with .NET in the Microsoft Office Developer Center:

Microsoft Office Developer Center
<URL:http://msdn.microsoft.com/office/>

Office Development
<URL:http://msdn.microsoft.com/office/understanding/>

Using the Microsoft Visual Studio Tools for the Microsoft Office System
<URL:http://www.devx.com/codemag/Article/18233/>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3
Hi Herfried and Ken,

Thanks for your information and it works fine!

One more question, there is some information composed by numeric digits but
I want to show it as text (e.g. tel. no. or barcode). In Excel, I will set
the cell format as text. How can I do that using VB.NET with Excel class?

Thanks again your help!

Regards,
James Wong

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> ¦b¶l¥ó
news:OY**************@TK2MSFTNGP09.phx.gbl ¤¤¼¶¼g...
"James Wong" <cp*****@commercialpress.com.hk.NO_SPAM> schrieb:
[Working with Excel]
General:

INFO: Develop Microsoft Office Solutions with Visual Studio .NET
<URL:http://support.microsoft.com/?kbid=311452>

HOWTO: Automate Microsoft Excel from Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;301982>

How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook

With Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;316934>

Samples:

How to handle events for Excel 2003 by using Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;822750>

HOW TO: Handle Events for Excel by Using Visual Basic .NET
<URL:http://support.microsoft.com/default.aspx?scid=kb;en-us;302814>

HOWTO: Automate Microsoft Excel from Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;301982>

HOW TO: Transfer Data to an Excel Workbook by Using Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;306022>

HOW TO: Handle Events for Excel by Using Visual C# .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;823981>

Related:

BUG: "Old Format or Invalid Type Library" Error When Automating Excel 2002
<URL:http://support.microsoft.com/?scid=kb;EN-US;320369>

PRB: Office Application Does Not Quit After Automation from Visual Studio
.NET Client
<URL:http://support.microsoft.com/?scid=kb;EN-US;317109>

HOWTO: Use the WebBrowser Control to Open an Office Document in Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;304643>

INFO: Considerations for Server-Side Automation of Office
<URL:http://support.microsoft.com/?scid=kb;EN-US;257757>

More information:

<URL:http://support.microsoft.com/search/...alog=LCID%3D10
33>
There are loads of information about how to use Office applications together with .NET in the Microsoft Office Developer Center:

Microsoft Office Developer Center
<URL:http://msdn.microsoft.com/office/>

Office Development
<URL:http://msdn.microsoft.com/office/understanding/>

Using the Microsoft Visual Studio Tools for the Microsoft Office System
<URL:http://www.devx.com/codemag/Article/18233/>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

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...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.