各位好,向大家请教个问题,是这样的,我在rdlc数据表中绑定了数据库中的数据,我不想让它在页面上显示,而想当我按页面上的一个按钮button1时,自动把该rdlc数据表导出到excel中去,请问该怎么实现呢,请知道的帮忙告诉一声,先在此谢谢大家了。

解决方案 »

  1.   

    可以用LocalReport类。
        static void Test()
        {
            LocalReport lr = new LocalReport();        string deviceInfo =
              "<DeviceInfo>" +
              "  <OutputFormat>Excel</OutputFormat>" +
              "</DeviceInfo>";        lr.ReportPath = @"C:\My Reports\Monthly Sales.rdlc";
            lr.DataSources.Add(new ReportDataSource("Sales", GetSalesData()));        string mimeType, encoding, extension;
            string[] streams;
            Warning[] warnings;        byte[] bytes = lr.Render(
                "Excel", 
                deviceInfo,
                out mimeType,
                out encoding,
                out extension,
                out streams,
                out warnings);        using (FileStream fs = new FileStream(@"c:\My Reports\Monthly Sales.xls", FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }
        }
      

  2.   

    gomoku 你好,用了你的方法,但我在运行时出现下面错误,不是很了解,能不能帮忙一下。
    用户代码未处理 Microsoft.Reporting.WebForms.LocalProcessingException
      Message="本地报表处理期间出错。"
      Source="Microsoft.ReportViewer.WebForms"
      StackTrace:
           在 Microsoft.Reporting.WebForms.LocalReport.CompileReport()
           在 Microsoft.Reporting.WebForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, CreateAndRegisterStream createStreamCallback, Warning[]& warnings)
           在 Microsoft.Reporting.WebForms.LocalReport.InternalRender(String format, Boolean allowInternalRenderers, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
           在 Microsoft.Reporting.WebForms.LocalReport.Render(String format, String deviceInfo, String& mimeType, String& encoding, String& fileNameExtension, String[]& streams, Warning[]& warnings)
           在 viiPOS.Reports.Rpt_StockSummary.Button1_Click(Object sender, EventArgs e) 位置 C:\Documents and Settings\SH_XuWuKe\My Documents\Visual Studio 2005\Projects\viiPOS 2.6.0\viiPOSManager\Reports\Rpt_StockSummary.aspx.cs:行号 226
           在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
           在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
           在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
           在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
           在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
           在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    提示错误出现在
     byte[] bytes = lr.Render(
                "Excel", 
                deviceInfo,
                out mimeType,
                out encoding,
                out extension,
                out streams,
                out warnings);
      

  3.   

    还有,你上面的GetSalesData()应该是获得数据吧,我把它改成我自己的一个数据表DataTable
      

  4.   

    但我在运行时出现下面错误,不是很了解,能不能帮忙一下。... 可能.rdlc有问题,或者数据源跟.rdlc不匹配。
      

  5.   

    gomoku 你好,这个问题找了还久还是找不出原因,我觉得我的.rdlc和数据源应该都没有问题,因为我做了一个测试,在这个页面上,我用了一个ReportViewer把.rdlc显示出现,可以正常显示,但当我导出时,我不用ReportViewer里面的导出按钮,我自己放了一个按钮,然后把上面的代码放到按钮的事件里面,但就是老是出现上面的问题,真的搞不懂