This is useful when we don't want to show the control, but we need to get the rendered report in different formats.
Step 1: download the Report viewer runtime from here.
Step 2: Install the runtime.
Step 3: Add the reference to your project. The dll should be in \Program Files (x86)\Microsoft Visual Studio 11.0\ReportViewer\Microsoft.ReportViewer.WinForms.dll
First we create a method that will return the report from the report viewer as an array of bytes.
public byte[] RenderFromServer()
{
ReportViewer rv = new ReportViewer();
// Set the processing mode for the ReportViewer to Remote
rv.ProcessingMode = ProcessingMode.Remote;
rv.ServerReport.ReportServerUrl = new Uri("Http://mypc/ReportServer);
rv.ServerReport.DisplayName = "New report.pdf";
rv.ServerReport.ReportPath = "/Reports/MyReport";
//set credentials
ReportServerCredentials rsCredentials = rv.ServerReport.ReportServerCredentials;
NetworkCredential nc = new NetworkCredential();
nc.Domain = "myDomain"; //can also be the computer name for local access
nc.UserName = "myUser";
nc.Password = "12333";
rsCredentials.NetworkCredentials = nc;
//set parameters
List<ReportParameter> paramLst = new List<ReportParameter>();
ReportParameter rp = new ReportParameter(ParamName, ParamValue);
paramLst.Add(rp);
rv.ServerReport.SetParameters(paramLst);
//get the bytes from the report viewer
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rv.ServerReport.Render
("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
rv.Dispose(); //very important for memory management!!
return bytes;
}
Notice that I called the Render function with the "PDF" paramater. The full list can be found at http://msdn.microsoft.com/en-us/library/ms154606.aspx.{
ReportViewer rv = new ReportViewer();
// Set the processing mode for the ReportViewer to Remote
rv.ProcessingMode = ProcessingMode.Remote;
rv.ServerReport.ReportServerUrl = new Uri("Http://mypc/ReportServer);
rv.ServerReport.DisplayName = "New report.pdf";
rv.ServerReport.ReportPath = "/Reports/MyReport";
//set credentials
ReportServerCredentials rsCredentials = rv.ServerReport.ReportServerCredentials;
NetworkCredential nc = new NetworkCredential();
nc.Domain = "myDomain"; //can also be the computer name for local access
nc.UserName = "myUser";
nc.Password = "12333";
rsCredentials.NetworkCredentials = nc;
//set parameters
List<ReportParameter> paramLst = new List<ReportParameter>();
ReportParameter rp = new ReportParameter(ParamName, ParamValue);
paramLst.Add(rp);
rv.ServerReport.SetParameters(paramLst);
//get the bytes from the report viewer
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = rv.ServerReport.Render
("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
rv.Dispose(); //very important for memory management!!
return bytes;
}
The possible values are:
- CSV
- IMAGE
- EXCEL
- WORD
- HTML 4.0
- MHTML
<DeviceInfo> <HTMLFragment>True</HTMLFragment> </DeviceInfo>
As stated in MSDN, "When a report is rendered as an HTML fragment, the content of the report is contained within a TABLE element without the use of an HTML or BODY element. You can use the HTML fragment to incorporate the report into an existing HTML document."
Link: http://technet.microsoft.com/en-us/library/ms155397.aspx
Then we can write the bytes to a file:
byte[] bytes = RenderFromServer();
File.WriteAllBytes("C:\\MyReport.pdf", bytes);
File.WriteAllBytes("C:\\MyReport.pdf", bytes);
Or, make a memory stream from them:
byte[] bytes = RenderFromServer();
MemoryStream ms = new MemoryStream(bytes);
ms.Seek(0, SeekOrigin.Begin);
MemoryStream ms = new MemoryStream(bytes);
ms.Seek(0, SeekOrigin.Begin);
No comments:
Post a Comment