Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to get data from database, write on excel and download file. I use the code given below and file cannot be download while data show in alert.

What I have tried:

Controller Code is given below
C#
        [HttpPost]
        public ActionResult ShipExportToExcel(string[] ShipID)
        {
            var products = new System.Data.DataTable("Person");
            products.Columns.Add("Name ", typeof(string));
            products.Columns.Add("Address", typeof(string));
            products.Columns.Add("Mobile Number", typeof(string));
           
            foreach (var i in ShipID)
            {
                int ID = Convert.ToInt32(i);
                Person obj_Person = new Person();
                obj_Person = new BL_Person().GetPersonByID(ID );
                if (obj_Person!= null)
                {
                    products.Rows.Add(obj_Person .Name, obj_Person.ShipAddress, obj_Person .Cell);
                }
            }
            var grid = new GridView();
            grid.DataSource = products;
            grid.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=ExportByExcel.xls");
            //Response.ContentType = "application/ms-excel";
            Response.ContentType = "application/vnd.ms-excel";

            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
           
            return View();
            
        }

view code.........................

 $('#btnSave').click(function () {
            var rowCount = $('Table >tbody >tr').length;
            
            if (rowCount > 0) {
            
                var data = {
                    ShipID: shipmentItems
                }
                $.ajax({
                    url: "@Url.Action("ShipExportToExcel", "ExportToExcel", new { area = "Person" })",
                    type: "POST",
                    data: JSON.stringify(data),
                    contentType: "application/json; charset=utf-8",
                    success: function (d) {
                        msgSuccess(d);                 
                    },
                    error: function (request, error) {
                        msgAlerts(error);                    
                    }
                    });
                   
            } else {
                msgAlerts("Please Enter the Record First");
            }
Posted
Updated 29-Aug-19 6:40am
v2

1 solution

UnComment this line
Response.ContentType = "application/ms-excel


Can you do quick watch here,
grid.DataSource = products;
 
Share this answer
 
Comments
Member 10028394 21-Dec-17 22:46pm    
I do UnComment Response.ContentType = "application/ms-excel
but file could not download
Can u told me that i write code to download file in jquery on success if yes then what?
[no name] 22-Dec-17 2:32am    
http://geekswithblogs.net/rgupta/archive/2014/06/23/downloading-file-using-ajax-and-jquery-after-submitting-form-data.aspx


Refer to this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900