How to read an excel (*.xls/.xlsx) file in C#. The C# Basics beginner course is a free C# Tutorial Series that helps beginning programmers learn the basics of the C# Programming Language. This is the best way to Learn C# for beginners

c# – Optimal way to Read an Excel file (.xls/.xlsx)
How to read the xls and xlsx files using c#
.net – Reading Excel files from C#
Read Excel XLS XLSX files in C#
Read and display data from an excel file (.xls and .xlsx)
Excel Data Reader – Read Excel files in .NET
Read Excel File in C# (Example)
C# – How To Get Data From Excel File In C# [with source code]
How to import excel file to datagridview in c#
How to Open Excel File in C#
C# Import From Excel Using OleDb Tutorial

Nguồn: https://kuherbal.com/

Xem thêm bài viết khác: https://kuherbal.com/category/cong-nghe

Author

35 Comments

  1. انا مسلم و كفى Reply

    iv a two pbs
    1. that when i click or select the sheet again so it reads the excel again and that wht idn want have
    2. when the sheet is empty so ive to show a messagebx that the Sheet empty si how i can check if the Sheet empty or not thank you so much 🙂

  2. Frank Hollstein Reply

    also the same:

    "IExcelDataReader" enthält keine Definition für "AsDataSet"
    what is to do?

  3. Jorge Manrique Reply

    Hi. I have a problem in this code line
    reader = ExcelReaderFactory.CreateOpenXmlReader(fs);
    System.IO.FileNotFoundException: 'No se puede cargar el archivo o ensamblado 'ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73' ni una de sus dependencias. El sistema no puede encontrar el archivo especificado.'

    What can i do to solve this?
    Thanks.

  4. TheInternetdirect Reply

    reader.IsFirstRowAsColumnName = true; doesnt show up, Cant add "using Excel;" i added "using ExcelDataReader;" help

  5. sarthak menpara Reply

    hello i am getting error like this can anyone have solution?
    System.IO.FileNotFoundException: 'Could not load file or assembly 'ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73' or one of its dependencies. The system cannot find the file specified.'

  6. Here is the working code for newer version. I found it on source's github and combined it with this video's code
    NOTE: need to nuget both ExcelDataReader and ExcelDataReader.DataSet

    using (OpenFileDialog open = new OpenFileDialog() { Filter = "Excel Workbook (*.xlsx)|*.xlsx|Excel 97-2003 Workbook (*.xls)|*.xls", ValidateNames = true })

    {

    if (open.ShowDialog() == DialogResult.OK)

    {

    using (FileStream fs = File.Open(open.FileName, FileMode.Open, FileAccess.Read))

    using (var reader = ExcelReaderFactory.CreateReader(fs))

    {

    result = reader.AsDataSet();

    cmbSheets.Items.Clear();

    foreach (DataTable dt in result.Tables)

    cmbSheets.Items.Add(dt.TableName);

    }

    }

    }

  7. For those which are interested to compile the above demo with the last ExcelDataReader 3.x, the corrections proposed here worked well for me:
    https://stackoverflow.com/questions/27634477/using-exceldatareader-to-read-excel-data-starting-from-a-particular-cell

    Or if you prefer, the following corrected code is working for 3.x versions. But you need to also install the ExcelDataReader.DataSet package from NuGet:
    using (OpenFileDialog ofd = new OpenFileDialog() {Filter = "Excel Workbook 97-2003|*.xls|Excel Workbook|*.xlsx", ValidateNames = true})
    {
    if (ofd.ShowDialog() == DialogResult.OK)
    {
    FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);

    IExcelDataReader reader;

    if (ofd.FilterIndex == 1)
    reader = ExcelReaderFactory.CreateBinaryReader(fs);
    else
    reader = ExcelReaderFactory.CreateOpenXmlReader(fs);

    var conf = new ExcelDataSetConfiguration
    {
    ConfigureDataTable = _ => new ExcelDataTableConfiguration
    {
    UseHeaderRow = true
    }
    };

    result = reader.AsDataSet(conf);

    cbSheet.Items.Clear();

    foreach (DataTable dt in result.Tables)
    cbSheet.Items.Add(dt.TableName);

    reader.Close();
    }
    }

  8. hello. I'm getting error NullReferenceException: Object reference not set to an instance of an object.

    At foreach<..> result.Tables. What should I do?

  9. GAMING GHOST Reply

    hey how to fix this ??
    ExcelDataReader.Exceptions.HeaderException HResult=0x80131500 Message=Invalid file signature. Source=ExcelDataReader

    please help

  10. Wonjae Jung Reply

    Thank you Fox learn! But, I faced the some problem that is when I import the excel file, it doesn't import excel style. How to import the excel format/style in WInforms.

  11. Edgardo Pichardo Reply

    I can not read xlsx files. I'm getting this error:

    ExcelDataReader.Exceptions.HeaderException
    HResult=0x80131500
    Message=Invalid file signature.
    Source=ExcelDataReader

    Any ideas how to solve it?

  12. Stelios Michael Reply

    Hello i getting this errorSystem.NullReferenceException: 'Object reference not set to an instance of an object.'

    result was null. can you pls help me with this?

  13. I tried this and working fine, but i have need to reuse the data read from excel to check with values are available in database.
    so plz send the code to reuse returning data viruvenkat27@gmail.com

  14. Hello, I am currently working on a very important project. May I ask if any of the good programmers can help me with it? It's actually using Visual C# to read an Excel/Word file populate into the Outlook calendar and the professors view the calendar using their mobile device. Will appreciate if any of y'all are able to help/guide me. Thank you. My email is limseeyin@gmail.com

  15. Mike Muñoz Reply

    the solution for IsFirstRowAsColumnNames is install the version 2.1 because the last version 3.0 is incompatible for this method :')

  16. Thivya Sekar Reply

    isfirstrowascolumnNames is showing error as no extension method , pls help me , mail @ jsthivyasekar@gmail.com

  17. Hellblazer 85 Reply

    hi, i installed the ExcelData reader and the IExcelDataReader doesnt show up.

    please help.

    thanks a lot

  18. Sahil Gurav Reply

    I Get a RED squiggly line under 'IExcelDataReader' & 'ExcelReaderFactory' any help…??? Thanks in Advance

  19. Abubker Ayiad Reply

    Thank you foxlearn for your great efforts to help people learn C#. As all the past sample worked perfectly with me, this one does not, it shows the red lilne under 'reader' with a tip says:" 'reader' is a variable but used as a type ". any suggestions please?

  20. I keep getting a squiggly line under '.IsFirstRowAsColumnNames'. For some reason, 'IsFirstRowAsColumnNames' doesn't even appear in the intellisense. Is there a reference i am not getting right?

  21. Mamdouh Aljunied Reply

    first of all thank you very much for this great video.
    I am wondering how I can read date and time correctly because the date and time were read float, not as date and time

  22. Adam O'Leary Reply

    Hi, Im getting an error "Form1.result is never assigned to, and will always have its default value null"

  23. Maryland Macapilit Reply

    How to load the excel file directly in the datagridview without using combo box?
    Please Help, Thanks

Write A Comment