0

我有一个生成报告的功能。有用。我的问题是我在 C# 中使用 MVC3,我无法在文件中插入报表查看器。.cshtml。Ascx 我用来尝试显示报告,但出现以下错误:

执行处理程序“System.Web.Mvc.HttpHandlerUtil ServerExecuteHttpHandlerWrapper +”的子请求时出错

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

relatorio.cshtml

@{
    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

<%@ 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

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 控制器中的函数

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;
            }
        }
4

1 回答 1

0

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

     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();
        }
于 2013-10-31T13:30:20.753 回答