从 ReportViewer 和 SQL Server 报表创建 Excel 文件

发布于 2024-12-02 07:31:37 字数 485 浏览 3 评论 0原文

我有一个 Web 应用程序,它有一个网格,向用户显示分页数据列表。用户需要将结果下载到 Excel 电子表格的选项。但是,正如我所说,网格中显示的项目是分页的,我需要整个结果集。此外,我只显示每个项目大约 7 个字段,而电子表格将包含一个项目的所有 20 多个字段。所以,我并不是在寻找网格到 Excel 的解决方案。

我正在尝试做一些我以前实际工作过的事情。在以前的雇主(源代码不可用)中,我们有一个应用程序,其中包含作为 RDLC 文件的 SQL Server 报告。我们将在代码中创建一个 ReportViewer 控件 (var reportViewer = new ReportViewer())。然后,我们将报告绑定到控件,为其提供数据源,然后以我们需要的任何格式呈现结果。就我而言,我需要一个 Excel 文件,然后将 ExcelFile 在响应中流式传输回用户。

我使用的是 MVC3,因此我将返回 Excel 文件作为 FileContentResult。我已经在互联网上搜索了一天,但找不到我要找的东西。

I have a web application that has a grid displaying a paged list of data to the user. The user needs the option to download the results to an Excel spreadsheet. However, the items displayed in the grid are, like I said, Paged and I need the whole result set. In addition, I'm only displaying about 7 fields per item, whereas the spreadsheet will contain all 20+ fields for an item. So, I'm not looking for a grid-to-Excel solution.

I'm trying to do something that I've actually worked with before. At a previous employer (source code not available), we had an application that contained SQL Server report as an RDLC file. We would create a ReportViewer control in code (var reportViewer = new ReportViewer()). Then, we would bind the report to the control, give it a datasource and then render the results in whatever format we needed. In my case I need an Excel File, and then stream the ExcelFile back to the user in the response.

I'm using MVC3, so I'll be returning the Excel file as a FileContentResult. I've been searching the internet for a day and can't quite find what I'm looking for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2024-12-09 07:31:37

控制器动作。格式参数应为“EXCEL”。

    public FileResult Report(String format)
    {
        LocalReport report = new LocalReport();
        report.ReportPath = Server.MapPath("~/TestReport.rdlc");

        report.DataSources.Clear();
        report.DataSources.Add(new ReportDataSource(GetData()));

        report.Refresh();

        return GetFileContentResult(report, format, null, "TestReport");
    }

从任何报告创建 FileContentResult 的帮助程序方法。

    public FileContentResult GetFileContentResult(Report report, String format, String deviceInfo, String fileDownloadName)
    {
        String mimeType;
        String encoding;
        String filenameExtension;
        String[] streamIds;
        Warning[] warnings;

        FileContentResult fileContentResult = new FileContentResult(report.Render(format, deviceInfo, out mimeType, out encoding, out filenameExtension, out streamIds, out warnings), mimeType);
        fileContentResult.FileDownloadName = Path.ChangeExtension(fileDownloadName, filenameExtension);

        return fileContentResult;
    }

编辑:忘记调用帮助功能。哎呀。

Controller action. The format parameter should be 'EXCEL'.

    public FileResult Report(String format)
    {
        LocalReport report = new LocalReport();
        report.ReportPath = Server.MapPath("~/TestReport.rdlc");

        report.DataSources.Clear();
        report.DataSources.Add(new ReportDataSource(GetData()));

        report.Refresh();

        return GetFileContentResult(report, format, null, "TestReport");
    }

Helper method that creates a FileContentResult from any Report.

    public FileContentResult GetFileContentResult(Report report, String format, String deviceInfo, String fileDownloadName)
    {
        String mimeType;
        String encoding;
        String filenameExtension;
        String[] streamIds;
        Warning[] warnings;

        FileContentResult fileContentResult = new FileContentResult(report.Render(format, deviceInfo, out mimeType, out encoding, out filenameExtension, out streamIds, out warnings), mimeType);
        fileContentResult.FileDownloadName = Path.ChangeExtension(fileDownloadName, filenameExtension);

        return fileContentResult;
    }

Edit: Forget to call the help function. Oops.

辞慾 2024-12-09 07:31:37

您可以尝试如下所示的操作...我们从数据访问级别填充对象列表,然后将该对象列表传递给如下所示的操作...

public static byte[] SaveExcelData<T>(List<T> answer)
{
    byte[] fileData = null;

    var grid = new System.Web.UI.WebControls.GridView();

    grid.DataSource = answer;
    grid.DataBind(); 

    HttpContext.Current.Response.ClearContent();
    string headerAddition = "attachment; filename=";
    headerAddition += answer[0].GetType().Name + ".xls";
    HttpContext.Current.Response.AddHeader("content-disposition", headerAddition);
    HttpContext.Current.Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid.RenderControl(htw);

    fileData = System.Text.Encoding.UTF8.GetBytes(sw.ToString());

    return fileData;
}

You might try something like the following... We fill a list of objects from our data access level and then pass that list of objects to something like the following...

public static byte[] SaveExcelData<T>(List<T> answer)
{
    byte[] fileData = null;

    var grid = new System.Web.UI.WebControls.GridView();

    grid.DataSource = answer;
    grid.DataBind(); 

    HttpContext.Current.Response.ClearContent();
    string headerAddition = "attachment; filename=";
    headerAddition += answer[0].GetType().Name + ".xls";
    HttpContext.Current.Response.AddHeader("content-disposition", headerAddition);
    HttpContext.Current.Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid.RenderControl(htw);

    fileData = System.Text.Encoding.UTF8.GetBytes(sw.ToString());

    return fileData;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文