开发者

how to save crystal report that converted to pdf in asp .net

开发者 https://www.devze.com 2023-03-05 12:24 出处:网络
I wrote code that converts my crystal report to pdf, but it can\'t store the pdf file automatically to my project.

I wrote code that converts my crystal report to pdf, but it can't store the pdf file automatically to my project.

protected void Page_Load(object sender, EventArgs e)
{
    CrystalReportViewer1.ReportSource = getReportDocument();
    CrystalReportViewer1.DataBind();
    // Get the report document
    ReportDocument repDoc = getReportDocument();
    // Stop buffering the response
    Response.Buffer = false;
    // Clear the response content and headers
    Response.ClearContent();
    Respon开发者_C百科se.ClearHeaders();
    try
    {
        // Export the Report to Response stream in PDF format and file name Customers
        repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "TFA");

        // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        ex = null;
    }
}

private ReportDocument getReportDocument()
{
  // File Path for Crystal Report
  string repFilePath = Server.MapPath("~/CrystalReport1.rpt");
  // Declare a new Crystal Report Document object
  // and the report file into the report document
  ReportDocument repDoc = new ReportDocument();

  repDoc.Load(repFilePath);

  // Set the datasource by getting the dataset from business
  // layer and
 // In our case business layer is getCustomerData function
 return repDoc;
}


 ExportOptions objExOpt;
            CrystalReportViewer1.ReportSource = (object)getReportDocument();
            CrystalReportViewer1.DataBind();
            // Get the report document
            ReportDocument repDoc = getReportDocument();
            repDoc.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            repDoc.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            DiskFileDestinationOptions objDiskOpt = new DiskFileDestinationOptions();
            objDiskOpt.DiskFileName = @"c:\r.pdf";
            repDoc.ExportOptions.DestinationOptions = objDiskOpt;
            repDoc.Export();


In an older project I've worked on, I opened the pdf-report in a new browser-window, so the user could choose whether or not to save the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消