Exporting from Crystal Reports to PDF, Word, Excel and HTML

Source: Internet
Author: User

Crystal Reports is a welcome subjects for blog posts. I still do like the product, My users are very happy with the results, the report editor is not that bad to work with and the components integrate well into a solution. but crystal documentation is an absolute disaster. I wanted to add some functionality to my basic export routine. the only thing was adding export to excel and to HTML. this functionality is present in the basic crystal installation but how to use it is something which took me really a lot of Googling. the answers were not on the crystal site but in the dungeons of the Usenet. let me share what I found.

On of the things I learned is that you haveClose ()A report after exporting. This was not in the official crystal example. I havn't measured the effect but it doesn't harm.

Exporting to excel turned out to be just a matter of setting the right content type. This type turned out to beApplication/vnd. MS-Excel. I had expectedApplication/MSExcel, As a nice siblingApplication/MSWord. Exporting to excel has some extra format options but you can do without them for a basic export. I'll leave these for another post.

To get the exporting to HTML to work took some things which are next to ridiculous, but I got it working. you have to set some format options. in these options you set the root directory and the filename. this directory-filename pair shocould be identical to the export filename passed to the report. the result will be an HTML formatted report, but in a "slightly" different location. to find the result you have to do some tricks. when crystals creates a report is does create a temporary. rpt file. this file is stored in the Windows/temp dir, It's name is a guid. when crystal creates the exported report it creates a directory in the root directory supplied with the name of this GUID. in this directory the Report will be created, using the filename supplied. thank goodness the name of the temporary file is available inFilepathProperty of the report. This snippet demonstrates the workaround:

String [] fp = selectedreport. filepath. Split ("//". tochararray ());
String leafdir = FP [FP. Length-1];
// Strip. rpt Extension
Leafdir = leafdir. substring (0, leafdir. Length-4 );
Tempfilenameused = string. Format ("{0} {1} // {2}", tempdir, leafdir, tempfilename );

To sum it all up:

Protected void exportreport (crystaldecisions. crystalreports. Engine. reportclass selectedreport, crystaldecisions. Shared. exportformattype EFT)
{
Selectedreport. exportoptions. exportformattype = EFT;

String contenttype = "";
// Make sure Asp.net has create and delete permissions in the directory
String tempdir = system. configuration. configurationsettings. receivettings ["tempdir"];
String tempfilename = session. sessionid. tostring () + ".";
Switch (EFT)
{
Case crystaldecisions. Shared. exportformattype. portabledocformat:
Tempfilename + = "pdf ";
Contenttype = "application/pdf ";
Break;
Case crystaldecisions. Shared. exportformattype. wordforwindows:
Tempfilename + = "Doc ";
Contenttype = "application/MSWord ";
Break;
Case crystaldecisions. Shared. exportformattype. Excel:
Tempfilename + = "xls ";
Contenttype = "application/vnd. MS-excel ";
Break;
Case crystaldecisions. Shared. exportformattype. html32:
Case crystaldecisions. Shared. exportformattype. html40:
Tempfilename + = "htm ";
Contenttype = "text/html ";
Crystaldecisions. Shared. htmlformatoptions hop = new crystaldecisions. Shared. htmlformatoptions ();
Hop. htmlbasefoldername = tempdir;
Hop. htmlfilename = tempfilename;
Selectedreport. exportoptions. formatoptions = hop;
Break;
}

Crystaldecisions. Shared. diskfiledestinationoptions DFO = new crystaldecisions. Shared. diskfiledestinationoptions ();
DFO. diskfilename = tempdir + tempfilename;
Selectedreport. exportoptions. destinationoptions = DFO;
Selectedreport. exportoptions. exportdestinationtype = crystaldecisions. Shared. exportdestinationtype. diskfile;

Selectedreport. Export ();
Selectedreport. Close ();

String tempfilenameused;
If (EFT = crystaldecisions. Shared. exportformattype. html32 | EFT = crystaldecisions. Shared. exportformattype. html40)
{
String [] fp = selectedreport. filepath. Split ("//". tochararray ());
String leafdir = FP [FP. Length-1];
// Strip. rpt Extension
Leafdir = leafdir. substring (0, leafdir. Length-4 );
Tempfilenameused = string. Format ("{0} {1} // {2}", tempdir, leafdir, tempfilename );
}
Else
Tempfilenameused = tempdir + tempfilename;

Response. clearcontent ();
Response. clearheaders ();
Response. contenttype = contenttype;

Response. writefile (tempfilenameused );
Response. Flush ();
Response. Close ();

System. Io. file. Delete (tempfilenameused );
}

Which is pretty straightforward and even works. With a lot of thanks to the same people on the Web who have also been strugling with this.

Peter
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////
ReportDocument Doc = new reportdocument ();
Doc. Load (this. mappath ("Allah. rpt "));
Doc. setdatasource (DS );
Exportoptions exportopts = Doc. exportoptions;
Exportopts. exportformattype = exportformattype. portabledocformat;
Exportopts. exportdestinationtype = exportdestinationtype. diskfile;
Exportopts. destinationoptions = new diskfiledestinationoptions ();
String fname = "C: // t //" + session. sessionid + ". pdf ";
Diskfiledestinationoptions diskopts = new diskfiledestinationoptions ();
(Diskfiledestinationoptions) Doc. exportoptions. destinationoptions). diskfilename = fname; // server. mappath ("finereport ");
Doc. Export ();
Response. clearcontent ();
Response. clearheaders ();
Response. contenttype = "application/pdf ";
// Response. contenttype = "application/MSWord ";
Response. writefile (fname );
Response. Flush ();
Response. Close ();

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.