Importing Excel files into Visual Basic 2005 Express
Hi, I'm a complete database newbie so please bear with me!
I'm
using Visual Basic 2005 Express to create an application which will
just list an editable database of stuff, but I don't want to have to
enter all the info manually.
I have it all on an XLS / CSV spreadsheet: 10 columns and 9000 rows, I just want to import it. But
how?
If your reading the data only you use ADO.NET to read the data as one approach, another may be to export the data to a CSV File and then use something like the TextFieldParser object to process the CSV file or you can access the Excel sheet directly
ADO.NET to Retrieve and Modify Records in an Excel Workbook
http://support.microsoft.com/kb/316934
READING A COMMA SEPARATED FILE USING TEXTFILEPARSER
http://msdn2.microsoft.com/en-us/library/cakac7e6(VS.80).aspx
EXCEL SHEET DIRECTLY
An interesting article which will show you how to get VB.Net to interact with Excel.
How to automate Excel from VB.Net
http://support.microsoft.com/kb/q301982/
Here's an example which will add the contents of an array into an excel spreadsheet. You'll need to ensure that you set the reference to the Excel Object Class Library. The array contents - whether integer or string shouldnt really matter when your setting the value property.
Writing from a control such as a textbox is also included.
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
'On Error GoTo Err_Handler
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True
' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
' Add cells by looping.
Dim iRow As Integer = 1
For iRow = 1 To 10
oSheet.Cells(iRow, 1).Value = "Test" & iRow
Next iRow
'//Create an Array and then add these entries to an excel spreadsheet
Dim a(10) As String
For iRow = 1 To 10
a(iRow) = "Array Entry" & iRow.ToString
Next
'//Add to sheet
For iRow = 1 To 10
oSheet.Cells(iRow, 2).Value = a(iRow)
Next iRow
'//Add from a textbox Control
oSheet.Cells(1, 2).Value = Textbox1.Text
' Make sure Excel is visible and give the user control
' of Microsoft Excel's lifetime.
oXL.Visible = True
oXL.UserControl = True
' Make sure you release object references.
oRng = Nothing
oSheet = Nothing
oWB = Nothing
oXL = Nothing
Catch ex As Exception
MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
End Try
End Sub
End Class
Hello Spotty,
I'm trying your code above in a VB project using Visual Studio 2005. The first line "Imports ..." does not work since it has no members. I am able to create the object since I have the client reference to excel object library, and the dim lines below work. However I don't have methods to "Open" or "Add" a workbook. Can you help? Thanks.
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
oXL = CreateObject("Excel.Application")
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet