Partilhar via


Using the API of Reporting Services to render reports to Excel or PDF in a multi language environment.

 

You might find yourself in a situation in which you want to use the API of Reporting Services to render reports to Excel or PDF in a multi language environment. If that is the case, you will want to display the date and number formats depending where the user is located.

Normally, this will make the WebService aware that we have a client app (for example in French).

In the sample, we override the GetWebRequest method so we can add a header with a “accept language” set to “fr”.

To be able to override this method, we need to create a class that inherits from the ReportExecutionService class (RS proxy object generated by VS).

So the code looks like this:

I have a Webreference to my ReportExecutionService (called ReportExec).

Here is the class I created and the override I did :

==

public class RSWebServiceExec : ReportExec.ReportExecutionService

        {

            protected override WebRequest GetWebRequest(Uri uri)

            {

                WebRequest wr = base.GetWebRequest(uri);

                WebHeaderCollection headers = ((HttpWebRequest)wr).Headers;

                headers.Add("Accept-Language:fr");

                return wr;

            }

        }

==

Then we use this object in my reportexecution code to render the report:

==

            RSWebServiceExec rs = new RSWebServiceExec();

            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

            rs.Url = "https://localhost/reportserver/ReportExecution2005.asmx";

            // Render arguments

            byte[] result = null;

            string reportPath = "/AdventureWorks Sample Reports/Employee Sales Summary";

            string format = "MHTML";

            string historyID = null;

            string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

            // Prepare report parameter.

            ParameterValue[] parameters = new ParameterValue[3];

            parameters[0] = new ParameterValue();

            parameters[0].Name = "EmpID";

            parameters[0].Value = "288";

            parameters[1] = new ParameterValue();

            parameters[1].Name = "ReportMonth";

            parameters[1].Value = "6"; // June

            parameters[2] = new ParameterValue();

            parameters[2].Name = "ReportYear";

            parameters[2].Value = "2004";

            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.mht", 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);

            }

==

We did this in a console application but this can be done in an ASP.NET app.

For Spanish, you have to put “es” instead of “fr” as the “accept Language” for you and so on.

You can also get the language dynamically from ASP.NET using :

 

request.Headers.Add("Accept-Language", HttpContext.Current.Request.Headers["Accept-Language"] );

 

You may also want to change the parameters langague (highlighted in orange).

 


Maria Esteban

Reporting Services Support Engineer

Comments