ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Importing data from the active sheet w/ c# (https://www.excelbanter.com/excel-programming/370520-importing-data-active-sheet-w-c.html)

Adrian Paul

Importing data from the active sheet w/ c#
 
Hi,
i'm using this code to import data from an EXCEL file:

oleConn = new OleDbConnection(strConn);
oleConn.Open();

dtSchema = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables , null);
DataRow fRow = dtSchema.Rows[0];
TheSheet = fRow["TABLE_NAME"].ToString();
TheSheet = TheSheet.Substring(0, TheSheet.Length - 1);
oleConn.Close();

if (!TheSheet.EndsWith("$")) TheSheet += "$";
OleDbDataAdapter myAdapter = new OleDbDataAdapter("SELECT * FROM [" +
TheSheet+ "]", strConn);
myAdapter.Fill(ds);


The problem occurs when the first sheet (DataRow fRow = dtSchema.Rows[0];)
is not the active one.

So my question is ... in the code above, how can i get the actie sheet
instead of the first one?

News[_3_]

Importing data from the active sheet w/ c#
 
I'm not a C# (or anything.Net) person, but I guess this is using OLEDB to
query an Excel file.
As such the file is closed and the concept of "ActiveSheet" does not apply.
You return data from the tables (worksheets) in the SQL executed.
I suppose...

NickHK

"Adrian Paul" <Adrian ...
Hi,
i'm using this code to import data from an EXCEL file:

oleConn = new OleDbConnection(strConn);
oleConn.Open();

dtSchema = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables , null);
DataRow fRow = dtSchema.Rows[0];
TheSheet = fRow["TABLE_NAME"].ToString();
TheSheet = TheSheet.Substring(0, TheSheet.Length - 1);
oleConn.Close();

if (!TheSheet.EndsWith("$")) TheSheet += "$";
OleDbDataAdapter myAdapter = new OleDbDataAdapter("SELECT * FROM [" +
TheSheet+ "]", strConn);
myAdapter.Fill(ds);


The problem occurs when the first sheet (DataRow fRow = dtSchema.Rows[0];)
is not the active one.

So my question is ... in the code above, how can i get the actie sheet
instead of the first one?





All times are GMT +1. The time now is 06:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com