Articles in this section
Category / Section

How to import data to table in Excel document using C#, VB.NET?

3 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It provides support for importing data into Excel and exporting data from Excel. XlsIO handles various data formats and structures, enabling users to integrate Excel with different data sources and utilize Excel’s capabilities for data manipulation and analysis.

This article explains how to import data to table in Excel document using C# and VB.NET.

Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

VB.NET

Imports Syncfusion.XlsIO

Use the following code snippet for importing data to table in Excel document.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    IWorkbook workbook = application.Workbooks.Create(1);
    IWorksheet worksheet = workbook.Worksheets[0];

    //Sample data
    object[,] data = new object[,]
    {
        { "ID", "Name", "Category", "Price" },
        { 1, "Apple", "Fruit", 0.99 },
        { 2, "Carrot", "Vegetable", 0.49 },
        { 3, "Milk", "Dairy", 1.49 }
    };

    //Import data to worksheet
    worksheet.ImportArray(data, 1, 1);

    //Calculate range from data size
    int rowCount = data.GetLength(0);
    int colCount = data.GetLength(1);

    IRange dataRange = worksheet.Range[1, 1, rowCount, colCount];

    //Create a table (ListObject)
    IListObject table = worksheet.ListObjects.Create("SalesTable", dataRange);

    //Apply built-in table style
    table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium9;

    //Auto-fit columns
    worksheet.UsedRange.AutofitColumns();

    #region Save
    //Saving the workbook
    FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
    workbook.SaveAs(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();
}

VB.NET

Using excelEngine As New ExcelEngine()
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Xlsx
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
    Dim worksheet As IWorksheet = workbook.Worksheets(0)

    'Sample data
    Dim data(,) As Object = {
        {"ID", "Name", "Category", "Price"},
        {1, "Apple", "Fruit", 0.99},
        {2, "Carrot", "Vegetable", 0.49},
        {3, "Milk", "Dairy", 1.49}
    }

    'Import data to worksheet
    worksheet.ImportArray(data, 1, 1)

    'Calculate range from data size
    Dim rowCount As Integer = data.GetLength(0)
    Dim colCount As Integer = data.GetLength(1)

    Dim dataRange As IRange = worksheet.Range(1, 1, rowCount, colCount)

    'Create a table (ListObject)
    Dim table As IListObject = worksheet.ListObjects.Create("SalesTable", dataRange)

    'Apply built-in table style
    table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium9

    'Auto-fit columns
    worksheet.UsedRange.AutofitColumns()

    'Saving the workbook
    workbook.SaveAs("TableImport.xlsx")
End Using  

You can get the complete sample for importing data to table in Excel document from here.

I hope you enjoyed learning how to import data to table in Excel document using C# and VB.NET in XlsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied