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 shows how to read cell values and types.

Note that when you iterate through all read cells in an ExcelRow, you should use AllocatedCellsExcelCell objects.

Screenshot

Reading 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        foreach (ExcelWorksheet sheet in ef.Worksheets)
19        {
20            sb.AppendLine();
21            sb.AppendFormat("--------- {0} ---------", sheet.Name);
22
23            foreach (ExcelRow row in sheet.Rows)
24            {
25                sb.AppendLine();
26                foreach (ExcelCell cell in row.AllocatedCells)
27                {
28                    if (cell.Value != null)
29                        sb.AppendFormat("{0}({1})", cell.Value, cell.Value.GetType().Name);
30
31                    sb.Append("\t");
32                }
33            }
34        }
35
36        Console.WriteLine(sb.ToString());
37    }
38}
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 StringBuilder = New StringBuilder
15
16        For Each sheet As ExcelWorksheet In ef.Worksheets
17
18            sb.AppendLine()
19            sb.AppendFormat("--------- {0} ---------", sheet.Name)
20
21            For Each row As ExcelRow In sheet.Rows
22                sb.AppendLine()
23                For Each cell As ExcelCell In row.AllocatedCells
24                    If Not cell.Value Is Nothing Then
25                        sb.AppendFormat("{0}({1})", cell.Value, cell.Value.GetType().Name)
26                    End If
27
28                    sb.Append(vbTab)
29                Next
30            Next
31        Next
32
33        Console.WriteLine(sb.ToString())
34
35    End Sub
36
37End Module

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