【问题标题】:asp.net C# webform show report without reportviewerasp.net C# webform 显示没有reportviewer的报告
【发布时间】:2015-12-01 13:30:17
【问题描述】:

目前我想生成报告而不显示在报告查看器中。我在网上搜索解决方案,但我只能找到 MVC 方法。我无法将其转换为正常的网络表单行为。

 public ActionResult Report(string id)
        {
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"), "ReportStateArea.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<StateArea> cm = new List<StateArea>();
            using (PopulationEntities dc = new PopulationEntities())
            {
                cm = dc.StateAreas.ToList();
            }
            ReportDataSource rd = new ReportDataSource("MyDataset", cm);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType;
            string encoding;
            string fileNameExtension;



            string deviceInfo =

            "<DeviceInfo>" +
            "  <OutputFormat>" + id + "</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 = lr.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);


            return File(renderedBytes, mimeType);
        }
    enter code here

这是一个代码示例,可以让用户生成不同格式的报告,例如 PDF、excel、图像。 (http://www.dotnetawesome.com/2013/09/microsoft-report-in-mvc-4.html)。

任何人都可以通过使用SQL select 语句而不是LINQ 来帮助或为我提供一个有简单教程的网站?

【问题讨论】:

    标签: asp.net webforms report


    【解决方案1】:

    就从 WebForms 页面返回报告而言,上面的内容实际上非常接近。您想通过当前 Page 上下文的 Response 对象返回 renderedBytes 数组,例如:

    protected void ReportPrint_Click(object sender, EventArgs e)
        {
        string id = "PDF"; // get this from another control on your page
        LocalReport lr = new LocalReport();
        string path = Path.Combine(Server.MapPath("~/Report"), "ReportStateArea.rdlc");
        if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
        else
            {
                // handle error condition
            }
    
        List<StateArea> cm = new List<StateArea>();
        using (PopulationEntities dc = new PopulationEntities())
            {
                cm = dc.StateAreas.ToList();
            }
        ReportDataSource rd = new ReportDataSource("MyDataset", cm);
        lr.DataSources.Add(rd);
        string reportType = id;
        string mimeType;
        string encoding;
        string fileNameExtension;
    
        string deviceInfo =
    
        "<DeviceInfo>" +
        "  <OutputFormat>" + id + "</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 = lr.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);
    
    
        Response.Clear(); // we're going to override the default page response
        Response.ContentType = mimeType;
        Response.AddHeader("content-disposition", "attachment; filename=report." + fileNameExtension);
        Response.BinaryWrite(renderedBytes);
        Response.End();
        }
    

    这将用您的报告内容替换正常的页面响应。

    就 SQL 与 Linq 而言,ReportViewer 使用的 DataSource 对象是旧式 System.Data.DataTable,因此只需查找有关从 SQL 填充 DataTable 的信息。

    【讨论】:

    • 您好,感谢您的代码,但最后一行 CompleteRequest() 在设计时出现错误。错误是上下文中不存在。
    • 尝试使用 Response.End() 代替(见编辑示例)
    • 嗨,它工作!!!谢谢!此外,如果选择 Image/Word/Excel 文件类型,是否可以制作保存文件对话框?因为我意识到如果我选择这些文件格式,它会下载到电脑上,但我希望可以让用户输入文件名和文件路径来保存。
    • 查看我的示例的最新编辑 - 特别是 BinaryWrite 之前的“content-disposition”的附加标头。它应该强制打开文件保存对话框,允许用户更改文件名。
    • 您好,感谢您的回复,我之前尝试过,但不幸的是它不起作用,它确实弹出了保存对话框,但它不允许更改文件名。另外,使用Chrome直接下载
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多