Export PDF of Crystal Reports (complete code C)

Source: Internet
Author: User

using system;
using system. collections;
using system. componentmodel;
using system. configuration;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;
// introduce two dll of the crystal report
using crystaldecisions. shared;
using crystaldecisions. crystalreports. engine;
using system. io;
using ghy. funds. fundsclasslibrary;
using system. data. sqlclient;

namespace webapptest
{< br> ///


// abstract description of webform1.
//
public class webform2: system. web. UI. page
{< br>
protected system. web. UI. webcontrols. dropdownlist dropdownlist1;
protected crystaldecisions. web. crystalreportviewer crystalreportviewer1;
protected system. web. UI. webcontrols. button btn_export2;
reportdocument reportdoc = new reportdocument ();

// page loading
sqlconnection myconn = new sqlconnection (configurationsettings. appsettings ["connectstring"]);
// sqlconnection myconn1 = new sqlconnection (configurationsettings. appsettings ["connectstring"]);
// reads the web. the database connection string in the config configuration file and connects to the specified database
private void page_load (Object sender, system. eventargs e)
{< br> // put user code to initialize the page here
// reportdatabind (); If (! Page. ispostback)
{< br> // bind during testing
reportdatabind ();
}< br>
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. btn_export2.click + = new system. eventhandler (this. btn_export2_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

// Bind the data source
Private void reportdatabind ()
{
String SQL = "select distinct, distinct, funds_rpt_hq.inoth, funds_rpt_hq.payhq, distinct from funds_rpt_hq where limit = '2017-05-09 '";
Myconn. open ();
Sqldataadapter mycomm = new sqldataadapter (SQL, myconn );
//
Dataset dset1 = new dataset ();
Mycomm. Fill (dset1, "funds_rpt_hq ");
Myconn. Close ();

String ls_rptpathname;
Ls_rptpathname = server. mappath (".") + "\ funds_rpt_hq.rpt ";
Reportdoc. Load (ls_rptpathname );
Reportdoc. setdatasource (dset1 );
Crystalreportviewer1.reportsource = reportdoc;
Crystalreportviewer1.databind ();
}

// Export a file
Private void btn_export2_click (Object sender, system. eventargs E)
{
Try
{
Reportdatabind ();
String exportpath;
String fname;
Exportpath = request. physicalapplicationpath + "exported ";
If (! Directory. exists (exportpath ))
{
System. Io. Directory. createdirectory (request. physicalapplicationpath + "exported ");
}

Fname = "funds_rpt_hq ";

Crystaldecisions. Shared. diskfiledestinationoptions opts = new crystaldecisions. Shared. diskfiledestinationoptions ();
// Export as a disk file
Crystaldecisions. Shared. exportoptions myexportoptions = reportdoc. exportoptions;
Myexportoptions. destinationoptions = opts;
Myexportoptions. exportdestinationtype = crystaldecisions. Shared. exportdestinationtype. diskfile;
// Export to PDF Format
Reportdoc. exportoptions. exportformattype = crystaldecisions. Shared. exportformattype. portabledocformat;
// Target path
Fname = fname + ". pdf ";
Opts. diskfilename = exportpath + fname;
// Export
Reportdoc. Export ();
Commoncode. Export howwindow ("exported file successful! ");
}
Catch (exception ex)
{
Commoncode. Export howwindow ("An error occurred while exporting the file! "+ Ex. Message. tostring (). substring (1, 90 ));
Return;

}

}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.