Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Excel 2007 connection via oledbconnection

I'm trying to open a connection to an excel 2007 file ( xlsx ) with
OleDbConnection.
The excel file in question contains a couple of worksheets, some of them
have graphs/charts on it and no data.

This is the connection string I use:
string con = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = " + fileName
+ ";Extended Properties =\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";

OleDbConnection DBCon = new OleDbConnection(con);
DBCon.Open(); // failed

It failed with an "External table is not in the expected format" exception.
Two points I found:
1. It works fine if I have the excel file open
2. It works fine if I remove all the worksheets with the graphs/charts in it

My question is if there is any option to include in the connection string to
ignore the graph/chart in the excel file? Just so that the connection to the
file can be opened..

any help will be much appreciated..
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Excel 2007 connection via oledbconnection

It depends on what you're trying to do. If you're querying data from the
workbook, you can specify the worksheet in your query string.

An example I've used:

Sub QueryExcel()

'create the connection string
Dim ConnectionString As String

ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" & _
"Data Source=K:\ADO\DataSheet.xls;" & _
"Extended Properties=Excel 8.0;"

'create the sql query
Dim MyQuery As String

'select the Today sheet
MyQuery = "SELECT * " & _
"FROM [Today$] "

'create the recordset
Dim MyRS As ADODB.Recordset
Set MyRS = New ADODB.Recordset

'open the recordset
MyRS.Open MyQuery, ConnectionString,adOpenStatic,adLockReadOnly, _
adCmdText

Sheets("xl data").Activate
ActiveSheet.Range("A1").CopyFromRecordset MyRS

MyRS.Close
Set MyRS = Nothing

End Sub

I'm not sure if you're trying to do the same type of thing.....
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Excel 2007 connection via oledbconnection

I was trying to get the worksheet names out of the workbook using
OleDBConnection. So unfortunately won't be able to specify the worksheet
name beforehand :(
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 data connection Kurt Barr Excel Discussion (Misc queries) 1 March 9th 10 08:32 PM
Data Connection between Excel 2007 and SharePoint 2007 kathryn.dunn Excel Discussion (Misc queries) 0 January 20th 10 10:27 PM
Data Connection Security in Excel 2007 Jason[_5_] Setting up and Configuration of Excel 5 August 29th 07 04:04 AM
Problem opening Excel 2007 file from OleDbConnection Dave Jenkins Excel Programming 1 October 24th 06 11:01 PM
Excel 2007 - The connection for this PivotTable has been deleted Michael Excel Discussion (Misc queries) 1 June 22nd 06 08:54 PM


All times are GMT +1. The time now is 10:26 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"