Horje
Microsoft.ACE.OLEDB.12.0  c# excel first sheet Code Example
Microsoft.ACE.OLEDB.12.0 c# excel first sheet
internal static DataTable GetExcelSheet(string excelFile,string sheetName = "")
{
    string fullPathToExcel = Path.GetFullPath(excelFile);
    string connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel " + (excelFile.ToLower().EndsWith("x") ? "12.0" : "8.0") + ";HDR=yes'", fullPathToExcel);
    return GetDataTable(connString, "SELECT * FROM [" + (string.IsNullOrEmpty(sheetName) ? GetTableName(connString, 0) : sheetName + "$") + "]");
}

private static DataTable GetDataTable(string connectionString, string sql)
{
    DataTable dt = new DataTable();

    using (OleDbConnection conn = new OleDbConnection(connectionString))
    {
        conn.Open();
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
        {
            using (OleDbDataReader rdr = cmd.ExecuteReader())
            {
                dt.Load(rdr);
                return dt;
            }
        }
    }
}
private static string GetTableName(string connectionString, int row = 0)
{
    OleDbConnection conn = new OleDbConnection(connectionString);
    try
    {
        conn.Open();
        return conn.GetSchema("Tables").Rows[row]["TABLE_NAME"] + "";
    }
    catch { }
    finally { conn.Close();}
    return "sheet1";
}




Csharp

Related
c# map number range Code Example c# map number range Code Example
invert bool c# Code Example invert bool c# Code Example
conncet oracle database in c# visual studio Code Example conncet oracle database in c# visual studio Code Example
C# unary operator Code Example C# unary operator Code Example
add buttons to taskbar thumbnail WPF Code Example add buttons to taskbar thumbnail WPF Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
11