EasyXLS

How to import data from Excel to DataSet in J#.NET

package Tutorial34;

/* -------------------------------------------------------------------
 * Tutorial 34
 * 
 * This tutorial shows how to import Excel to DataSet in J#. The data 
 * is imported from the active sheet of the Excel file (the Excel file 
 * generated in Tutorial 09).
 * ------------------------------------------------------------------- */

import System.*;
import System.IO.*;
import System.Data.*;
import EasyXLS.*;

public class Tutorial34
{
    
    public Tutorial34()
    {
    }
    
    /** @attribute System.STAThread() */
    public static void main(String[] args)
    {
        Console.WriteLine("Tutorial 34\n-----------\n");

        // Create an instance of the class that imports Excel files
        ExcelDocument workbook = new ExcelDocument();

        // Import Excel file to DataSet
        Console.WriteLine("Reading file C:\\Samples\\Tutorial09.xlsx.\n");
        DataSet ds = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\\Samples\\Tutorial09.xlsx");

        String sError = workbook.easy_getError();
        if (sError.Equals(""))
        {
            // Display imported DataSet values
            DataTable dt = ds.get_Tables().get_Item(0);
            for (int row=0; row<dt.get_Rows().get_Count(); row++)
                for (int column=0; column<dt.get_Columns().get_Count(); column++)
                    Console.WriteLine("At row " + (row + 1) + ", column " + (column + 1) +
                         " the value is '" + dt.get_Rows().get_Item(row).get_ItemArray()[column] + "'");
        }
        else
            Console.Write("\nError encountered: " + sError );
		    
        Console.Write("\nPress Enter to Exit...");

        // Dispose memory
        workbook.Dispose();

        Console.ReadLine();
    }
}

EasyXLS Excel libraries:

.NET
Excel Library for J#
full .NET version to import, export or convert Excel files
Excel Reader for J#
.NET version to read and import Excel files
Download EasyXLS™ Excel Library for J#

File formats:

MS Excel 97 - 2003
MS Excel 2007 - 2019
MS Excel 2021
Office 365
XLSXXLSMXLSBXLS
XMLHTMLCSVTXT