Open and read Excel file in C# and VB.NET

GemBox.Spreadsheet can open many Excel file formats (including XLSX, XLS, ODS, CSV, and HTML) in the same manner. Specifically, it uses an ExcelFile.Load method to read from an Excel file or stream in C# and VB.NET.

The following example shows how you can read all ExcelCells, their Value, and ValueType properties in an ExcelFile object.

Excel cell values read with GemBox.Spreadsheet
Screenshot of Excel cell values read with GemBox.Spreadsheet
Upload your file (Drag file here)
using System;
using System.Text;
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = ExcelFile.Load("%InputFileName%");

        var sb = new StringBuilder();

        // Iterate through all worksheets in an Excel workbook.
        foreach (var worksheet in workbook.Worksheets)
        {
            sb.AppendLine();
            sb.AppendFormat("{0} {1} {0}", new string('-', 25), worksheet.Name);

            // Iterate through all rows in an Excel worksheet.
            foreach (var row in worksheet.Rows)
            {
                sb.AppendLine();

                // Iterate through all allocated cells in an Excel row.
                foreach (var cell in row.AllocatedCells)
                    if (cell.ValueType != CellValueType.Null)
                        sb.Append(string.Format("{0} [{1}]", cell.Value, cell.ValueType).PadRight(25));
                    else
                        sb.Append(new string(' ', 25));
            }
        }

        Console.WriteLine(sb.ToString());
    }
}
Imports System
Imports System.Text
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = ExcelFile.Load("%InputFileName%")

        Dim sb = New StringBuilder()

        ' Iterate through all worksheets in an Excel workbook.
        For Each worksheet In workbook.Worksheets

            sb.AppendLine()
            sb.AppendFormat("{0} {1} {0}", New String("-"c, 25), worksheet.Name)

            ' Iterate through all rows in an Excel worksheet.
            For Each row In worksheet.Rows

                sb.AppendLine()

                ' Iterate through all allocated cells in an Excel row.
                For Each cell In row.AllocatedCells
                    If cell.ValueType <> CellValueType.Null Then
                        sb.Append(String.Format("{0} [{1}]", cell.Value, cell.ValueType).PadRight(25))
                    Else
                        sb.Append(New String(" "c, 25))
                    End If
                Next
            Next
        Next

        Console.WriteLine(sb.ToString())
    End Sub
End Module

Note that, when you iterate through all read cells in an ExcelRow, you should use AllocatedCells property to prevent unnecessary allocation of ExcelCell objects.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.