开发者

Crystal Report Viewer - Programmatically restricting File Format options

开发者 https://www.devze.com 2022-12-21 19:13 出处:网络
In Crystal Reports Viewer (2008) for ASP.Net, when you click on the Export button, the Export dialog shows up with File Format options:

In Crystal Reports Viewer (2008) for ASP.Net, when you click on the Export button, the Export dialog shows up with File Format options:

  • Crystal Reports (RPT)
  • PDF
  • Microsoft Excel(97-2003)
  • Microsoft Excel(97-2003) Data-Only
  • Microsoft Word (97-2003)
  • Microsoft Word (97-2003) Editable
  • Rich Text Format (RTF)
  • XML

e开发者_如何学JAVAtc..

Does anyone know how to remove some of these options so that end users wouldn't see it?


We've ran into this same issue and ended up rolling our own export page and limited the selection there.

It works great, but I would have expected more from Crystal Reports!


From what I was able to find, you could try to create your own export button option, removing the given button option and adding your own to the asp page. You would need to start by dragging the button onto the page and double clicking it to auto generate the code. From there add the code

crystalReportViewer1.ExportReport ()  

Once this code is in it will use the default settings for the export options, however if you want to change the export options within that button then you have to manually code it.

' Declare variables and get the export options.
Dim exportOpts As New ExportOptions()
Dim diskOpts As New DiskFileDestinationOptions()
Dim excelFormatOpts As New ExcelFormatOptions()
exportOpts = Report.ExportOptions

' Set the excel format options.
excelFormatOpts.ExcelTabHasColumnHeadings = true

exportOpts.ExportFormatType = ExportFormatType.Excel
exportOpts.FormatOptions = excelFormatOpts

' Set the export format.
exportOpts.ExportFormatType = ExportFormatType.Excel

exportOpts.ExportDestinationType = ExportDestinationType.DiskFile

' Set the disk file options.
diskOpts.DiskFileName = fileName
exportOpts.DestinationOptions = diskOpts

Report.Export()

MSDN SITE

This link gives you the same code posted above. It show how to do it in Visual Basic code. therefor in your aspx.vb code you would have to manually enter the the types of formats you want.


You might be able to control the export options by removing the export DLLs. Search for crxf_*.dll in the Business Objects\Common\\bin directory.


Refer Below Answer to remove unwanted export Option from Crystal Reports

Display PDF and Excel export options in Crystal Reports?


 <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" />   

try this:

   <CR:CrystalReportViewer ... HasExportButton="false"   HasPrintButton="False" >

  <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 (System.Threading.ThreadAbortException)
    {
    //The issue has been identified and logged under Problem Report ID     
    //ADAPT00765364. The error is likely caused because Response.End() is used inside the    
    //ExportToHttpResponse() method.
    //It is a known issue that Reponse.End() causes the thread to abort. This is by design.
    //See Microsoft KB312629 Article for more info.
    }

    catch (Exception ex)
    {
       //error management

    }
}


Apparently a later version (13.0?) adds a AllowedExportFormats property to the CrystalReportViewer class.

I don't have that newer version (I just have 12.0) but apparently something like this is all you need:

int exportFormatFlags = (int)
    (CrystalDecisions.Shared.ViewerExportFormats.PdfFormat |
     CrystalDecisions.Shared.ViewerExportFormats.ExcelFormat |
     // any other desired formats
    );
crystalReportViewer1.AllowedExportFormats = exportFormatFlags;


    using (ReportClass rptH = new ReportClass())
   {
    rptH.FileName = @"C:/Report/crJournal.rpt"; //Your rpt file path 
        // if you put your rpt file on Bin then you need to write only rpt file name
    rptH.Load();
    rptH.SetDataSource( ds );// Provide Dataset for report : Ds is DataSet
    rptH.ExportToDisk(ExportFormatType.Excel, "Give Output file path");

   }

This code is defenetly work :

0

精彩评论

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

关注公众号