How to programmatically run and export to HTML a ReportViewer

3.8k views Asked by At

This question has two parts. Part 1: How can I programmatically run a reportviewer and part 2: How to programmatically export to html a reportviewer.

Part 1: the .rdl is hosted on a remote server and I need to run this report as if the user had clicked "view results"but in a programmatic way. I'm giving it the right report url, path and parameters, but I seems to be missing something cause when I export it, the results are 0 which means it didn't run. I'm pretty sure I'm missing something but I don't know what it is.

Part 2: I'm running on VS12. I had read you couldn't export to HTML in VS10, but in VS12 you can, but it comes out all wrong. This report has a few tables with color padded cells. The cells aren't colored even though the data is align in a table format. I got it working on a pdf format, but data is null cause of part 1. I'd would prefer it to be on an html format cause I plan to place this export on an email's body.

This is what I got so far:

report.ServerReport.ReportPath = reportPath;

//Assign parameters
var parameters = new List<ReportParameter>();

            parameters.Add(new ReportParameter("Param1", param1));

            parameters.Add(new ReportParameter("Param2", param2));

            //Execute report
            report.ServerReport.SetParameters(parameters.ToArray());

            string mimeType,
                encoding,
                fileNameExtension;

            Warning[] warnings;

            string[] streamIds;

            //Export report
            byte[] exportBytes = report.ServerReport.Render("HTML4.0", null, out mimeType, out encoding,
                out fileNameExtension, out streamIds, out warnings);

             //For pdf format
             //HttpContext.Current.Response.Buffer = true;
             //HttpContext.Current.Response.Clear();
             //HttpContext.Current.Response.ContentType = mimeType;
             //HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportedReport." + fileNameExtension);
             //HttpContext.Current.Response.BinaryWrite(exportBytes);
             //HttpContext.Current.Response.Flush();
             //HttpContext.Current.Response.End();

            var exportHtml = System.Text.Encoding.UTF8.GetString(exportBytes);

If it's not to much to ask, please add some explanation to your answer. I'm still new to reporting and I would like to understand why it's not working.

1

There are 1 answers

0
José Corretjer-Gómez On

I managed to solve it. My code was correct, even though I was passing the required parameters, the report, itself, wasn't really using the parameter.