954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

opening an Excel file in VB.NET

The title of this thread pretty much says it all - I would like to know how to open an already created excel spreadsheet (I want the application to launch).
Thanks so much to anyone who has time to respond to this.

daskeyboard
Newbie Poster
2 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
The title of this thread pretty much says it all - I would like to know how to open an already created excel spreadsheet (I want the application to launch). Thanks so much to anyone who has time to respond to this.


check out this article by Microsoft , http://support.microsoft.com/default.aspx?scid=kb%3B [LN]%3BQ316934

Copy and paste the URL in your browser

Letscode
Junior Poster
175 posts since Feb 2005
Reputation Points: 11
Solved Threads: 6
 

Anyone have another suggestion - the microsoft example is slightly prolix.

daskeyboard
Newbie Poster
2 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
Anyone have another suggestion - the microsoft example is slightly prolix.


:)
Dim xlsApp As Excel.Application
Dim xlsWB As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim xlsCell As Excel.Range
Dim xlsDatei As String

xlsApp = New Excel.Application
xlsApp.Visible = True
xlsWB = xlsApp.Workbooks.Open(Path)
xlsSheet = xlsWB.Worksheets(1)
xlsCell = xlsSheet.Range("A1")

Greetings from Wolfskind

Wolfskind
Newbie Poster
1 post since Aug 2005
Reputation Points: 10
Solved Threads: 1
 

Hi there

I used the code as given but I get an error as follows:

System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2147417851
Message="The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
Source="Interop.Excel"
StackTrace:
at Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)

So far I have wasted an afternoon trying to do this basic thing and I would be wrapped if someone could give me a little shove in the back :-)

Thanks very much!
Rino

RinoStoof
Newbie Poster
4 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Good Info :)

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

thanks for info..

Estella
Junior Poster in Training
99 posts since Jan 2008
Reputation Points: 64
Solved Threads: 7
 
The title of this thread pretty much says it all - I would like to know how to open an already created excel spreadsheet (I want the application to launch). Thanks so much to anyone who has time to respond to this.

you can read worksheet like this

xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("c:\vbexcel.xlsx")
xlWorkSheet = xlWorkBook.Worksheets("sheet1")

range = xlWorkSheet.UsedRange

For rCnt = 1 To range.Rows.Count
For cCnt = 1 To range.Columns.Count
Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
MsgBox(Obj.value)
Next
Next http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm

http://vb.net-informations.com/excel-2007/vb.net_excel_read_entire%20_worksheet.htm

ian_taylor
Newbie Poster
1 post since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Thank you, ian_taylor

Your url really helps me

I need tutorial about crystal report and i find them in the related url in your link

Thank!!! Kids

Kids
Newbie Poster
7 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

:) Dim xlsApp As Excel.Application Dim xlsWB As Excel.Workbook Dim xlsSheet As Excel.Worksheet Dim xlsCell As Excel.Range Dim xlsDatei As String

xlsApp = New Excel.Application xlsApp.Visible = True xlsWB = xlsApp.Workbooks.Open(Path) xlsSheet = xlsWB.Worksheets(1) xlsCell = xlsSheet.Range("A1")

Greetings from Wolfskind

I have use this code. But I resive runtime errow "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))". If anyone know how to dilling with this error please answer

ollia
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

if this " Microsoft Excel 12.0 Object Library " is not displaying under PROJECTS --> ADD REFERENCES how to create that object liberary appear there

shanakaprabath
Newbie Poster
14 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Here's what I had to do to get the code samples working.

First (as already mentioned), add the Excel object library as a reference. (You must have Excel installed). Something like this:
Project->Add Reference->COM->Microsoft Excel 12.0 Object Library->OK

Then add these two lines to the top of your program:

Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core


So here's my complete sample program (new VB.NET project, added one button):

' Make sure to first go to:
' Project->Add Reference->COM->Microsoft Excel 12.0 Object Library->OK

' Now import classes from reference
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Local variables
        Dim xlsApp As Excel.Application
        Dim xlsWorkBook As Excel.Workbook
        Dim xlsWorkSheet As Excel.Worksheet
        Dim xlsCell As Excel.Range

        ' Initialise Excel Object
        xlsApp = New Excel.Application

        ' Open test Excel spreadsheet
        xlsWorkBook = xlsApp.Workbooks.Open("c:\test.xls")
        ' Open worksheet (can open by number or name)
        xlsWorkSheet = xlsWorkBook.Worksheets(1) ' You could also do it by name: "sheet1"
        ' Read the first cell
        xlsCell = xlsWorkSheet.Range("A1")
        ' Display the first cell
        MsgBox(xlsCell.Text)
    End Sub
End Class
glaverty
Newbie Poster
1 post since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Here's what I had to do to get the code samples working.

First (as already mentioned), add the Excel object library as a reference. (You must have Excel installed). Something like this: Project->Add Reference->COM->Microsoft Excel 12.0 Object Library->OK

Then add these two lines to the top of your program:

Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core

So here's my complete sample program (new VB.NET project, added one button):

' Make sure to first go to:
' Project->Add Reference->COM->Microsoft Excel 12.0 Object Library->OK

' Now import classes from reference
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Local variables
        Dim xlsApp As Excel.Application
        Dim xlsWorkBook As Excel.Workbook
        Dim xlsWorkSheet As Excel.Worksheet
        Dim xlsCell As Excel.Range

        ' Initialise Excel Object
        xlsApp = New Excel.Application

        ' Open test Excel spreadsheet
        xlsWorkBook = xlsApp.Workbooks.Open("c:\test.xls")
        ' Open worksheet (can open by number or name)
        xlsWorkSheet = xlsWorkBook.Worksheets(1) ' You could also do it by name: "sheet1"
        ' Read the first cell
        xlsCell = xlsWorkSheet.Range("A1")
        ' Display the first cell
        MsgBox(xlsCell.Text)
    End Sub
End Class


Thank you soo much glaverty this helps me very much i'll contact u if i enroll with any problem during i'm using this ... THanks again

shanakaprabath
Newbie Poster
14 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
Dim oxcel As Excel.Application
Dim owb As Excel.Workbook
Dim osht As Excel.Worksheet
Dim ocell As Excel.Range

oxcel= New Excel.Application
oxcel.Visible = True
owb= oxcel.Workbooks.Open(Path)
osht= owb.Worksheets(1)
ocell = osht.Range("A1")
raymyster
Light Poster
27 posts since Jan 2008
Reputation Points: -3
Solved Threads: 0
 

If you are just trying to export rows of a Gridview into Excel, then there is a tutorial on how to do it here.

brianmaddox
Newbie Poster
9 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

do you have some code that use Microsoft Excel 11.0 Object Library
as a reference to open excel file to vb.net. . i don't have
reference like this one
Microsoft Excel 12.0 Object Library

zakie
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This is a great help, but what if I wanted to open a spreadsheet using the excel program? How would I pass along that information to the Excel program? Just a newbie needing help. Thanks in advance. :)

wes3066
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Try this code its 100% working

Add reference "Microsoft Excel 10.0 object library" to your project

then paste the code to your open Excel file button Click event

Private Sub OPEN_EXCEL_FILE_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OPEN_EXCEL_FILE_Button.Click


     
            Dim xlsApp As Microsoft.Office.Interop.Excel.ApplicationClass
            Dim xlsWB As Microsoft.Office.Interop.Excel.WorkbookClass

            xlsApp = New Microsoft.Office.Interop.Excel.ApplicationClass
            xlsApp.Visible = True
            xlsWB = xlsApp.Workbooks.Open("D:\vbexcel.xlsx")'ADD PATH OF YOUR EXCEL FILE IN PLACE OF D:\vbexcel.xlsx
        Catch ex As Exception
            MessageBox.Show("excel file not created/found")
       
End Sub
ADHAVRAJESH
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

I'm going to close this thread to avoid further confusion. Please create a new thread to ask your question.

Thread Locked.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You