How to Read an Excel File in C Sharp

Photo by Markus Spiske Originally Posted On: https://ironsoftware.com/csharp/excel/tutorials/how-to-read-excel-file-csharp/

Saturday, March 7th 2020, 2:49 am

By: News On 6


How to Read an Excel File in C SharpPhoto by Markus Spiske

Originally Posted On: https://ironsoftware.com/csharp/excel/tutorials/how-to-read-excel-file-csharp/

 

  1. Install IronXL Excel Library from NuGet or the DLL download
  2. Use the WorkBook.Load method to read any XLS, XLSX or CSV document.
  3. Get Cell values using intuitive syntax: sheet[“A11”].DecimalValue

In this tutorial, we will see how easy it is to read Excel files in C# or VB.Net using the IronXL “Excel” library.

Installation

The first thing we need to do is install the IronXL.Excel library, adding Excel functionality to the .NET framework.

We can do this using our NuGet package or by downloading the .Net Excel DLL.

PM > Install-Package IronXL.Excel

Read an XLS or XLSX File

In this example we can see that Excel files can be read efficiently without Interop in C#:

  1. using IronXL;
  2. using System.Linq;
  3. //Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
  4. WorkBook workbook = WorkBook.Load(“test.xlsx”);
  5. WorkSheet sheet = workbook.WorkSheets.First();
  6. //Select cells easily in Excel notation and return the calculated value
  7. int cellValue = sheet[“A2”].IntValue;
  8. // Read from Ranges of cells elegantly.
  9. foreach (var cell in sheet[“A2:A10”])
  10. {
  11. Console.WriteLine(“Cell {0} has value ‘{1}'”, cell.AddressString, cell.Text);
  12. }
  13. ///Advanced Operations
  14. //Calculate aggregate values such as Min, Max and Sum
  15. decimal sum = sheet[“A2:A10”].Sum();
  16. //Linq compatible
  17. decimal max = sheet[“A2:A10”].Max(c => c.DecimalValue);

Copy code to clipboardVB C#

The final Advanced Operations show Linq compatibility and aggregate range mathematics.

Learning More

Further Documentation

You may also find the IronXL class documentation within the Object Reference of great value.

In addition, there are other tutorials which may shed light in other aspects of IronXL.Excel including Creating, Reading, Editing Saving and Exporting XLS, XLSX and CSV files without using Excel Interop..

Information contained on this page is provided by an independent third-party content provider. Frankly and this Site make no warranties or representations in connection therewith. If you are affiliated with this page and would like it removed please contact pressreleases@franklymedia.com

logo

Get The Daily Update!

Be among the first to get breaking news, weather, and general news updates from News on 6 delivered right to your inbox!

More Like This

March 7th, 2020

December 15th, 2024

December 14th, 2024

December 14th, 2024

Top Headlines

December 15th, 2024

December 15th, 2024

December 14th, 2024

December 14th, 2024