none
Change report name after exporting in pdf/excel/word RRS feed

  • Question

  • Hello everyone,

    I am developing an asp.net mvc app which insert delete (etc...) information into a database, after the insert statement the user can decide to print a report with the pc information ( i am using report viewer, so .rdlc filename extension), everything is working but i didn't find anything working for this purpose.

    Below i attach my c# code for exporting and downloading the report

    public ActionResult Report(string id, string hostName, int idPc)
            {
                
                LocalReport lr = new LocalReport();
                lr.DisplayName = ""+hostName+"-"+DateTime.Today.ToString("yyyy-MM-dd")+"";
                string path = Path.Combine(Server.MapPath("~/reportDev.rdlc"));
                if (System.IO.File.Exists(path))
                {
                    lr.ReportPath = path;
                }
                else
                {
                    return RedirectToAction("Index");
                }
                //selection of the elements which i need
                //the elements come from the entity framework model
                var query = from c in db.Pc
                            where c.ID == idPc
                            select new
                            {
                                c.hostname,
                                c.pc_language,
                                c.last_update,
                                c.Offices.version_office,
                                c.Operating_systems.name_os,
                                c.processor,
                                c.ram,
                                c.Builders.builder_name,
                                c.antivirus,
                                c.ssd,
                                c.general_conditions,
                                c.notes
                            };
                
                DataTable dt = new DataTable();
                //create the column for my dataTable
                //----------------------------//
                DataColumn hostname = new DataColumn("hostname");
                hostname.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(hostname);
                //---------------------------//
                DataColumn pc_language = new DataColumn("pc_language");
                pc_language.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(pc_language);
                //--------------------------//            
                DataColumn last_update = new DataColumn("last_update");
                last_update.DataType = System.Type.GetType("System.DateTime");
                dt.Columns.Add(last_update);
                //-------------------------//
                DataColumn officeID = new DataColumn("officeID");
                officeID.DataType = System.Type.GetType("System.Int32");
                dt.Columns.Add(officeID);
                //--------------------------//
                DataColumn op_sysID = new DataColumn("op_sysID");
                op_sysID.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(op_sysID);
                //--------------------------//
                DataColumn processor = new DataColumn("processor");
                processor.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(processor);
                //-------------------------//
                DataColumn ram = new DataColumn("ram");
                ram.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(ram);
                //-------------------------//
                DataColumn builderID = new DataColumn("builderID");
                builderID.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(builderID);
                //-------------------------//
                DataColumn antivirus = new DataColumn("antivirus");
                antivirus.DataType = System.Type.GetType("System.Boolean");
                dt.Columns.Add(antivirus);
                //-------------------------//
                DataColumn ssd = new DataColumn("ssd");
                ssd.DataType = System.Type.GetType("System.Boolean");
                dt.Columns.Add(ssd);
                //-------------------------//
                DataColumn general_conditions = new DataColumn("general_conditions");
                general_conditions.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(general_conditions);
                //-------------------------//
                DataColumn notes = new DataColumn("notes");
                notes.DataType = System.Type.GetType("System.String");
                dt.Columns.Add(notes);
                //get the info from the precedent query
                //and store the info into the rows
                foreach (var x in query)
                {
                    DataRow dr = dt.NewRow();
                    dr["hostname"] = x.hostname.ToUpper();
                    dr["pc_language"] = x.pc_language;
                    dr["last_update"] = x.last_update;
                    dr["officeID"] = x.version_office;
                    dr["op_sysID"] = x.name_os;
                    dr["processor"] = x.processor;
                    dr["ram"] = x.ram;
                    dr["builderID"] = x.builder_name;
                    dr["antivirus"] = x.antivirus;
                    dr["ssd"] = x.ssd;
                    dr["general_conditions"] = x.general_conditions;
                    dr["notes"] = x.notes;
                    dt.Rows.Add(dr);
                }
    
                //the data table becomes the data source of my report
                ReportDataSource dataSource = new ReportDataSource("dataSet_reportDev", dt);
                lr.DataSources.Add(dataSource);
                //variables initialization
                //for exporting in the various format
                string reportType = id;
                string mimeType;
                string encoding;
                string fileNameExtension;
                //----------------------------//
                Warning[] warnings;
                string[] streams;
                byte[] renderedBytes;
    
                renderedBytes = lr.Render(reportType, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
                return File(renderedBytes, mimeType);
            }

    Best regards

    Leo

    Wednesday, February 6, 2019 2:09 PM

Answers

  • This is really an ASP.NET question as you're trying to rename the file as seen by the browser. Please post future questions related to ASP.NET in the ASP.NET forums.

    However for this question you simply need to set the content filename and the browser will show that.

    return File(renderedBytes, mimeType, "MyReport.pdf");


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, February 6, 2019 2:59 PM
    Moderator