首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为ReportViewer创建自定义导出到Excel (rdlc)

为ReportViewer创建自定义导出到Excel (rdlc)
EN

Stack Overflow用户
提问于 2010-08-16 22:20:39
回答 3查看 53K关注 0票数 19

我对在ReportViewer中为我的报告创建一个自定义的“导出到Excel”选项很感兴趣。这主要是因为我想要pdf disalbed,我通过:

代码语言:javascript
复制
 ReportViewer1.ShowExportControls = false;

因为在ReportViewer中无法禁用任何特定的导出功能(例如,pdf而不是excel)。下面是我(稍微)修改过的代码。理想情况下,我希望类似于以前的导出选项,我可以将文件保存到我想要的任何位置。

编辑:代码可以工作,但我需要如何修改文件流,以便提示用户可以保存到他们想要的任何位置,而不是让文件自动保存?

代码语言:javascript
复制
protected void btnExportExcel_Click(object sender, EventArgs e)
{
    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;

    byte[] bytes = ReportViewer1.LocalReport.Render(
       "Excel", null, out mimeType, out encoding,
        out extension,
       out streamids, out warnings);

    FileStream fs = new FileStream(@"c:\output.xls",
       FileMode.Create);
    fs.Write(bytes, 0, bytes.Length);
    fs.Close();

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-08-18 20:54:16

我基于微软关于ReportViewer的文档和一些谷歌搜索将这些放在一起,以防任何人遇到类似我的问题:

代码语言:javascript
复制
protected void ExportExcel_Click(object sender, EventArgs e)
{
    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    string filename;

    byte[] bytes = ReportViewer1.LocalReport.Render(
       "Excel", null, out mimeType, out encoding,
        out extension,
       out streamids, out warnings);

    filename = string.Format("{0}.{1}", "ExportToExcel", "xls");
    Response.ClearHeaders();
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
    Response.ContentType = mimeType;
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}
票数 21
EN

Stack Overflow用户

发布于 2013-04-23 08:35:44

提醒一下..。接受的答案将呈现为XLS文件,这是原始发帖者所要求的。

但是,您现在也可以导出到XLSX。您必须将Render()方法的format参数从"Excel"更改为"EXCELOPENXML"

要获得可能值的完整列表,可以调用ReportViewer1.LocalReport.ListRenderingExtensions()。当我在我的报表查看器实例上运行它时,我得到了以下可能的选项:

"Excel" "EXCELOPENXML" "IMAGE" "PDF" "WORD" "WORDOPENXML"

我发现很难确定您需要为这些格式传递什么内容。如果你问我,MSDN对此的描述非常糟糕。

票数 35
EN

Stack Overflow用户

发布于 2021-05-25 23:41:37

有一种方法可以在报告查看器中禁用特定导出。见下文。

代码语言:javascript
复制
Step 1: Add OnPreRender event for the report viewer
Step 2: Inside the ReportViewer_PreRender function add the following code

DisableReportExportType.HideUnwantedExportFormat((ReportViewer)sender, "PDF"); // Disables PDF
DisableReportExportType.HideUnwantedExportFormat((ReportViewer)sender, "WORDOPENXML"); //Disables Word
DisableReportExportType.HideUnwantedExportFormat((ReportViewer)sender, "EXCELOPENXML"); //Disables Excel

根据需要使用上面的内容。希望这就是你要找的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3494009

复制
相关文章

相似问题

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