none
exporting multiple datatables to respective worksheet in excel RRS feed

  • Question

  • Hi All,

    Need all your help.

    Tell me how to export data from multiple datatables to respective worksheet in excel. in c#

    ex:

    Address worksheet;AddressDatatable data has to go for this sheet only

    DateofBirth:DOBDatatable has to go for this sheet only.

    like this there are 18worksheets.

    Thanks,

    Shankar






    • Edited by Shankarbs Monday, July 16, 2018 2:22 AM
    Monday, July 16, 2018 1:30 AM

All replies

  • If working with .xlsx then SpreadSheetLight works great. Select an existing sheet or create a sheet, use the method ImportDataTable to import a DataTable.

    See my MSDN code sample for the above. Install via NuGet.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 16, 2018 9:55 AM
    Moderator
  • Hi Shankar,

    You can achieve this using Spire.Xls.dll available on NuGet. The following code for your reference:

    Workbook workbook = new Workbook();
    //Specify excel version (the default version is 2003)
    workbook.Version = ExcelVersion.Version2013;
    
    //Load file
    workbook.LoadFromFile("Input.xlsx");
    
    //get the first sheet
    Worksheet sheet = workbook.Worksheets[0]; //you can also use sheet name
    
    //export datatable to the sheet 
    sheet.InsertDataTable(datatable, true, 1, 1); //1 means the start row number and column number
    
    //Save
    workbook.SaveToFile("DataTableToExcel.xlsx", ExcelVersion.Version2013);
    Hope that helps.

    Tuesday, July 17, 2018 7:55 AM
  • Hi Shankarbs,

    The following document provide a method which use 3rd-party package named closedXML to Export Multiple Data Tables to Multiple Worksheets Inside a Single Excel File for your reference.


    https://www.c-sharpcorner.com/UploadFile/rahul4_saxena/how-to-export-multiple-data-tables-to-multiple-worksheets-in/

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 17, 2018 8:17 AM
    Moderator