You must Sign In to post a response.
  • Category: ASP.Net MVC

    Export value from datatable to excel

    I have records in data table.. How to transfer to excel ???

    This should be done n mvc..
  • #768106
    The following link will help you:

    http://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Data-Export-/Import-Export-Datatable-to-Excel-from-Database.html

  • #768107
    with the help of response object and memory stream you can easily do it. Just fill data table from database, and export it to 'EXCEL' with the help of 'ClosedXML'
    'ClosedXML' package can be download from visual studio templates
    see below snippet to know how to export it to excel

    using (XLWorkbook wb = new XLWorkbook())
    {
    wb.Worksheets.Add(dt);
    wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
    wb.Style.Font.Bold = true;

    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;filename= EmployeeReport.xlsx");

    using (MemoryStream MyMemoryStream = new MemoryStream())
    {
    wb.SaveAs(MyMemoryStream);
    MyMemoryStream.WriteTo(Response.OutputStream);
    Response.Flush();
    Response.End();
    }
    }

    following link will help you more
    http://www.dotnetcodesg.com/Article/UploadFile/2/14426/Export%20Data%20Table%20To%20Excel%20in%20ASP.NET%20MVC%204.aspx

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768114
    Hi,

    As you mentioned it has MVC.
    I will prefer you to gave a try on NuGet package tool.
    We have tool called spire.xls
    You can install the packages using the Package Manager Console with the following query.


    PM> Install-Package Spire.XLS


    And then read the documentation on NuGet sites about the spire.xls and how to proceed with the implementation. Its simple and easy to understand and work with.

    Thanks,
    Mani


Sign In to post your comments