We can use the codes below to read the sheet name mySheet in a Exel file.

string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" +
Server.MapPath("Book1.xls") + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from
[mySheet$]",conn);
DataSet ds = new DataSet();
....

But if we do'nt know the sheet name, but I just want read the first sheet in
the excel file.
How can I do?

Re: How to read the first sheet in Excel by Cor

Cor
Mon May 08 00:20:13 CDT 2006

Ad,

da.Fill(ds, "Sheet1");

I hope this helps,

Cor


"ad" <flying@wfes.tcc.edu.tw> schreef in bericht
news:OyM4U8hcGHA.3900@TK2MSFTNGP05.phx.gbl...
> We can use the codes below to read the sheet name mySheet in a Exel file.
>
> string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" +
> Server.MapPath("Book1.xls") + ";Extended Properties=Excel 8.0";
> OleDbConnection conn = new OleDbConnection(strConn);
> OleDbDataAdapter adp = new OleDbDataAdapter("Select * from
> [mySheet$]",conn);
> DataSet ds = new DataSet();
> ....
>
> But if we do'nt know the sheet name, but I just want read the first sheet
> in the excel file.
> How can I do?
>



Re: How to read the first sheet in Excel by ad

ad
Mon May 08 07:05:04 CDT 2006

Thanks,
But how to modify the line below when I don't know the WhorkSheet name at
first?

OleDbDataAdapter adp = new OleDbDataAdapter("Select * from
[mySheet$]",conn);



"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> ¼¶¼g©ó¶l¥ó·s»D:uncGV7lcGHA.1276@TK2MSFTNGP03.phx.gbl...
> Ad,
>
> da.Fill(ds, "Sheet1");
>
> I hope this helps,
>
> Cor
>
>
> "ad" <flying@wfes.tcc.edu.tw> schreef in bericht
> news:OyM4U8hcGHA.3900@TK2MSFTNGP05.phx.gbl...
>> We can use the codes below to read the sheet name mySheet in a Exel file.
>>
>> string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" +
>> Server.MapPath("Book1.xls") + ";Extended Properties=Excel 8.0";
>> OleDbConnection conn = new OleDbConnection(strConn);
>> OleDbDataAdapter adp = new OleDbDataAdapter("Select * from
>> [mySheet$]",conn);
>> DataSet ds = new DataSet();
>> ....
>>
>> But if we do'nt know the sheet name, but I just want read the first sheet
>> in the excel file.
>> How can I do?
>>
>
>



Re: How to read the first sheet in Excel by Paul

Paul
Mon May 08 09:22:34 CDT 2006

On Mon, 8 May 2006 05:41:59 +0800, "ad" <flying@wfes.tcc.edu.tw> wrote:

¤ We can use the codes below to read the sheet name mySheet in a Exel file.
¤
¤ string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" +
¤ Server.MapPath("Book1.xls") + ";Extended Properties=Excel 8.0";
¤ OleDbConnection conn = new OleDbConnection(strConn);
¤ OleDbDataAdapter adp = new OleDbDataAdapter("Select * from
¤ [mySheet$]",conn);
¤ DataSet ds = new DataSet();
¤ ....
¤
¤ But if we do'nt know the sheet name, but I just want read the first sheet in
¤ the excel file.
¤ How can I do?

There are only two ways, of which I am aware, that will enable you to retrieve an Excel Worksheet by
its ordinal position. First method is to use DAO:

Dim xlWB As DAO.Database
Dim strFirstSheetName As String

xlWB = OpenDatabase("C:\Test Files\Book10.xls", False, True, "Excel 8.0;")

strFirstSheetName = xlWB.TableDefs(0).Name

xlWB.Close

...and the other is to use automation with Microsoft Excel:

Dim obj As Excel.Application
Dim objWB As Excel.Workbook
Dim strFirstSheetName As String

obj = CreateObject("Excel.Application")
objWB = obj.Workbooks.Open("C:\Test Files\Book10.xls")

strFirstSheetName = objWB.Worksheets(1).Name

objWB.Close
objWB = Nothing
obj.Quit
obj = Nothing


Paul
~~~~
Microsoft MVP (Visual Basic)