如何使用c#将rdlc报告保存在文件夹中

本文关键字:保存 存在 文件夹 报告 rdlc 何使用 | 更新日期: 2023-09-27 18:22:23

我在这个主题上做了很多搜索,但没有得到任何正确的解决方案。我想自动将rdlc报告保存在文件夹中。

如何使用c#将rdlc报告保存在文件夹中

当前您无法按原样保存rdlc报告。但您可以将其导出为其他格式并保存(Word/Excel/Pdf)。因为rdlc文件是由报告查看器呈现的。

private void savereport( )
    { string FileName = "File_" + DateTime.Now.ToString("ddMMyyyyhhmmss") +".pdf";
            string extension;
            string encoding;
            string mimeType;
            string[] streams;
            Warning[] warnings;
            string contentType = "application/pdf";
            Byte[] mybytes = ReportViewer1.LocalReport.Render("PDF", null,
                            out extension, out encoding,
                            out mimeType, out streams, out warnings); //for exporting to PDF  
            using (FileStream fs = File.Create(Server.MapPath("~/Report/") + FileName))
            {
                fs.Write(mybytes, 0, mybytes.Length);
            }
            //Response.ClearHeaders();
            //Response.ClearContent();
            //Response.Buffer = true;
            //Response.Clear();
            Response.ContentType = contentType;
            Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
            Response.WriteFile(Server.MapPath("~/Report/" + FileName));
            Response.Flush();
            //Response.Close();
            //Response.End();   
    }
try
{
    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    var reportViewer1 = new ReportViewer();
    reportViewer1.ProcessingMode = ProcessingMode.Local;
    reportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/CGReports.rdlc");
    var dsCustomers = new DataSet();
    using (var client = ServiceClient<ILoaneeStatusManager>.Create(ObjectConstants.LoaneeStatusManager))
    {
        DataTable dt = client.Instance.GetAllLoaneeStatusByCGdanForReport(CGDANNo);
        //   DataTable dt = new DataTable();
        //   dt.Clear();
        //   dt.Columns.Add("MLIFees");
        //   dt.Columns.Add("TranscationAmount");
        //   dt.Columns.Add("CreatedDate");
        //   dt.Columns.Add("AfterDate");
        //   DataRow row = dt.NewRow();
        //row["MLIFees"] = statusDtos[0].MLIFees;
        //row["TranscationAmount"]=statusDtos[0].SanctionedAmount;
        //row["CreatedDate"]=statusDtos[0].CreatedDate;
        //row["AfterDate"] = statusDtos[0].AfterDate;
        //dt.Rows.Add(row);
        dsCustomers.Tables.Add(dt.Copy());
        var datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);
        reportViewer1.LocalReport.DataSources.Clear();
        //RDLC FILE CAN CONTAIN MULTIPLE DATASOURCES WITHOUT
        reportViewer1.LocalReport.DataSources.Add(datasource);

        byte[] bytes = reportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
        // CODE TO SAVE THE REPORT FILE ON SERVER
        if (File.Exists(Server.MapPath("~/Documents/CGDANNo_" + CGDANNo + ".pdf")))
        {
            File.Delete(Server.MapPath("~/Documents/CGDANNo_" + CGDANNo + ".pdf"));
        }
        FileStream fileStream = new FileStream(Server.MapPath("~/Documents/CGDANNo_" + CGDANNo + ".pdf"), FileMode.Create);
        for (int i = 0; i < bytes.Length; i++)
        {
            fileStream.WriteByte(bytes[i]);
        }
        fileStream.Close();
    }
}
catch (Exception ex)
{
    ex.ToString();
}
ModelRealEstate.DB_RealEstateEntities objdb = new ModelRealEstate.DB_RealEstateEntities();
        var rows = objdb.Tbl_Property.Where(x => x.Adress.Contains(mtxbxRprt.Text)).ToList();
        reportViewer1.LocalReport.ReportPath = ("Report1.rdlc");
        reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("P2", mtxbxRprt.Text));
        reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", rows));
        reportViewer1.RefreshReport();