trying to open get values from workbook via vb.net

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
hey all

im trying to open (by filling a dataset if i can) a workbook/worksheet and grab values from a worksheet and add them together. seems simple enough to me but for some reason i just cant get it to work. ive tried a couple of methods but none are working.. the main error is cant find "Excel.Application" etc etc. i realise i need a certain library but i have no idea where to get it! please help this is really frustrating.
thanks in advance
adam
 
First, you'll want to be sure to add references for both Excel (v. 9 for 2000, v. 10 for XP) and Office (same).
Solution Explorer => highlight 'References' => right click and choose add references. Choose from the COM tab to find Microsoft Excel 10.0 and Microsoft Office 10.0.

at the top of the form add:
Imports Excel


I don't have specific code for you but i can give you a sample for writing to excel:
VB.NET:
' Declare Excel object variables and create types
        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)

        ' Insert data
        xlSheet.Cells(1, 2) = 5000
        xlSheet.Cells(2, 2) = 75
        xlSheet.Cells(3, 1) = "Total"
        ' Insert a Sum formula in cell B3
        xlSheet.Range("B3").Formula = "=Sum(R1C2:R2C2)"
        ' Format cell B3 with bold
        xlSheet.Range("B3").Font.Bold = True
        ' Display the sheet
        xlSheet.Application.Visible = True
        ' Save the sheet to c:\vbnet03sbs\chap13 folder
        xlSheet.SaveAs("C:\myexcelsheet.xls")
        ' Leave Excel running and sheet open
See if that gets you farther....


P.S. I have some projects that use excel spreadsheets as inputs to datasets, let me know if you need more code or help.
Also search the forum, I have posted code and projects for Excel before.
 
Last edited:
Back
Top