在div中使用MVC show RDLC [英] Use MVC show RDLC in div

查看:84
本文介绍了在div中使用MVC show RDLC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre lang =c#> 
public class ReportsResult:ActionResult
{
public ReportsResult(byte [] data,string mineType)
{
this.Data = data;
this.MineType = mineType;
}

public byte [] Data {get;组; }
公共字符串MineType {get;组; }

public override void ExecuteResult(ControllerContext context)
{
if(Data == null)
{
new EmptyResult()。ExecuteResult(context );
返回;
}
context.HttpContext.Response.ContentType = MineType;

使用(MemoryStream ms = new MemoryStream(Data))
{
ms.Position = 0;
using(StreamReader sr = new StreamReader(ms))
{
context.HttpContext.Response.Output.Write(sr.ReadToEnd());
}
}
}
}



  public   static   class  HotelReport 
{
public static byte [] GenerateReport( string dtatSetName,IEnumerable dataSource, string reportFilePath,
string reportType, out string mimeType, out 字符串编码, out 字符串 fileNameExtension )
{

ReportDataSource reportDataSource = new ReportDataSource(dtatSetName,dataSource);

LocalReport localReport = new LocalReport();
localReport.ReportPath = reportFilePath;
localReport.DataSources.Add(reportDataSource);

string deviceInfo =
< DeviceInfo> +
< OutputFormat> + reportType + < / OutputFormat> +
< PageWidth> 8.5in< / PageWidth> +
< PageHeight> 11in< / PageHeight> +
< span class =code-string>< MarginTop> 0.5in< / MarginTop>
+
< MarginLeft> 1in< / MarginLeft> +
< MarginRight> 1in< / MarginRight> +
< MarginBottom> 0.5in< / MarginBottom> +
< / DeviceInfo>中;

警告[]警告;
string [] streams;
byte [] renderedBytes;

renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out 编码,
out fileNameExtension,
输出流,
输出警告);

return renderedBytes;
}
}



  public  ActionResult EmployeesNumberPerYear()
{
string dtatSetName = DsENPerYear;
var dataSource =
EmployeeReports.EmployeesNumberPerYear(employeeRepository);
string reportFilePath =
Server.MapPath( 〜/ RDLC /雇员/ EmployeesNumberPerYear.rdlc);
string reportType = PDF;

string mimeType;
string 编码;
string fileNameExtension;

byte [] renderedBytes =
HotelReport.GenerateReport(dtatSetName,dataSource,reportFilePath,
reportType, out mimeType, out 编码, out fileNameExtension);

return new ReportsResult(renderedBytes,mimeType);
}



< pre lang =HTML> 
< div>
@ {
Html.RenderAction(EmployeesNumberPerYear,EmployeeReports);
}
< / div>

解决方案

如果EmployeeReports是您的控制器,那么我会创建一个像这样的新方法:



  public   void  EmployeesNumberPerYearToPDF()
{
string dtatSetName = DsENPerYear;
var dataSource =
EmployeeReports.EmployeesNumberPerYear(employeeRepository);
string reportFilePath =
Server.MapPath( 〜/ RDLC /雇员/ EmployeesNumberPerYear.rdlc);
string reportType = PDF;

string mimeType;
string 编码;
string fileNameExtension;

byte [] renderedBytes =
HotelReport.GenerateReport(dtatSetName,dataSource,reportFilePath,
reportType, out mimeType, out 编码, out fileNameExtension);

System.Web.HttpContext.Current.Response.ContentType = mimeType;
System.Web.HttpContext.Current.Response.BinaryWrite(renderedBytes);

}





然后你可以在你的div中嵌入Google Doc Viewer(iframe)并指定PDF您要显示的文件,即在您的情况下将URL传递给控制器​​,该控制器将返回PDF文件的字节数组。这是您应该在div中添加的代码:



 <   iframe     src   =  http://docs.google.com/gview?url=@Url.Action(\"<  EmployeesNumberPerYearToPDF,   EmployeeReports,    null,   请求。 Url.Scheme)& embedded   =   true   样式  = 宽度:600px ; height:500px;    frameborder   =  0 >  <   / iframe  >  


<pre lang="c#">
public class ReportsResult : ActionResult
    {
        public ReportsResult(byte[] data, string mineType)
        {
            this.Data = data;
            this.MineType = mineType;
        }

        public byte[] Data { get; set; }
        public string MineType { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (Data == null)
            {
                new EmptyResult().ExecuteResult(context);
                return;
            }
            context.HttpContext.Response.ContentType = MineType;

            using (MemoryStream ms = new MemoryStream(Data))
            {
                ms.Position = 0;
                using (StreamReader sr = new StreamReader(ms))
                {
                    context.HttpContext.Response.Output.Write(sr.ReadToEnd());
                }
            }
        }
    }


public static class HotelReport
   {
       public static byte[] GenerateReport(string dtatSetName, IEnumerable dataSource, string reportFilePath,
                              string reportType, out string mimeType, out string encoding, out string fileNameExtension)
       {

           ReportDataSource reportDataSource = new ReportDataSource(dtatSetName, dataSource);

           LocalReport localReport = new LocalReport();
           localReport.ReportPath = reportFilePath;
           localReport.DataSources.Add(reportDataSource);

           string deviceInfo =
               "<DeviceInfo>" +
               "  <OutputFormat>" + reportType + "</OutputFormat>" +
               "  <PageWidth>8.5in</PageWidth>" +
               "  <PageHeight>11in</PageHeight>" +
               "  <MarginTop>0.5in</MarginTop>" +
               "  <MarginLeft>1in</MarginLeft>" +
               "  <MarginRight>1in</MarginRight>" +
               "  <MarginBottom>0.5in</MarginBottom>" +
               "</DeviceInfo>";

           Warning[] warnings;
           string[] streams;
           byte[] renderedBytes;

           renderedBytes = localReport.Render(
               reportType,
               deviceInfo,
               out mimeType,
               out encoding,
               out fileNameExtension,
               out streams,
               out warnings);

           return renderedBytes;
       }
   }


public ActionResult EmployeesNumberPerYear()
        {
         string dtatSetName = "DsENPerYear";
         var dataSource = 
                 EmployeeReports.EmployeesNumberPerYear(employeeRepository);
         string reportFilePath = 
                 Server.MapPath("~/RDLC/Employee/EmployeesNumberPerYear.rdlc");
         string reportType = "PDF";

         string mimeType;
         string encoding;
         string fileNameExtension;

         byte[] renderedBytes = 
            HotelReport.GenerateReport(dtatSetName, dataSource, reportFilePath,
                reportType, out mimeType, out encoding, out fileNameExtension);

        return new ReportsResult(renderedBytes, mimeType);
        }


<pre lang="HTML">
<div>
    @{
        Html.RenderAction("EmployeesNumberPerYear", "EmployeeReports");
    }
</div>

解决方案

If EmployeeReports is your controller, then I'd create a new method like this:

public void EmployeesNumberPerYearToPDF()
        {
         string dtatSetName = "DsENPerYear";
         var dataSource = 
                 EmployeeReports.EmployeesNumberPerYear(employeeRepository);
         string reportFilePath = 
                 Server.MapPath("~/RDLC/Employee/EmployeesNumberPerYear.rdlc");
         string reportType = "PDF";
 
         string mimeType;
         string encoding;
         string fileNameExtension;
 
         byte[] renderedBytes = 
            HotelReport.GenerateReport(dtatSetName, dataSource, reportFilePath,
                reportType, out mimeType, out encoding, out fileNameExtension);
 
        System.Web.HttpContext.Current.Response.ContentType = mimeType;
        System.Web.HttpContext.Current.Response.BinaryWrite(renderedBytes);

        }



Then you could embed Google Doc Viewer (an iframe) in your div and specify the PDF file you want to display i.e. in your case pass the URL to your controller that will return the byte array of the PDF file. This is the code you should add inside your div:

<iframe src="http://docs.google.com/gview?url=@Url.Action("EmployeesNumberPerYearToPDF", "EmployeeReports", null, Request.Url.Scheme)&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>


这篇关于在div中使用MVC show RDLC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆