Avatar of Jinx70
Jinx70
 asked on

Create an EXCEL File Programatically Using VB.NET

I want to create a VB.NET application that creates an Excel File. Currently I have installed Office 2007.
Most examples I found on the web uses office 2003 objects library. Some, like the link below, uses Excel 9.0 Object Library.

http://www.vbdotnetheaven.com/UploadFile/ggaganesh/ExcelSpreadsheet04182005093012AM/ExcelSpreadsheet.aspx?ArticleID=14bf0d5f-a633-42da-ab3a-0a95dc974554

I have read about the Primary Interop Assemblies and have downloaded for Office version 2003.

I have tried the code provided on the link above but it doesnt seem to run on my machine.

All I wanted to do was to create an excel file where I can define any name I want even if there was no Office installed in the machine where this supposedly program will be installed.

I need to know the specific steps that I need to take and of course a sample code on how to do it.

Thanks.
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
athapa

8/22/2022 - Mon
theplonk

Check out:
http://msdn2.microsoft.com/en-us/library/aa432112.aspx

You will need to reference the office 2007 object library.

The example shown in the website is:
Application.NewDocument.Add FileName:="C:\NewDocument.doc", _
        Section:=msoNew, DisplayName:="New Document"
    CommandBars("Task Pane").Visible = True

It has references for creating an excel file. Change the Application.NewDocument to Application.NewWorkbook.
Jinx70

ASKER
How do you implement your code?
ASKER CERTIFIED SOLUTION
rockiroads

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
theplonk

Rockiroads is correct. I should have read my reference more carefully :)
Mine reference is to add new items to the File New page for an Office Application.

You will just need to reference the Microsoft Excel Object Library.
And do something similar to Rockiroads answer or as below.

Dim OfficeApp As New Microsoft.Office.Interop.Excel.Application
OfficeApp.Workbooks.Add()
OfficeApp.ActiveWorkbook.SaveAs("PathAndFileName")
OfficeApp.ActiveWorkbook.Close()
OfficeApp.Quit()

Within the SaveAs method there are numerous options.
http://msdn2.microsoft.com/fr-fr/library/microsoft.office.interop.excel._workbook.saveas(vs.80).aspx
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
athapa

I've used Excel when I'm sure that the deployed machines will have Excel installed. However, when you are using Excel you need to be very careful and compeltely release all com objects. Otherwise you'll start to leak memory in your program.
I prefer to use Aspose.Cell. It is faster than using MS Excel com objects and you dont' need to worry about com objects may loose reference but not released.
You might also find some dotnet code to write biff or compound ole documents.