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