Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help with reading
Microsoft excel file
Thanks alot
Posted
Comments
Awolola Adedeji 12-Mar-14 5:48am    
I will appreciate if it could cover reading it into an excel file
Sirstee5S 13-Feb-17 7:09am    
This is the simplest and most efficient way that I found to read an Excel file in C#, in order to use hat approach you need to download a C# Excel library.

It's your lucky day!
I was just looking at teh page in my code where I do just that...
C#
/// <summary>
/// Load a sheet from an Excel file
/// </summary>
/// <param name="path">Path to excel file</param>
/// <param name="columns">Comma separated list of columns to load. If not specified, loads all</param>
/// <param name="sheet">Sheet name to load. If not specified, loads "Sheet1$"</param>
/// <param name="verify">If true, checks that the file and sheet exist, and throws exceptions if not. Defaults to true</param>
/// <returns>Table of data retrieved.</returns>
public static System.Data.DataTable LoadExcel(string path, string columns = "*", string sheet = "Sheet1$", bool verify = true)
    {
    if (verify)
        {
        if (!File.Exists(path)) throw new IOException("The input file does not exist: " + path);
        if (!GetExcelSheetNames(path).Contains(sheet)) throw new ArgumentException("The requested sheet does not exist: " + sheet);
        }
    if (string.IsNullOrWhiteSpace(columns)) columns = "*";

    System.Data.DataTable dt = new System.Data.DataTable();
    using (OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path)))
        {
        con.Open();
        string cmdStr = string.Format("SELECT {0} FROM [{1}]", columns, sheet);
        using (OleDbCommand cmd = new OleDbCommand(cmdStr, con))
            {
            using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
                {
                da.Fill(dt);
                }
            }
        }
    return dt;
    }
/// <summary>
/// Returns a list of all sheet names in an Excel File.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static List<string> GetExcelSheetNames(string path)
    {
    List<string> sheetNames = new List<string>();
    using (OleDbConnection con = new OleDbConnection(GetExcelConnectionString(path)))
        {
        con.Open();
        using (DataTable sheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null))
            {
            foreach (DataRow sheet in sheets.Rows)
                {
                if (sheet["TABLE_NAME"].ToString().Contains("$"))
                    {
                    // Filtered to just sheets - they all end in '$'
                    sheetNames.Add(sheet["TABLE_NAME"].ToString());
                    }
                }
            }
        }
    return sheetNames;
    }
/// <summary>
/// Returns a connection string for an Excel file
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static string GetExcelConnectionString(string path)
    {
    return string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0", path);
    }
 
Share this answer
 
We're using EPPlus[^] to read Excel files. The requirement is that they are saved in the Open Office XML format (xlsx), but you wouldn't need to have Excel installed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900