首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在C# MVC3中使用报告查看器

如何在C# MVC3中使用报告查看器
EN

Stack Overflow用户
提问于 2013-10-31 13:09:38
回答 1查看 2.6K关注 0票数 0

我有一个生成报告的函数。它起作用了。我的问题是,我在C#中使用了MVC3,并且不能在文件中插入报告查看器。Cshtml。我正在使用Ascx尝试显示报表,但会发生以下错误:

错误执行处理程序'System.Web.Mvc.HttpHandlerUtil ServerExecuteHttpHandlerWrapper +'的子请求

当我在o@Html.Partial文件中调用relatorio.cshtml ("relatorioApontamento")时。

relatorio.cshtml

代码语言:javascript
运行
复制
@{
    ViewBag.Title = "relatorio";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="@Url.Content("~/Scripts/relatorio.js")" type="text/javascript"></script>
@using (Html.BeginForm("relatorio", "Paginas", FormMethod.Post, new { @id = "frmParametroConfigPath" }))
{
   <div id="relatorio">
        <h1 class="titulo">Relatório</h1>
        <div class="boxRecurso">
            <label id="lbl_recurso">Recurso:</label><br />
            <select ID="ddl_nm_recurso" class="txtRecurso"></select>
        </div>
        <div class="boxDataInicial">
            <label id="lbl_data_inicial">Data Inicial:</label><br />
            <input type="text" id="datepicker_ida" />
        </div>
        <div class="boxDataFinal">
            <label id="lbl_data_final">Data Final:</label><br />
            <input type="text" id="datepicker_volta" />
        </div>
        <div id="box_btnGerar">
            <input type="button" ID="btnGerar" class="botao" value="Gerar" />
        </div>
    </div>
    @Html.Partial("relatorioApontamento");

relatorioApontamento.ascx

代码语言:javascript
运行
复制
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="relatorioApontamento.ascx.cs" Inherits="ControleBU.Views.Paginas.relatorioApontamento" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
     <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
<rsweb:ReportViewer ID="rv" runat="server" Height="679px" Width="1300px">
</rsweb:ReportViewer>

relatorioApontamento.ascx.cs

代码语言:javascript
运行
复制
namespace ControleBU.Views.Paginas
{
    public partial class relatorioApontamento : System.Web.Mvc.ViewUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["item"] != null)
            {
                rv.LocalReport.ReportPath = "Reports\\Report1.rdlc";
                rv.LocalReport.DataSources.Add((ReportDataSource)Session["item"]);
                rv.LocalReport.SetParameters(new ReportParameter("TotalHoras", Convert.ToInt32(Math.Truncate(((TimeSpan)Session["TotalHoras"]).TotalHours)).ToString() + ":" + ((TimeSpan)Session["TotalHoras"]).Minutes.ToString()));
                rv.LocalReport.SetParameters(new ReportParameter("Ida", Convert.ToString(Session["DataInicio"])));
                rv.LocalReport.SetParameters(new ReportParameter("Volta", Convert.ToString(Session["DataFim"])));
                rv.LocalReport.Refresh();
            }

        }
    }
}

Paginas控制器函数

代码语言:javascript
运行
复制
public int gerarRelatorioRelatorio(DateTime datepicker_ida, DateTime datepicker_volta, string ddl_nm_recurso)
        {
            try
            {
                ProjectBoxDAL dalProjectBox = new ProjectBoxDAL();
                Softbox.DashBordGBU.Reports.dtsReportRecurso dt = new Softbox.DashBordGBU.Reports.dtsReportRecurso();

                BUProjetosDAL dalBuProjetos = new BUProjetosDAL();

                int codRecurso = Convert.ToInt32(ddl_nm_recurso);
                int codCliente = dalBuProjetos.retornaCodigoClienteRecurso(codRecurso);

                IDataReader dr = dalProjectBox.relatorioRecurso(codCliente, datepicker_ida, datepicker_volta, codRecurso);

                dt.Tables[0].Load(dr);

                if (dt.Tables[0].Rows.Count > 0)
                {
                    var total = dt.ReportRecurso.AsEnumerable().Sum(x => x.horas_ms);

                    TimeSpan totalHoras = TimeSpan.FromMilliseconds(total);

                    Microsoft.Reporting.WebForms.ReportDataSource item = new Microsoft.Reporting.WebForms.ReportDataSource();
                    item.Value = dt.Tables[0];
                    item.Name = "ReportRecurso";

                    ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
                    Session["DataInicio"] = datepicker_ida;
                    Session["DataFim"] = datepicker_volta;
                    Session["ddl"] = ddl_nm_recurso;
                    Session["TotalHoras"] = totalHoras;
                    Session["Item"] = item;

                    return 1;
                }
                else
                    return 2;

            }
            catch (Exception)
            {
                return 0;
            }
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-31 13:30:20

您不需要新视图或部分视图来显示报表:)只需执行以下操作:在Paginas控制器的末尾添加新方法调用print(),并在print方法中定义报表,并将其打印为pdf格式,如下所示:

代码语言:javascript
运行
复制
     public void Print()
                {
        LocalReport localReport = new LocalReport();
        localReport.ReportPath = @"report full path [Reports/myreport.rdlc]";
//if you have parameters set your parameters here
           Warning[] warnings;
                    string[] streamids;
                    string mimeType;
                    string encoding;
                    string filenameExtension;

                    byte[] rebytes = localReport.Render(
        "PDF", null, out mimeType, out encoding, out filenameExtension,
        out streamids, out warnings);

                    Response.Buffer = true;
                    Response.Clear();
                    Response.ContentType = mimeType;
                    Response.AddHeader("application/pdf", "attachment; filename= filename" + "." + filenameExtension);
                    Response.OutputStream.Write(rebytes, 0, rebytes.Length); // create the file  

                    Response.Flush(); // send it to the client to download  
                    Response.End();
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19707418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档