我对在ReportViewer中为我的报告创建一个自定义的“导出到Excel”选项很感兴趣。这主要是因为我想要pdf disalbed,我通过:
ReportViewer1.ShowExportControls = false;
因为在ReportViewer中无法禁用任何特定的导出功能(例如,pdf而不是excel)。下面是我(稍微)修改过的代码。理想情况下,我希望类似于以前的导出选项,我可以将文件保存到我想要的任何位置。
编辑:代码可以工作,但我需要如何修改文件流,以便提示用户可以保存到他们想要的任何位置,而不是让文件自动保存?
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();
}
发布于 2010-08-18 20:54:16
我基于微软关于ReportViewer的文档和一些谷歌搜索将这些放在一起,以防任何人遇到类似我的问题:
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();
}
发布于 2013-04-23 08:35:44
提醒一下..。接受的答案将呈现为XLS文件,这是原始发帖者所要求的。
但是,您现在也可以导出到XLSX。您必须将Render()
方法的format
参数从"Excel"
更改为"EXCELOPENXML"
。
要获得可能值的完整列表,可以调用ReportViewer1.LocalReport.ListRenderingExtensions()
。当我在我的报表查看器实例上运行它时,我得到了以下可能的选项:
"Excel"
"EXCELOPENXML"
"IMAGE"
"PDF"
"WORD"
"WORDOPENXML"
我发现很难确定您需要为这些格式传递什么内容。如果你问我,MSDN对此的描述非常糟糕。
发布于 2021-05-25 23:41:37
有一种方法可以在报告查看器中禁用特定导出。见下文。
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
根据需要使用上面的内容。希望这就是你要找的。
https://stackoverflow.com/questions/3494009
复制相似问题