Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:38:15+00:00 2026-06-16T10:38:15+00:00

Hi i have created export class that converts Gridview to excel file. See below

  • 0

Hi i have created export class that converts Gridview to excel file.

See below code:

DownloadFileActionResult Class:

public class DownloadFileActionResult : ActionResult
    {


        public GridView ExcelGridView { get; set; }
        public string fileName { get; set; }


        public DownloadFileActionResult(GridView gv, string pFileName)
        {

            ExcelGridView = gv;

            fileName = pFileName;

        }



        public override void ExecuteResult(ControllerContext context)
        {

            HttpContext curContext = HttpContext.Current;
            curContext.Response.ClearContent();
            curContext.Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            ExcelGridView.RenderControl(htw);
            curContext.Response.Write(sw.ToString());
            curContext.Response.End();

        }

    }

Jquery-ajax:

function Export(){


    var search = {};                                        
    search.Name = "MaterialShape";
    search.Description = "";
    search.Address ="";

    var url_ = generateURL("/Home/Download");                    //Call Save Controller  and pass details entities  

    $.ajax({
        type: "POST",
        url: url_,
        data: search,                                            //details will act as the Entities Model
        traditional: true,
        success: function(data) {


        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("error: " + XMLHttpRequest.responseText);
        },
        dataType: 'json'
    });

};

Search Parmeter Properties:

public class SearchParams
    {
        public string Name{ get; set; }
        public string Description {get;set;}
        public string Address{get;set;}
        ...
     }

And then i implement it on my controller:

     //Export to excel
    public ActionResult Download(SearchParam param)
    {

       List<Lookup> lookupList = data.GetLookup(param);
      var grid = new System.Web.UI.WebControls.GridView();

       grid.DataSource = lookupList;
       grid.DataBind();

       return new DownloadFileActionResult(grid, "test");

    }

It is working(without search param values) when i type the controller url manually

http://localhost:54928/Home/Download  

or using html.action link

<%= Html.ActionLink("Home", "/Download", "Home")%> 

but it is not working when i use ajax call

<img src="<%=Url.Content("~/Images/export.png")%>" id="Img1" onclick="Export();" alt="Export" /> 

that i really need to use.

I am missing something here..any ideas?

Thanks in Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:38:16+00:00Added an answer on June 16, 2026 at 10:38 am

    It does not make sense to use $.ajax to download excel file – the ajax call is really meant to get text-based data (html. xml, JSON) in your js code so that you can work with it. Browser knows how to handle (e.g. prompt for saving the file) the binary content such as excel.

    In this case, all you need to is a simple POST/GET request to initiate the excel file download (as simple as document.location = "/home/download?q=keyword";)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a roxygen file for a function that uses S3 an class.
I have a class User. I have created a generic CSV exporter that works
so I have a project that only has one class Main: public class Main{
I have created an application which converts Excel files to Access databases. During conversion
I have one SQL server database which was created using Import/Export Wizard from an
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based
I have a pretty big web application that I created last year using ASP.NET
So, I have this code that grabs a bunch of data from the database,
I have created a MEF plugin control that I import into my app. Now,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.