Import Data from Excel to Datatable in web servers, it's no need to install Microsoft Office software.

XsExcel SDK provides methods to import data from other data source, like DataTable. The whole datas from data table are converting and inserting to a new worksheet with table name. All the data type will keey the original data type in data table.

This guide page provides C# users with how to import data from Data Table to XLSX and XLS spreadsheet with XsExcel SDK in C#.NET.


public static void ImportDataTableToSheet()
{
    string filename = "DataTableSample.xlsx";
    // Get a data table with specified data
    DataTable dt = createDataTable();

    // Create a new excel workbook
    Workbook workbook = new Workbook();
    // Import data from data table to worksheet
    workbook.ConvertDataTableToSheet(dt);

    // Save and show the spreedsheet
    if (File.Exists(filename)) File.Delete(filename);  
    workbook.Save(filename);
    Process.Start(filename);
}

private static DataTable createDataTable()
{
    DataTable dt = new DataTable("SampleTable1");
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Number", typeof(int));
            
    for (int i = 0; i < 100; i++)
    {
        DataRow dr = dt.NewRow();
        dr[0] = "name-" + i.ToString();
        dr[1] = i;
        dt.Rows.Add(dr);
    }

    return dt;
}

Notice - If you used the trial version of XsExcel SDK, can only edit, insert or get the first 300 row data!

More Excel tutorial