用rdlc文件直接导出到excel或PDF

本文介绍了一个使用C#从XML文件加载销售数据的方法,并展示了如何将这些数据展示在本地报表中,最后实现了将报表以Excel格式导出的功能。

private DataTable LoadSalesData()
        {
            // Load data from XML file.
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(@"c:\Reports\data.xml");
            return dataSet.Tables[0];
        }
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.reportViewer1.ProcessingMode = ProcessingMode.Local;
            this.reportViewer1.LocalReport.ReportPath = @"c:\Reports\Report1.rdl";
            reportViewer1.LocalReport.DataSources.Add(
            new ReportDataSource("Sales", LoadSalesData()));
            this.reportViewer1.RefreshReport();
        }

        private void button1_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();

            MessageBox.Show("Report exported to output.xls", "Info");

        }

 

如果是BS模式,先保存在服务器上面,再从服务器上面下载下来

转载于:https://www.cnblogs.com/hubj/archive/2008/10/23/1318114.html

在C#中使用RDLC报表时,设置导出页面格式可从不同方面进行操作。 ### 限制导出格式 若要限制导出格式,比如只允许导出PDF,可在ASP.NET中对`ReportViewer`的`PreRender`事件进行处理。示例代码如下: ```csharp protected void ReportViewer1_PreRender(object sender, EventArgs e) { if (Request.QueryString["type"] == "Delivery") { // 默认导出格式有三种 Excel,WORD,PDF。此时设置只能导出PDF foreach (RenderingExtension extension in ReportViewer1.LocalReport.ListRenderingExtensions()) { if (extension.Name == "Excel" || extension.Name == "WORD") { FieldInfo fi = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic); fi.SetValue(extension, false); } } } } ``` 此代码通过遍历`ReportViewer`的渲染扩展,将Excel和Word的渲染扩展设置为不可见,从而仅允许导出PDF格式 [^2]。 ### 自动导出指定格式 可以编写后台代码实现自动导出ExcelPDF、Word格式。示例代码如下: ```csharp // 自动导出excel/pdf/word private void ResponseFile(int oType, string fileName) { string outType; if (oType == 0) { outType = "Excel"; } else if (oType == 1) { outType = "Word"; } else { outType = "Word"; } try { Warning[] warnings; string[] streamids; string mimeType; string encoding; string extension; byte[] bytes = ReportViewer1.LocalReport.Render( outType, null, out mimeType, out encoding, out extension, out streamids, out warnings); Response.Clear(); Response.Buffer = true; Response.ContentType = mimeType; Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "." + extension); Response.BinaryWrite(bytes); Response.Flush(); } catch (Exception ex) { throw new Exception(ex.Message); } } ``` 该方法依据传入的`oType`参数确定导出格式,然后利用`ReportViewer`的`Render`方法生对应格式的文件并进行输出 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值