Tuesday, 28 August 2012
VB.NET - How to creae Excel file - Simple example
1. Create new project
2. Add reference
3. Select Microsoft.Office.Interop.Excel - ensure Excel is installed.
4. Copy the following code - this will create simple Excel file in your C:\Temp\ExcelTest directory
Dim fileTest As String = "C:\Temp\ExcelTest\test.xlsx"
If File.Exists(fileTest) Then
File.Delete(fileTest)
End If
Dim oExcel As Object
oExcel = CreateObject("Excel.Application")
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
oBook = oExcel.Workbooks.Add
oSheet = oExcel.Worksheets(1)
oSheet.Name = "Test Name"
oSheet.Range("A1").Value = "SOME VALUE"
oBook.SaveAs(fileTest)
oBook.Close()
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
5. More info on this video:
Subscribe to:
Post Comments (Atom)
Hi Publisher,
ReplyDeleteI follow your way, but I got "Cannot create ActiveX component." Error. I got this error, when the code is trying to create:
Dim oExcel As Object
oExcel = CreateObject("Excel.Application")
Can you help me to solve this?
thx.
This comment has been removed by a blog administrator.
DeleteDo you have excel installed on your computer?
ReplyDeleteThis comment has been removed by a blog administrator.
DeleteThis comment has been removed by a blog administrator.
ReplyDeleteFor 2017 VB users:
ReplyDeleteProject>Add Reference> COM > Type Libraries