Jump to content
Xtreme .Net Talk

Recommended Posts

Posted


       Dim xlApp As Excel.Application
       Dim xlBook As Excel.Workbook
       Dim xlSheet As Excel.Worksheet
       Dim myString as string
       
       'initialize the xlapplication
       xlApp = CType(CreateObject("Excel.Application"), Excel.Application)

       xlBook = xlApp.Workbooks.Open(strPath & strFileName)

       xlSheet = xlBook.Worksheets(1)
       myString = xlSheet.Cells(1, 1)  '(Row, Column)

       xlSheet = nothing
       xlBook.Close()
       xlBook = nothing
       xlApp.Quit()
       xlApp = Nothing

 

This should get you started.

 

You want to make very sure that the Xl objects get closed down completly or bad things will happen. A lot times if you stop execution with out closing the object down you will have to go into the Task Manager -> Processes, sort by process name to see if EXCEL.exe is running then end the process. I had 20 open at one time and the computer was doing all kinds of funky stuff till I shut down all the instances of the app, which will not show under applications on the task manager.

 

In order to get an Idea of the commands, record macros then look at the VBA code (in Excel) to reverse engineer the Excel Object Model. The commands will not be exactly the same for .net but close enough that you can experiment and figure it out.

 

MTS

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

Posted

You can also read data from excel in a very similar fashion to the way you read from a database. The only requirements are that

  • your spreadsheet has named range(s)
  • you use the jet provider

the connection string includes the following line

"Extended Properties=Excel 8.0;"

 

then you can use an sql statement like

"select * from yournamedrange" and fill a dataset or whatever.

 

Also you can use column names e.g if you use row 1 of the spreadsheet to name the columns you can use those names in your sql statement.

You can not get ye flask.
  • 3 weeks later...
Posted

thanks

 

I Tried the

 

Dim aExcel as Excel.Application

 

aExcel = cType(CreateObject("Excel.Application"),Excel.Application)

 

 

but i got the error

 

"Cannot Create ActiveX component"

 

also tried

 

Dim aExcel as new Excel.application

 

and got the error

 

"Access Denied"

 

Both of these worked on my development machine but wouldn't work on the server.

 

I even went to the extreme of installing Excel on the server and still got the same errors

 

can anyone help me out?

Posted

Is there any way you can check the server to see if someone has the document open on anouther computer. That is the error I get when that condition exsists.

 

MTS

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...