How to export data from XLS spreadsheet to Data Table with Spreadsheet SDK - ByteScout

How to export data from XLS spreadsheet to Data Table with Spreadsheet SDK

  • Home
  • /
  • Articles
  • /
  • How to export data from XLS spreadsheet to Data Table with Spreadsheet SDK

This example demonstrates how to get data from XLS spreadsheet and export them to data table in C# and Visual Basic .NET using Bytescout Spreadsheet SDK.

C#

using System;
using System.Data;

namespace Bytescout.Spreadsheet.Demo.Csharp.ExportToDataTable
{
    class Program
    {
        static void Main(string[] args)
        {
            const string inputFile = @"PeriodicTableOfElementsSpreadsheet.xls";

            // Open and load spreadsheet
            Spreadsheet spreadsheet = new Spreadsheet();
            spreadsheet.LoadFromFile(inputFile);

            // Get the data from the spreadsheet
            DataTable dt = spreadsheet.ExportToDataTable();

            // Close spreadsheet
            spreadsheet.Close();

            // Display array
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Console.Write(dt.Rows&#91;i&#93;&#91;j&#93; + " ");
                }
                Console.WriteLine();
            }

            // Pause
            Console.ReadLine();
        }
    }
}
&#91;/vb&#93;
<p><strong>VB.NET</strong></p>
[vb]Module Module1

    Sub Main()
        Dim inputFile As String = "PeriodicTableOfElementsSpreadsheet.xls"

        'Open and load spreadsheet
        Dim spreadsheet = New Spreadsheet()
        spreadsheet.LoadFromFile(inputFile)

        'Get the data from the spreadsheet
        Dim dt As DataTable = spreadsheet.ExportToDataTable()

        'Close spreadsheet
        spreadsheet.Close()

        'Display data in datatable
        For i As Integer = 0 To dt.Rows.Count - 1
            For j As Integer = 0 To dt.Columns.Count - 1

                Console.Write(dt.Rows(i)(j) + " ")
            Next
            Console.WriteLine()
        Next

        'Pause
        Console.ReadLine()
    End Sub

End Module

Tutorials:

prev
next