Excel 2.1 - .NET Automation

J

jworksinc

I have an application that needs to retrieve the name of the first
Worksheet in an Excel Workbook. This works great, unless an older
excel file is used with this routine that did not support multiple
sheets (i.e. Excel 2.1). What is the work around to retrieve the sheet
name?

Here is some code:

private string GetFirstSheetName(sExcelFile)
{
....
oExcel = new Excel.ApplicationClass();
oBooks = oExcel.Workbooks;
oBook =
oBooks.Open(sExcelFile,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
oSheets = oBook.Worksheets;
oSheet = (Excel.Worksheet)oSheets[1];
return oSheet.Name;
}

If sExcelFile is a 2.1 Excel file the name of the file gets returned
for some reason.

Thanks
 
J

Jim Rech

If sExcelFile is a 2.1 Excel file the name of the file gets returned for
The reason is that prior to Excel 5 worksheets did not have names beyond
their file names.

--
Jim
|I have an application that needs to retrieve the name of the first
| Worksheet in an Excel Workbook. This works great, unless an older
| excel file is used with this routine that did not support multiple
| sheets (i.e. Excel 2.1). What is the work around to retrieve the sheet
| name?
|
| Here is some code:
|
| private string GetFirstSheetName(sExcelFile)
| {
| ...
| oExcel = new Excel.ApplicationClass();
| oBooks = oExcel.Workbooks;
| oBook =
|
oBooks.Open(sExcelFile,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
| oSheets = oBook.Worksheets;
| oSheet = (Excel.Worksheet)oSheets[1];
| return oSheet.Name;
| }
|
| If sExcelFile is a 2.1 Excel file the name of the file gets returned
| for some reason.
|
| Thanks
|
 
J

jworksinc

Any idea on how I could access a sheet in Excel 2.1 using OLEDB?

Most queries look like this with more recent version of excel:

SELECT * FROM [Sheet Name$]

What happens when there are no sheets as in the case of older excel
versions?
 
J

Jim Rech

Sorry, out of my depth here. Maybe the file name would work? (Q - Would a
DB driver that works with XL97+ files work with older Excel file formats?)

--
Jim
| Any idea on how I could access a sheet in Excel 2.1 using OLEDB?
|
| Most queries look like this with more recent version of excel:
|
| SELECT * FROM [Sheet Name$]
|
| What happens when there are no sheets as in the case of older excel
| versions?
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top