Click here to Skip to main content
14,330,776 members
Rate this:
Please Sign up or sign in to vote.
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
        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;

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

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


            return View();

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

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

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

Can you do quick watch here,
grid.DataSource = products;
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?
Prem Kumar Rathrolla 22-Dec-17 2:32am

Refer to this

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100