Visual Basic Development Bookmark and Share   
 Home > Visual Basic Interop and Upgrade > VB program reading Excel spreadsheet and writing to SQL DB
 

VB program reading Excel spreadsheet and writing to SQL DB

VS 2008. I need to read an Excel Spreadsheet, search through the rows and columns, pick out data and write it to a SQL DB. I know how to do the SQL part, but do NOT know how to do the Excel part. I don't even know if this is the right forum. I have read a lot but most of it is either out of date (2003 or 2005) and most of it concerns writing add-ins for Excel. I will be running this on a machine(s) that do not even have office on them.

I've found lots of "detail" but cannot find the overview that explains in general terms how to proceed.

Carl

HopkinsITM  Tuesday, November 24, 2009 3:57 AM
There are a couple of methods that you can use. First, you can use data access w/ADO.NET:

How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET

Or, you can use automation (which requires that Excel be installed):

How to automate Microsoft Excel from Visual Basic .NET



Paul ~~~~ Microsoft MVP (Visual Basic)
Paul P Clement IV  Tuesday, November 24, 2009 2:49 PM
There are a couple of methods that you can use. First, you can use data access w/ADO.NET:

How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET

Or, you can use automation (which requires that Excel be installed):

How to automate Microsoft Excel from Visual Basic .NET



Paul ~~~~ Microsoft MVP (Visual Basic)
Paul P Clement IV  Tuesday, November 24, 2009 2:49 PM

Thank you Paul for your friendly help and support!

Hi Carl,

Besides, here are more tutorials and code samples for you to check.

Topic: How to read/writeExcel documentvia Excel Automation in VB.NET?

Firstly Add Reference to COM component “Microsoft Excel Object Library�into your project.

Code sample: Get/Set cell value in Excel Spreadsheet.

Imports Microsoft.Office.Interop.Excel

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim oExcel As Object = CreateObject("Excel.Application")

Dim oBook As Object = oExcel.Workbooks.Open("C:\Book1.xls")

Dim oSheet As Object = oBook.Worksheets(1)

'e.g. Read value in A2 cell

Dim cellValue As String = oSheet.Range("A2").Value

'e.g. Change value in A2 cell

oSheet.Range("A2").Value = ""

'Save this Excel document

oBook.SaveAs("C:\Book1.xls", True)

oExcel.Quit()

End Sub

End Class


Many code samples about: How to manipulate Excel Spreadsheet data in VB.NET.

1) Store each Array element to Excel in consecutive cells

http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/1a41f104-73ff-4de4-9002-687b5243ba94/

2) Sum those consecutive cells horizontally or vertically

http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/6cbe0a15-4e21-4bd1-aad9-ea364443d3f4/

3) Retrieve all WorkSheet names in Excel file and binding specific WorkSheet content to DataGridView

http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/ce4bbe9c-7d13-44c5-a26c-4b84912a51f3/

4) Retrieve a range of cells and bind to DataGridView

http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/91f1f3a7-05e6-4b75-934f-b49e6f0f4f54/



Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  Monday, November 30, 2009 8:05 AM

You can use google to search for other answers

Custom Search

More Threads

• differences in buttons from vb 6 to vb.net 2.0
• VB6 Active X DLL called within VB.NET Stability Analysis Question
• Unmanaged code conversion
• Best upgrade: vb6 dynamic parameter based object creation for custom classes to C# equivalent
• Components Error
• VB Runtime? What is it?
• Are Sharepoint classes com wrappable?
• Equivalent of ActiveX dll in VB.Net
• what we can use instead of "sortkey" in vb.net ?
• Microsoft.Office.Interop.Outlook.dll TYPE_E_LIBNOTREGISTERED error VB.NET 2005 app