using System;
using System.Data;
using Aspose.Cells;

namespace ConvertExcelToDataTableInCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the license to generate output file without the trial version watermark
            License license = new License();
            license.SetLicense("Aspose.Cells.lic");

            // Instantiate the new Excel file for importing to DataTable 
            Workbook ExcelToDataTable = new Workbook();
 
            // Access first worksheet
            Worksheet worksheetToDataTable = ExcelToDataTable.Worksheets[0];

            // Insert sample data to be exported to DataTable in the first sheet
            worksheetToDataTable.Cells["A1"].Value = "Roll No";
            worksheetToDataTable.Cells["B1"].Value = "Name";
            worksheetToDataTable.Cells["C1"].Value = "Age";

            worksheetToDataTable.Cells["A2"].Value = 1;
            worksheetToDataTable.Cells["B2"].Value = "Bob";
            worksheetToDataTable.Cells["C2"].Value = 21.5;

            worksheetToDataTable.Cells["A3"].Value = 2;
            worksheetToDataTable.Cells["B3"].Value = "Neil";
            worksheetToDataTable.Cells["C3"].Value = 23;

            worksheetToDataTable.Cells["A4"].Value = 3;
            worksheetToDataTable.Cells["B4"].Value = "John";
            worksheetToDataTable.Cells["C4"].Value = 25;

            // Exporting the contents of 4 rows and 3 columns starting from 1st cell to DataTable
            DataTable dataTable = worksheetToDataTable.Cells.ExportDataTable(0, 0, 4, 3, true);

            // Verify data in the DataTable by displaying all the columns in each row
            for (int iDataTableRow = 0; iDataTableRow < dataTable.Rows.Count; iDataTableRow++)
            {
                // Fetch individual column data
                Int32 RollNo  = Convert.ToInt32(dataTable.Rows[iDataTableRow]["Roll No"]);
                String Name  = Convert.ToString(dataTable.Rows[iDataTableRow]["Name"]);
                Double Age  = Convert.ToDouble(dataTable.Rows[iDataTableRow]["Age"]);

                // Display data in the row
                System.Console.WriteLine($"Roll No:{RollNo}, Name : {Name}, Age : {Age} Years");
            }

            // Save the Excel file that is Imported to DataTable
            ExcelToDataTable.Save("ExcelToDataTable.xlsx");
        }    
    }
}