Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi team

I really need some help, i have a logic to download report, but i dont want my file extension be PDF. Rather as Microsoft Excel or Word, i need some guidance based on a given logic below;

What I have tried:

C#
<pre> //GET/Download_Report

        public ActionResult Download_ExcelReport()
        {
            //var _db = new eNtsaRegistration();

            var data = (from q in db.eNtsaRegForms select new { 
            Id = q.Id,
            FirstName = q.FirstName
            
            }).ToList();

            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("~/Reports/uYiloReporting.rpt"));
            rpt.SetDataSource(data);


            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            try
            {
                Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                stream.Seek(0, SeekOrigin.Begin);
                return File(stream, "application/excel", "eNtsaRegistrationForm.excel");
            }
            catch
            {
                throw;
                //return View();
            }

           
        }
Posted
Updated 23-Jul-20 7:05am

1 solution

Changing the format of a file is not simply a case of specifying a different content type or file extension. The content of a PDF file cannot be interpreted as an Excel file.

You need to specify the correct ExportFormatType for the file type you're trying to export. You'll also need to specify the correct content type and file extension.

For example:
C#
Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/vnd.ms-excel", "eNtsaRegistrationForm.xls");
ExportFormatType Enumeration (CrystalDecisions.Shared) | Microsoft Docs[^]
 
Share this answer
 
Comments
gcogco10 23-Jul-20 14:00pm    
Thanks so much Richard.

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