Read Excel file in C# and VB.NET

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

The following sample demonstrates how to read all ExcelCells, their Value and ValueTypes, in ExcelFile object.

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

Screenshot

Reading Excel Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.Drawing;
3using System.Text;
4using GemBox.Spreadsheet;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
13
14        ExcelFile ef = ExcelFile.Load("SimpleTemplate.xlsx");
15
16        StringBuilder sb = new StringBuilder();
17
18        // Iterate through all worksheets in an Excel workbook.
19        foreach (ExcelWorksheet sheet in ef.Worksheets)
20        {
21            sb.AppendLine();
22            sb.AppendFormat("{0} {1} {0}", new string('-', 25), sheet.Name);
23
24            // Iterate through all rows in an Excel worksheet.
25            foreach (ExcelRow row in sheet.Rows)
26            {
27                sb.AppendLine();
28
29                // Iterate through all allocated cells in an Excel row.
30                foreach (ExcelCell cell in row.AllocatedCells)
31                {
32                    if (cell.ValueType != CellValueType.Null)
33                        sb.Append(string.Format("{0} [{1}]", cell.Value, cell.ValueType).PadRight(25));
34                    else
35                        sb.Append(new string(' ', 25));
36                }
37            }
38        }
39
40        Console.WriteLine(sb.ToString());
41    }
42}
1Imports System.Text
2Imports System.Drawing
3Imports GemBox.Spreadsheet
4
5Module Samples
6
7    Sub Main()
8
9        ' If using Professional version, put your serial key below.
10        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
11
12        Dim ef As ExcelFile = ExcelFile.Load("SimpleTemplate.xlsx")
13
14        Dim sb As New StringBuilder()
15
16        ' Iterate through all worksheets in an Excel workbook.
17        For Each sheet As ExcelWorksheet In ef.Worksheets
18            sb.AppendLine()
19            sb.AppendFormat("{0} {1} {0}", New String("-"c, 25), sheet.Name)
20
21            ' Iterate through all rows in an Excel worksheet.
22            For Each row As ExcelRow In sheet.Rows
23                sb.AppendLine()
24
25                ' Iterate through all allocated cells in an Excel row.
26                For Each cell As ExcelCell In row.AllocatedCells
27                    If cell.ValueType <> CellValueType.Null Then
28                        sb.Append(String.Format("{0} [{1}]", cell.Value, cell.ValueType).PadRight(25))
29                    Else
30                        sb.Append(New String(" "c, 25))
31                    End If
32                Next
33            Next
34        Next
35
36        Console.WriteLine(sb.ToString())
37
38    End Sub
39
40End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.