Mar 30, 2016

Using C# to export Reporting Services report to Excel

Add Reference: Microsoft.ReportViewer.WebForms

Using Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution;

ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = "http://servername/reportserver/ReportExecution2005.asmx";
 
// Render arguments
byte[] result = null;
string reportPath = "/folder/reportname";
string format = "EXCEL";
string historyID = null;
string devInfo = @"False";
 
// Prepare report parameter.
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "Date";
parameters[0].Value = "2012-05-07";
 
DataSourceCredentials[] credentials = null;
string showHideToggle = null;
string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
ParameterValue[] reportHistoryParameters = null;
string[] streamIDs = null;
 
ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();
 
rs.ExecutionHeaderValue = execHeader;
 
execInfo = rs.LoadReport(reportPath, historyID);
 
rs.SetExecutionParameters(parameters, "en-us");
String SessionId = rs.ExecutionHeaderValue.ExecutionID; Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID); try {     result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);     execInfo = rs.GetExecutionInfo();     Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime); } catch (SoapException e) {     Console.WriteLine(e.Detail.OuterXml); } // Write the contents of the report to an MHTML file. try {     FileStream stream = File.Create("report.xls", result.Length);     Console.WriteLine("File created.");     stream.Write(result, 0, result.Length);     Console.WriteLine("Result written to the file.");     stream.Close(); } catch (Exception e) {     Console.WriteLine(e.Message); }
 public void ExportToFile(string ext)
        {
            if (string.IsNullOrEmpty(ext))
            {
                return;
            }
            string format = string.Empty;
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            switch (ext)
            {
                case "excel":
                    saveFileDialog.Filter = "Excel files (*.xls)|*.xls";
                    format = "Excel";
                    break;
                case "doc":
                    saveFileDialog.Filter = "Doc files (*.doc)|*.doc";
                    format = "Doc";
                    break;
                case "pdf":
                    saveFileDialog.Filter = "*PDF files (*.pdf)|*.pdf";
                    format = "PDF";
                    break;
            }
            if (!string.IsNullOrEmpty(format))
            {
                // Variables
                Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string extension;
                string path = " ";
                DialogResult dr = saveFileDialog.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    path = saveFileDialog.FileName;
                    byte[] bytes = reportViewer1.LocalReport.Render(format, null, out   mimeType, out encoding, out extension, out streamids, out warnings);
                    FileStream fs = new FileStream(path, FileMode.Create);
                    fs.Write(bytes, 0, bytes.Length);
                    fs.Close();
                    MessageBox.Show("Exported!");
                }
            }
        }

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib