1. 首先定义DeviceInfo
strDevInfo = "<DeviceInfo>" +
"<Section>" + strPageIndex + "</Section>" +
"<HTMLFragment>False</HTMLFragment>" +
"<FindString>" + strFindString + "</FindString>" +
"</DeviceInfo>";
注:如果是第一次执行FindString方法,strPageIndex应该为"1",否则就是下一页(CurrentPage+1)
2. 执行Render方法
rptRefined = res.Render(strFormat, strDevInfo, out strExtension, out strMimeType, out strEncoding, out warnings, out strStreamIDs);
当执行完Render方法后,就可以得到该report总共有多少页res.GetExecutionInfo().NumPages
3. 执行FindString方法
nFoundPageIndex = res.FindString(Convert.ToInt32(strPageIndex), res.GetExecutionInfo().NumPages, strFindString);
如果找到所要查询的字符窜,FindString将返回该字符窜所在页的PageNumber,否则返回0。
4. 如果nFoundPageIndex不等于0且不等于strPageIndex,重新执行Render方法以获得所要显示的报表页面
a. 首先重新定义DeviceInfo
strPageIndex = nFoundPageIndex.ToString();
strDevInfo = "<DeviceInfo>" +
"<Section>" + strPageIndex + "</Section>" +
"<HTMLFragment>False</HTMLFragment>" +
"<FindString>" + strFindString + "</FindString>" +
"</DeviceInfo>";
b. 执行Render方法
rptRefined = res.Render(strFormat, strDevInfo, out strExtension, out strMimeType, out strEncoding, out warnings, out strStreamIDs);
5. 最后将获得报表流数据输出到浏览器端
具体代码如下:
ReportExecutionService res = new ReportExecutionService();
res.Credentials = System.Net.CredentialCache.DefaultCredentials;
res.Url = strRESUrl;
res.Timeout = System.Threading.Timeout.Infinite;
//String strDevInfo = "";
String strDevInfo = "";
String strExtension;
String strEncoding;
String strMimeType;
Warning[] warnings = null;
String[] strStreamIDs = null;
ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();
res.ExecutionHeaderValue = execHeader;
//convert xmldocument to byte[]
MemoryStream xmlStream = new MemoryStream();
xmldoc.Save(xmlStream);
Byte[] rptDefinition = xmlStream.ToArray();
//load report definition
if ("" != strExecutionID)
{
res.ExecutionHeaderValue.ExecutionID = strExecutionID;
}
else
{
execInfo = res.LoadReportDefinition(rptDefinition, out warnings);
}
if (strFormat == "HTML4.0")
{
strDevInfo = "<DeviceInfo>" +
"<Section>" + strPageIndex + "</Section>" +
"<HTMLFragment>False</HTMLFragment>" +
"<FindString>" + strFindString + "</FindString>" +
"</DeviceInfo>";
}
Byte[] rptRefined = null;
try
{
//execute the report
rptRefined = res.Render(strFormat, strDevInfo, out strExtension, out strMimeType, out strEncoding, out warnings, out strStreamIDs);
//find a string in current report
int nFoundPageIndex = 0;
if (strFormat == "HTML4.0")
{
if (strFindString != "")
{
nFoundPageIndex = res.FindString(Convert.ToInt32(strPageIndex), res.GetExecutionInfo().NumPages, strFindString);
if (nFoundPageIndex != 0 && nFoundPageIndex != Convert.ToInt32(strPageIndex))
{
strPageIndex = nFoundPageIndex.ToString();
strDevInfo = "<DeviceInfo>" +
"<Section>" + strPageIndex + "</Section>" +
"<HTMLFragment>False</HTMLFragment>" +
"<FindString>" + strFindString + "</FindString>" +
"</DeviceInfo>";
//execute the report to switch the proper page
rptRefined = res.Render(strFormat, strDevInfo, out strExtension, out strMimeType, out strEncoding, out warnings, out strStreamIDs);
}
}
}
//flush any pending response
Response.Clear();
//set the HTTP headers for a HTML response
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
switch (strFormat)
{
case "PDF": HttpContext.Current.Response.ContentType = "application/pdf"; break;
case "EXCEL": HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; break;
case "Word": HttpContext.Current.Response.ContentType = "application/msword"; break;
default: HttpContext.Current.Response.ContentType = "text/html"; break;
}
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//send the bytes array containing the report as a binary response
if (strFormat == "HTML4.0")
{
Byte[] resPageIndex = Encoding.UTF8.GetBytes("<script language=/"javascript/">var rptPageIndex=" + strPageIndex + ";");
Byte[] resPageCount = Encoding.UTF8.GetBytes("rptPageCount=" + res.GetExecutionInfo().NumPages.ToString() + ";");
Byte[] resExecutionID = Encoding.UTF8.GetBytes("rptExecutionID=/"" + execInfo.ExecutionID + "/";");
Byte[] resFoundPageIndex = Encoding.UTF8.GetBytes("rptFoundPageIndex=" + nFoundPageIndex.ToString() + ";</script>");
Byte[] resBinaryHTML = new Byte[resPageIndex.Length + resPageCount.Length + resExecutionID.Length + resFoundPageIndex.Length + rptRefined.Length];
resPageIndex.CopyTo(resBinaryHTML, 0);
resPageCount.CopyTo(resBinaryHTML, resPageIndex.Length);
resExecutionID.CopyTo(resBinaryHTML, resPageIndex.Length + resPageCount.Length);
resFoundPageIndex.CopyTo(resBinaryHTML, resPageIndex.Length + resPageCount.Length + resExecutionID.Length);
rptRefined.CopyTo(resBinaryHTML, resPageIndex.Length + resPageCount.Length + resExecutionID.Length + resFoundPageIndex.Length);
HttpContext.Current.Response.BinaryWrite(resBinaryHTML);
}
else
{
HttpContext.Current.Response.BinaryWrite(rptRefined);
}
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
if (ex.Message != "Thread was being aborted.")
{
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "text/html";
HttpContext.Current.Response.Write("<html><body><h1>Error</h1><br><br>" + ex.Message + "</body></html>");
}
}