How to read an excel file in C# without using Microsoft.Office.Interop.Excel libraries

How to read an excel file in C# without using Microsoft.Office.Interop.Excel libraries

Reading Excel files in C# without using the Microsoft.Office.Interop.Excel library can be achieved using third-party libraries or by directly reading the Excel file's data without Excel automation.

Here's how you can do it using a popular third-party library called EPPlus:

  1. Install EPPlus: First, you need to install the EPPlus library. You can do this via NuGet Package Manager in Visual Studio or by running the following command in the Package Manager Console:
Install-Package EPPlus 
  1. Read Excel Data:
using System; using System.IO; using OfficeOpenXml; class Program { static void Main(string[] args) { // Path to your Excel file string filePath = @"C:\path\to\your\excel.xlsx"; // Check if the file exists if (!File.Exists(filePath)) { Console.WriteLine("File not found!"); return; } // Read Excel file using (ExcelPackage package = new ExcelPackage(new FileInfo(filePath))) { // Get the first worksheet in the workbook ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; // Get the number of rows and columns in the worksheet int rowCount = worksheet.Dimension.Rows; int colCount = worksheet.Dimension.Columns; // Loop through each cell in the worksheet for (int row = 1; row <= rowCount; row++) { for (int col = 1; col <= colCount; col++) { // Read cell value object cellValue = worksheet.Cells[row, col].Value; // Do something with the cell value Console.Write(cellValue + "\t"); } Console.WriteLine(); } } } } 

This code reads an Excel file using the EPPlus library and outputs the content to the console. Adjust the file path (filePath) to point to your Excel file.

EPPlus provides a more friendly and efficient way to work with Excel files in C# compared to using interop libraries like Microsoft.Office.Interop.Excel. Additionally, EPPlus does not require Excel to be installed on the machine running the code.

Examples

  1. "C# read Excel file without Interop libraries"

    • Description: Explains alternatives to using Microsoft.Office.Interop.Excel libraries for reading Excel files in C#, offering solutions that avoid dependency on Excel installation.
    using ExcelDataReader; using System.Data; using System.IO; // Read Excel file using ExcelDataReader library using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read)) { using (var reader = ExcelReaderFactory.CreateReader(stream)) { // DataSet result var result = reader.AsDataSet(); DataTableCollection tableCollection = result.Tables; DataTable table = tableCollection["Sheet1"]; // Access data from 'table' DataTable object } } 
  2. "C# read Excel file without Microsoft Interop in .NET"

    • Description: Discusses methods to read Excel files in C# without using Microsoft Interop libraries, providing alternative approaches for accessing Excel data.
    using ClosedXML.Excel; // Read Excel file using ClosedXML library using (var workbook = new XLWorkbook(filePath)) { var worksheet = workbook.Worksheet(1); // Access data from 'worksheet' object } 
  3. "How to parse Excel file in C# without Interop Excel"

    • Description: Provides techniques for parsing Excel files in C# without relying on Interop Excel libraries, offering alternative solutions for data extraction.
    using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.IO; // Read Excel file using NPOI library using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { IWorkbook workbook = new XSSFWorkbook(file); ISheet sheet = workbook.GetSheetAt(0); // Access data from 'sheet' object } 
  4. "C# open Excel file without Interop"

    • Description: Describes methods to open Excel files in C# without using Interop libraries, providing alternative approaches for accessing Excel data.
    using GemBox.Spreadsheet; // Read Excel file using GemBox.Spreadsheet library ExcelFile ef = ExcelFile.Load(filePath); ExcelWorksheet ws = ef.Worksheets[0]; // Access data from 'ws' ExcelWorksheet object 
  5. "Reading Excel file in C# without Interop Excel dependencies"

    • Description: Explains how to read Excel files in C# without dependencies on Interop Excel, offering alternative libraries and methods for Excel data retrieval.
    using Syncfusion.XlsIO; // Read Excel file using Syncfusion.XlsIO library using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; IWorkbook workbook = application.Workbooks.Open(filePath); IWorksheet worksheet = workbook.Worksheets[0]; // Access data from 'worksheet' object } 
  6. "C# read Excel file without Office Interop"

    • Description: Discusses techniques to read Excel files in C# without using Office Interop libraries, providing alternative solutions for Excel data extraction.
    using ExcelDataReader; using System.Data; using System.IO; // Read Excel file using ExcelDataReader library using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read)) { using (var reader = ExcelReaderFactory.CreateReader(stream)) { // DataSet result var result = reader.AsDataSet(); DataTableCollection tableCollection = result.Tables; DataTable table = tableCollection["Sheet1"]; // Access data from 'table' DataTable object } } 
  7. "Reading Excel file in C# without Excel Interop"

    • Description: Provides methods to read Excel files in C# without relying on Excel Interop libraries, offering alternative approaches for Excel data access.
    using ClosedXML.Excel; // Read Excel file using ClosedXML library using (var workbook = new XLWorkbook(filePath)) { var worksheet = workbook.Worksheet(1); // Access data from 'worksheet' object } 
  8. "C# parse Excel file without Interop Excel"

    • Description: Offers techniques for parsing Excel files in C# without using Interop Excel libraries, providing alternative solutions for data extraction.
    using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.IO; // Read Excel file using NPOI library using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { IWorkbook workbook = new XSSFWorkbook(file); ISheet sheet = workbook.GetSheetAt(0); // Access data from 'sheet' object } 
  9. "C# open Excel file without Interop Excel"

    • Description: Describes methods to open Excel files in C# without using Interop Excel libraries, providing alternative approaches for accessing Excel data.
    using GemBox.Spreadsheet; // Read Excel file using GemBox.Spreadsheet library ExcelFile ef = ExcelFile.Load(filePath); ExcelWorksheet ws = ef.Worksheets[0]; // Access data from 'ws' ExcelWorksheet object 
  10. "C# read Excel file without Excel Interop dependencies"

    • Description: Explains how to read Excel files in C# without dependencies on Excel Interop libraries, offering alternative libraries and methods for Excel data retrieval.
    using Syncfusion.XlsIO; // Read Excel file using Syncfusion.XlsIO library using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; IWorkbook workbook = application.Workbooks.Open(filePath); IWorksheet worksheet = workbook.Worksheets[0]; // Access data from 'worksheet' object } 

More Tags

concurrent-collections selectize.js train-test-split char testing py-amqplib rider exoplayer2.x iteration linq-to-nhibernate

More Programming Questions

More Biochemistry Calculators

More Organic chemistry Calculators

More Other animals Calculators

More Mixtures and solutions Calculators