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

fir*_*ger 18 .net reportviewer

我有兴趣为ReportViewer中的报表创建自定义导出到Excel选项.这主要是因为我想要pdf被剥夺,我通过以下方式做到了:

 ReportViewer1.ShowExportControls = false;
Run Code Online (Sandbox Code Playgroud)

由于无法在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();

}
Run Code Online (Sandbox Code Playgroud)

JBa*_*mer 30

只是抬头......接受的答案将呈现为XLS文件,这是原始海报所要求的.

但是,您现在也可以导出到XLSX.您必须formatRender()方法的参数更改"Excel""EXCELOPENXML".

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

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

我发现很难确定你需要传递什么格式.如果你问我,MSDN会很难记录这个.


fir*_*ger 19

我根据微软在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();
}
Run Code Online (Sandbox Code Playgroud)