如何在 ASP.NET 中使用 C# 代码导出 PDF、HTML 和 DOC 格式的 Crystal Report?

发布于 2024-11-15 04:42:51 字数 172 浏览 6 评论 0 原文

我在 ASP.NET 网站中设计了一个报告,现在我需要提供以 PDF、HTML 和 DOC 格式导出该报告的选项,我该如何实现?

Crystal Report 有一个按钮可以做到这一点,但是当我尝试保存该报告时,它会保存为 .aspx 格式,就像我在 ASP.NET 网页中查看它一样。

I have designed a Report in my ASP.NET web site now i need to provide options to export that report in PDF, HTML, and DOC formats, how do i achieve that?

crystal report has one button to do that but when ever i try to save that report its saved as .aspx format as i am viewing it in asp.net web page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见终念 2024-11-22 04:42:51

试试这个:

  <asp:ImageButton Width="20px" Height="20px" ID="btnPdf" runat="server"
    OnClick="btnExport_Click" ImageUrl="~/Images/PDF.png" AlternateText="Export To PDF" CssClass="AddedButton" />
   <asp:ImageButton Width="20px" Height="20px" ID="btnXls" runat="server" 
    OnClick="btnExport_Click" ImageUrl="~/Images/XLS.png" AlternateText="Export To Excel" />   
   <asp:ImageButton Width="20px" Height="20px" ID="btnDoc" runat="server" 
    OnClick="btnExport_Click" ImageUrl="~/Images/DOC.png" AlternateText="Export To Word" />   

C# 代码:

protected void btnExport_Click(object sender, EventArgs e)
{
    // Stop buffering the response
    Response.Buffer = false;
    // Clear the response content and headers
    Response.ClearContent();
    Response.ClearHeaders();
    try
    {
        string senderID = ((ImageButton)sender).ID;
        if (senderID == "btnPdf")
            reportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, Page.Title);
        else if (senderID == "btnXls")
            reportDocument.ExportToHttpResponse(ExportFormatType.ExcelRecord, Response, true, Page.Title);
        else if (senderID == "btnDoc")
            reportDocument.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, Page.Title);
        // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
    }
    catch (Exception ex)
    {
       //error management

    }
}

try this:

  <asp:ImageButton Width="20px" Height="20px" ID="btnPdf" runat="server"
    OnClick="btnExport_Click" ImageUrl="~/Images/PDF.png" AlternateText="Export To PDF" CssClass="AddedButton" />
   <asp:ImageButton Width="20px" Height="20px" ID="btnXls" runat="server" 
    OnClick="btnExport_Click" ImageUrl="~/Images/XLS.png" AlternateText="Export To Excel" />   
   <asp:ImageButton Width="20px" Height="20px" ID="btnDoc" runat="server" 
    OnClick="btnExport_Click" ImageUrl="~/Images/DOC.png" AlternateText="Export To Word" />   

C# code:

protected void btnExport_Click(object sender, EventArgs e)
{
    // Stop buffering the response
    Response.Buffer = false;
    // Clear the response content and headers
    Response.ClearContent();
    Response.ClearHeaders();
    try
    {
        string senderID = ((ImageButton)sender).ID;
        if (senderID == "btnPdf")
            reportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, Page.Title);
        else if (senderID == "btnXls")
            reportDocument.ExportToHttpResponse(ExportFormatType.ExcelRecord, Response, true, Page.Title);
        else if (senderID == "btnDoc")
            reportDocument.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, Page.Title);
        // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
    }
    catch (Exception ex)
    {
       //error management

    }
}
夏末染殇 2024-11-22 04:42:51

您必须自己完成:创建一个包含所需格式的下拉列表和一个用于导出回发的按钮。

这是 .Net 1.1 / CR9 的示例。回发到以下内容时:

  1. 请分配给报表类实例属性值 MyReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
  2. 如果要导出为 .pdf, 请执行以下操作:< code>MyReport.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat; 您也可以选择WordForWindowsRichTextExcelHTML40 等。
  3. 然后执行以下操作:

    CrystalDecisions.Shared.DiskFileDestinationOptions fileOptions = new DiskFileDestinationOptions();

    fileOptions.DiskFileName = "someTmpFileName";

    MyReport.DestinationOptions = fileOptions;

    MyReport.Export();

您可以找到有关ExportOptions 类位于此处

这里是 VS 2005 / .Net 2 的示例

You have to do it by yourself: create a dropdown list with formats you want and a button to make a postback for exporting.

This is an example for the .Net 1.1 / CR9. When making a postback to the following:

  1. assign to your report class instance property value MyReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
  2. if you want to export to .pdf do the following: MyReport.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat; You can also choose WordForWindows, RichText, Excel, HTML40 and more.
  3. Then do the following:

    CrystalDecisions.Shared.DiskFileDestinationOptions fileOptions = new DiskFileDestinationOptions();

    fileOptions.DiskFileName = "someTmpFileName";

    MyReport.DestinationOptions = fileOptions;

    MyReport.Export();

You can find more about ExportOptions class here.

And here is an example for VS 2005 / .Net 2

五里雾 2024-11-22 04:42:51

您可以使用 Crystal 报表查看器控件,该控件具有 Word、Excel、odf 等的内置导出功能...

水晶报告查看器

You can use the Crystal report viewer control which has in built exporting functionality for Word, excel, odf etc...

Crystal report viewer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文