3/09/2011

c# code to export local ssrs report to pdf


I got the Visual Studio 2008 c# app to export the Transcript Report (removed the sub-reports) to pdf.

        public void ExportReport(object sender, EventArgs e)
        {
            Microsoft.Reporting.WinForms.Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;

            // The application is responsible for collecting parameters
            ReportParameterInfoCollection parameterInfo = reportViewer1.LocalReport.GetParameters();

            string strUser = "BLOOMJON";
            string strReportType = "T";
            string strPermNum = "";
            string strPCSBSchNum = "2081";
            string strGrade = "09";
            string strStudent = "5205008373";
            string strInstrSet = "";
            string strWIP = "Y";

            _paramDescriptor = parameterInfo;

            //SetParameters();
            _parameters = new ReportParameter[8];
            _parameters[0] = new ReportParameter(_paramDescriptor[0].Name);
            _parameters[1] = new ReportParameter(_paramDescriptor[1].Name);
            _parameters[2] = new ReportParameter(_paramDescriptor[2].Name);
            _parameters[3] = new ReportParameter(_paramDescriptor[3].Name);
            _parameters[4] = new ReportParameter(_paramDescriptor[4].Name);
            _parameters[5] = new ReportParameter(_paramDescriptor[5].Name);
            _parameters[6] = new ReportParameter(_paramDescriptor[6].Name);
            _parameters[7] = new ReportParameter(_paramDescriptor[7].Name);

            _parameters[0].Values.Add(strUser.ToString()); //user
            _parameters[1].Values.Add(strReportType.ToString()); //ReportType
            _parameters[2].Values.Add(strPCSBSchNum.ToString()); //PCSBSchNum
            _parameters[3].Values.Add(strGrade.ToString()); //Grade
            _parameters[4].Values.Add(strStudent.ToString()); //Students
            _parameters[5].Values.Add(strPermNum.ToString()); //PermNum
            _parameters[6].Values.Add(strInstrSet.ToString()); //InstrSet
            _parameters[7].Values.Add(strWIP.ToString()); //WIP

            // Add report parameters
            reportViewer1.LocalReport.SetParameters(Parameters);

            try
            {
                // Add the customer data source
                this.fOCUS_SS_1800DataSet.DataSetName = "CourseHistory";
                this.sp_StuCrsHistTableAdapter = new ReportingServiceExample.FOCUS_SS_1800DataSetTableAdapters.sp_StuCrsHistTableAdapter();
                this.sp_StuCrsHistTableAdapter.Connection.Open();
                this.sp_StuCrsHistTableAdapter.Fill(this.fOCUS_SS_1800DataSet.sp_StuCrsHist, strStudent, strGrade, strPCSBSchNum);

            }
            catch (NullReferenceException ee)
            {
                System.Console.WriteLine("{0} Caught exception #1.", ee);
                Console.WriteLine(String.Concat(ee.StackTrace, ee.Message));
            }
            catch (ConstraintException c)
            {
                System.Console.WriteLine("{0} Caught exception #2.", c);
                Console.WriteLine(String.Concat(c.StackTrace, c.Message));
            }

            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("CourseHistory", this.fOCUS_SS_1800DataSet.sp_StuCrsHist));
            //this.reportViewer1.RefreshReport();

            byte[] bytes = this.reportViewer1.LocalReport.Render(
               "PDF", null, out mimeType, out encoding,
                out extension,
               out streamids, out warnings);

            FileStream fs = new FileStream(@"c:\transcript.pdf",
               FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();

        }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Thoughts to Ponder