[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.odbcnet

excel - unknown worksheet name - no automation

noam

7/17/2002 7:01:00 PM

I am using odbc.net to read in data from several different external file
types, including Excel (2000).
When I try to load excel data I supply the name of the worksheet as the
table name, and fill the dataset based on that worksheet. (See the code
snippet below.) If I know that name of the worksheet - say "Sheet1" then I
affix a "$" to signify that it's a worksheet and everything works well.
However, if I don't know the name of the worksheet - and 99% of the time it
most definitely is NOT "Sheet1$" - I have a problem. I'm not using
automation, so I can't reference the worksheet by requesting Worksheets[0],
for example. I tried also using CurrentRegion and CurrentRange, but it
seems that they are properties accessible only through automation.

1. Assuming there is only 1 worksheet, with only 1 region (or range), does
anyone know how I can access that single worksheet using ODBC.NET (or ODBC
at all for that matter) without knowing the worksheet's user-defined name?
2. Does the excel driver support some pre-defined naming convention for the
worksheets based on index?
3. Is it possible, alternatively, to query for a list of worksheets in a
spreadsheet file?
4. Is my assumption that CurrentRegion and CurrentRange are properties
available onlt with automation correct, or can I use these somehow with
odbc?


The following code snippet works correctly:
//---------------------------------------------------------------------
dataDriver = "{Microsoft Excel Driver (*.xls)}";
dataDataBase = fi.FullName; // file name of the requested file
dataTableName = "Sheet1$"; // PROBLEM IS HERE

OdbcConnection conn = new OdbcConnection( getConnStr() );
string select = "SELECT * FROM [" + dataTableName + "]";
OdbcCommand cmd = new OdbcCommand( select, conn);

OdbcDataAdapter da = new OdbcDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
DataTable tbl = ds.Tables.Add();
da.Fill( tbl );
//---------------------------------------------------------------------

ANY help is greatly appreciated!
I have a very tight deadline, resting partly on the solution to this
problem.

Thanks in advance,
Noam
noamr@britannica-ks.com




2 Answers

(Douglas Laudenschlager [MS])

7/18/2002 1:08:00 AM

0


Noam,

1. You do need a table name to read the data using ADO or ADO.Net.
Remember, the data access library is simply writing a SQL SELECT statement
for you, and SELECT requires a table name.

2. No, it does not.

3. Yes:
Q318373 HOW TO: Use GetOleDbSchemaTable with Excel in Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...

4. Yes, those are properties in the Excel object model accessible only
through automation.

Best wishes,

-Doug


USING ADO AND ADO.NET WITH EXCEL: Resources and Known Issues
July 2002

General
-------
Q257819 HOWTO: Use ADO with Excel Data from Visual Basic or VBA
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q303814 HOWTO: Use ADOX with Excel Data from Visual Basic or VBA
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q278973 SAMPLE: ExcelADO Shows How to Read/Write Data in Excel Workbooks
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q195951 HOWTO: Query and Update Excel Data Using ADO From ASP
http://support.microsoft.com/default.aspx?scid=kb;EN-...

Transferring Data into Excel
----------------------------
Q247412 INFO: Methods for Transferring Data to Excel from Visual Basic
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q295646 HOWTO: Transfer Data from ADO Data Source to Excel with ADO
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q246335 HOWTO: Transfer Data from ADO Recordset to Excel with Automation
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q319951 HOW TO: Transfer Data to Excel by Using SQL Server DTS
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q306125 HOW TO: Import Data from SQL Server into Microsoft Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...

Known Issues (most recent listed first)
------------
Q319998 BUG: Memory Leak When You Query Open Excel Worksheet with ADO
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q316831 PRB: Cannot Configure Data Connection to Excel Files in VS .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q316809 BUG: No ADO Connection Error When Excel Workbook Is Not Found
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q316756 PRB: Error w/ ADO.NET OLEDbDataAdapter to Modify Excel Workbook
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q314763 FIX: ADO Inserts Data into Wrong Columns in Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q316475 PRB: "Operation Must Use an Updateable Query" Error Message
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q300948 BUG: Incorrect TABLE_TYPE Is Returned for Excel Worksheets
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q294410 ACC2002: Nulls Replaced w/ Next Field's Data Exporting to Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q293828 BUG: Excel File Size Grows When You Edit ADO Recordset
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q288343 BUG: Excel ODBC Driver Disregards FirstRowHasNames/HDR Setting
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q246167 PRB: Collating Sequence Error Opening XLS as ADO Recordset
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q211378 XL2000: 'Could Not Decrypt File' Error with Password-Protected File
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q194124 PRB: Excel Values Returned as NULL Using DAO OpenRecordset (mixed
data types)
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q189897 XL97: Data Truncated to 255 Characters with Excel ODBC Driver (Rows
To Scan)
http://support.microsoft.com/default.aspx?scid=kb;EN-...

VB/VB.Net (most recent listed first)
---------
Q319180 HOWTO: Transform Dataset to Spreadsheet XML for Microsoft Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q318373 HOW TO: Use GetOleDbSchemaTable with Excel in Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q316934 HOW TO: Use ADO.NET to Retrieve and Modify Records in Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q311731 HOW TO: Query and Display Excel Data by Using ADO.NET, VB .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q306022 HOW TO: Transfer Data to Excel Workbook Using Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q302094 HOWTO: Fill/Retrieve Excel Data Using Arrays From VB .Net
http://support.microsoft.com/default.aspx?scid=kb;EN-...

ASP/ASP.Net/Web (most recent listed first)
---------------
Q317719 HOW TO: Export Data in DataGrid on an ASP. NET WebForm to Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q308247 HOW TO: Use ASP.NET to Query and Display Database Data in Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q307603 HOW TO: Write Binary Files to the Browser Using ASP.NET & VB
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q306572 HOW TO: Query and Display Excel Data by Using ASP.NET, ADO.NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q276488 HOWTO: Use ADODB.Stream to Read Binary Files to the Browser
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q257757 INFO: Considerations for Server-Side Automation of Office
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q199841 HOWTO: Display ASP Results Using Excel in IE with MIME Types
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q195951 HOWTO: Query and Update Excel Data Using ADO From ASP
http://support.microsoft.com/default.aspx?scid=kb;EN-...

SQL Server/DTS (most recent listed first)
--------------
Q321686 HOW TO: Import Data into SQL Server from Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q319951 HOW TO: Transfer Data to Excel by Using SQL Server DTS
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q306397 HOWTO: Use Excel w/ SQL Linked Servers & Distributed Queries
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q306125 HOW TO: Import Data from SQL Server into Microsoft Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q281517 PRB: Transfer of Data from Jet 4.0 OLEDB Source Fails w/ Error
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q236605 PRB: DTS Wizard May not Detect Excel Column Type for Mixed Data
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q231880 BUG: Import Wizard Fails if Excel File Open During Import/Export
http://support.microsoft.com/default.aspx?scid=kb;EN-U...
Q207446 BUG: Cannot Import Excel 97 Spreadsheet with 256 or More Columns
http://support.microsoft.com/default.aspx?scid=kb;EN-...

XML (most recent listed first)
---
Q319180 HOWTO: Transform Dataset to Spreadsheet XML for Microsoft Excel
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q307021 HOW TO: Transfer XML Data to Microsoft Excel 2002 with VB .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q285891 HOWTO: Use VB or ASP to Create an XML Spreadsheet for Excel 2002
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q278976 HOWTO: Transform Excel XML Spreadsheet for Server-Side Use
http://support.microsoft.com/default.aspx?scid=kb;EN-...

ADO within Excel (most recent listed first)
----------------
Q291199 XL2002: "Invalid Use of New Keyword" Error Using ADODB Library
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q263498 BUG: Run-Time Error 5 Using Add Method of QueryTables Collection
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q244761 XL2000: How to Use ADO to Return Data to a ListBox or ComboBox
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q228633 OFF2000: "Catastrophic Failure" Error Running Samples.xls Macro
http://support.microsoft.com/default.aspx?scid=kb;EN-...
Q225059 XL2000: "Invalid Use of New Keyword" Error Using ADODB Library
http://support.microsoft.com/default.aspx?scid=kb;EN-...

----------------------------------------------------------------------------
------------
Disclaimer: This document is provided "AS IS" with no warranties, and
confers no rights.


This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.co....

noam

7/23/2002 9:54:00 PM

0

Douglas,

Thank you for your help.

Your response to my 3rd question:

3. Yes:
Q318373 HOW TO: Use GetOleDbSchemaTable with Excel in Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-...

is exactly what I was looking for.
The example in the HOWTO helped a lot.

Thanks.
Noam