创建自定义导出到Excel中的ReportViewer(RDLC) [英] Creating a Custom Export to Excel for ReportViewer (rdlc)

查看:283
本文介绍了创建自定义导出到Excel中的ReportViewer(RDLC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣创建自定义导出到Excel选项为我的ReportViewer报告。这主要是因为我想PDF disalbed而我做了通过:

  ReportViewer1.ShowExportControls = FALSE;
 

由于没有办法禁用任何特定的导出功能(例如PDF格式,但不擅长)中的ReportViewer。这里是我的(略)修改code以下。理想情况下,我想类似previous导出选项,我可以将文件保存到任何位置,我想要的东西。

编辑是否:code ++工程,但如何将我需要修改的文件流而不是由文件会自动保存,我可以提示用户,以便他们可以保存到任何位置,他们希望让

 保护无效btnExportExcel_Click(对象发件人,EventArgs的)
{
    警告[]警告;
    字符串[] streamids;
    串MIMETYPE;
    字符串编码;
    字符串的扩展;

    字节[]字节= ReportViewer1.LocalReport.Render(
       创先争优,空,MIMETYPE出来,出来的编码,
        延伸出来,
       出streamids,出警告);

    的FileStream FS =新的FileStream(@C:\ output.xls
       FileMode.Create);
    fs.Write(字节,0,bytes.Length);
    fs.Close();

}
 

解决方案

我把这个一起基于微软的上的ReportViewer一些,以防有人跑进我相似的问题,谷歌搜索文档:

 保护无效ExportExcel_Click(对象发件人,EventArgs的)
{
    警告[]警告;
    字符串[] streamids;
    串MIMETYPE;
    字符串编码;
    字符串的扩展;
    字符串的文件名;

    字节[]字节= ReportViewer1.LocalReport.Render(
       创先争优,空,MIMETYPE出来,出来的编码,
        延伸出来,
       出streamids,出警告);

    文件名=的String.Format({0} {1},ExportToExcel,XLS);
    Response.ClearHeaders();
    Response.Clear();
    Response.AddHeader(内容处置,附件;文件名=+文件名);
    Response.ContentType = MIMETYPE;
    Response.BinaryWrite(字节);
    Response.Flush();
    到Response.End();
}
 

I'm interested in creating a custom Export to Excel option for my Report in ReportViewer. This is mostly because I want pdf disalbed and I did that via:

 ReportViewer1.ShowExportControls = false;

Since there is no way to disable any specific export functionality (e.g. pdf but not excel) in ReportViewer. Here's my (slightly) modified code below. Ideally I would like something similar to the previous Export options where I can save the file to whatever location I want.

EDIT: The code works but how would I need to modify the Filestream so that instead of having the file get saved automatically I can prompt the user so that they can save to whichever location they want?

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();

}

解决方案

I put this together based on Microsoft's documentation on ReportViewer and some Google searches in case anyone runs into the issue similar to mine:

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();
}

这篇关于创建自定义导出到Excel中的ReportViewer(RDLC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆