Link to home
Start Free TrialLog in
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.
Avatar of theplonk
theplonk
Flag of Australia image

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.
Avatar of Jinx70
Jinx70

ASKER

How do you implement your code?
ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.