How to generate Excel files in VB.Net?

kpao

Active member
Joined
Apr 3, 2006
Messages
29
Programming Experience
Beginner
I've found that there is a namespace called Excel in VB.Net. However, I can't figure out how to use. Can anybody post some examples about generating Excel files. Thanks~
 
No, there is not a Excel namespace in .Net.

If you have Excel installed you can use the automation engine provided by the Excel Object library to create Excel files.
 
JohnH's link is a real good one.

Here is sample code also:
Folow the steps in JohnH's link - Adding The Project Reference


Imports Excel ' at the top of the form/module


VB.NET:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        [COLOR=Green]' Declare Excel object variables and create types[/COLOR]
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

[COLOR=Green]        ' Insert data[/COLOR]
        xlSheet.Cells(1, 2) = 5000
        xlSheet.Cells(2, 2) = 75
        xlSheet.Cells(3, 1) = "Total"
[COLOR=Green]        ' Insert a Sum formula in cell B3[/COLOR]
        xlSheet.Range("B3").Formula = "=Sum(R1C2:R2C2)"
[COLOR=Green]        ' Format cell B3 with bold[/COLOR]
        xlSheet.Range("B3").Font.Bold = True
[COLOR=Green]        ' Display the sheet[/COLOR]
        xlSheet.Application.Visible = True
        [COLOR=Green]' Save the sheet to c:\[/COLOR]
        xlSheet.SaveAs("C:\myexcelsheet.xls")
       [COLOR=Green] ' Leave Excel running and sheet open
    [COLOR=Blue]End Sub[/COLOR][/COLOR]
 
DavidT_macktool said:
JohnH's link is a real good one.

Here is sample code also:
Folow the steps in JohnH's link - Adding The Project Reference


Imports Excel ' at the top of the form/module


VB.NET:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        [COLOR=green]' Declare Excel object variables and create types[/COLOR]
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
 
[COLOR=green]       ' Insert data[/COLOR]
        xlSheet.Cells(1, 2) = 5000
        xlSheet.Cells(2, 2) = 75
        xlSheet.Cells(3, 1) = "Total"
[COLOR=green]       ' Insert a Sum formula in cell B3[/COLOR]
        xlSheet.Range("B3").Formula = "=Sum(R1C2:R2C2)"
[COLOR=green]       ' Format cell B3 with bold[/COLOR]
        xlSheet.Range("B3").Font.Bold = True
[COLOR=green]       ' Display the sheet[/COLOR]
        xlSheet.Application.Visible = True
        [COLOR=green]' Save the sheet to c:\[/COLOR]
        xlSheet.SaveAs("C:\myexcelsheet.xls")
       [COLOR=green]' Leave Excel running and sheet open
    [COLOR=blue]End Sub[/COLOR][/COLOR]
UM... Error occured in this statement.
" xlApp = CType(CreateObject("Excel.Application"), Excel.Application)"
Can't create Active X..........
How can I solve this problem?
 
Back
Top