1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

C# Crystal Reports Export to Excel

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.18 MB, 71 trang )


There are situations when we want to export Crystal reports to .xls format

programmatically in C#. In these situations we can use ExportOptions for

export the Crystal Reports to .xls format. Also we have to set

ExcelFormatOptions and ExportFormatType.Excel . In the following example

you can see how to export a Crystal Reports as a Excel format file.

All C# Crystal Reports Tutorial in this website is based on the following

database - crystaldb. So before you begin this section , please take a look at the

database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal

Reports from C# , please take a look at the section step by step tutorial for

creating a Crystal Reports from C#.

In this section we are using our earlier program step by step tutorial for creating

a Crystal Reports from C# for pull data from database to Crystal Reports .

Before we start this section take a look at the step by step tutorial for creating a

Crystal Reports from C# .

Here we are generating a Crystal Report from Product table and export the

report content to an Excel format file.

Select the default form (Form1.cs) you created in C# and drag two buttons

(Button1, Button2 ) and a CrystalReportViewer control to your form.

You have to include CrystalDecisions.CrystalReports.Engine in your C#

Source Code.

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

Copy and paste the following source code and run your C# project



using System;

using System.Windows.Forms;



using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

ReportDocument cryRpt;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

cryRpt = new ReportDocument();

cryRpt.Load(PUT CRYSTAL REPORT PATH

HERE\\CrystalReport1.rpt");

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}

private void button2_Click(object sender, EventArgs e)

{

try

{

ExportOptions CrExportOptions ;

DiskFileDestinationOptions CrDiskFileDestinationOptions

= new DiskFileDestinationOptions();

ExcelFormatOptions CrFormatTypeOptions = new

ExcelFormatOptions();

CrDiskFileDestinationOptions.DiskFileName =

"c:\\csharp.net-informations.xls";

CrExportOptions = cryRpt.ExportOptions;

CrExportOptions.ExportDestinationType =

ExportDestinationType.DiskFile;

CrExportOptions.ExportFormatType =

ExportFormatType.Excel;

CrExportOptions.DestinationOptions =

CrDiskFileDestinationOptions;

CrExportOptions.FormatOptions = CrFormatTypeOptions;

cryRpt.Export();

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

}

}



cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");



The Crystal Reports file path in your project files location, there you can see

CrystalReport1.rpt . So give the full path name of Crystal Reports file like

c:\projects\crystalreports\CrystalReport1.rpt

When you run this program you will get the Excel file (csharp.netinformations.xls) in your computer's C:\

12. Email Crystal Reports from C# Application

The following program describes how to email a Crystal Reports from C# .

All C# Crystal Reports Tutorial in this website is based on the following

database - crystaldb. So before you begin this section , please take a look at the

database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal

Reports from C# , please take a look at the section step by step tutorial for

creating a Crystal Reports from C#.

For email a Crystal Report from C# first we have to export the Crystal Reports

in any of the File Format available in Crystal Reports and then Email it.

In this section we use the previous tutorial C# Crystal Reports Export to PDF

file . So before we start this section please take a look at the tutorial C# Crystal

Reports Export to PDF file.

After export the Crystal Reports as a PDF file format in your disk , the next

step is to email that .pdf file . For that here we are using System.Web.Mail of

C# . We have to provide the necessary information to configuring SmtpMail

client and send the exported file as attachment .

Select the default form (Form1.cs) you created in C# and drag two buttons

(Button1, Button2 ) and a CrystalReportViewer control to your form.

You have to include CrystalDecisions.CrystalReports.Engine in your C#

Source Code.

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;



using System.Web.Mail

Copy and paste the following source code and run your C# project



using

using

using

using

using



System;

System.Windows.Forms;

CrystalDecisions.CrystalReports.Engine;

CrystalDecisions.Shared;

System.Web.Mail;



namespace WindowsApplication1

{

public partial class Form1 : Form

{

ReportDocument cryRpt;

string pdfFile = "c:\\csharp.net-informations.pdf";

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

cryRpt = new ReportDocument();

cryRpt.Load(PUT CRYSTAL REPORT PATH

HERE\\CrystalReport1.rpt");

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

}

private void button2_Click(object sender, EventArgs e)

{

try

{

ExportOptions CrExportOptions ;

DiskFileDestinationOptions CrDiskFileDestinationOptions

= new DiskFileDestinationOptions();

PdfRtfWordFormatOptions CrFormatTypeOptions = new

PdfRtfWordFormatOptions();

CrDiskFileDestinationOptions.DiskFileName = pdfFile;

CrExportOptions = cryRpt.ExportOptions;

CrExportOptions.ExportDestinationType =

ExportDestinationType.DiskFile;

CrExportOptions.ExportFormatType =

ExportFormatType.PortableDocFormat;

CrExportOptions.DestinationOptions =

CrDiskFileDestinationOptions;

CrExportOptions.FormatOptions = CrFormatTypeOptions;

cryRpt.Export();

sendmail();

}



}



}



catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}



private void sendmail()

{

try {

SmtpMail.SmtpServer.Insert(0, "your hostname");

MailMessage Msg = new MailMessage();

Msg.To = "to address here";

Msg.From = "from address here";

Msg.Subject = "Crystal Report Attachment ";

Msg.Body = "Crystal Report Attachment ";

Msg.Attachments.Add(new MailAttachment(pdfFile));

System.Web.Mail.SmtpMail.Send(Msg);

}

catch (Exception ex) {

MessageBox.Show (ex.ToString());

}

}



}



cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");

The Crystal Reports file path in your C# project files location, there you can

see CrystalReport1.rpt . So give the full path name of Crystal Reports file like

c:\projects\crystalreports\CrystalReport1.rpt

Before you run this program , you have to provide the necessary SMTP

information , that is your HOSTNAME , FROM ADDRESS and TO

ADDRESS to the SMTP client.

13. C# Crystal Reports without database

Usually we are using Crystal Reports to fetch data from databases and show it

as a report. Here we are generating a Crystal Reports from C# without using a

database . For generating a Crystal Reports without database , here we are

using a Strongly Typed Dataset and a Data Table of C#.

If you are new to Crystal Reports and do not know how to create Crystal

Reports from C# , please take a look at the section step by step tutorial for

creating a Crystal Reports from C#.



Generating a Strongly Typed DataSet

Create a new C# Project and create a Dataset from Project - Add New Item

Dialogue Box.



Select Dataset from list



Accept the default name DataSet1.xsd .



Create a data table for DataSet1.xsd in C#.



Xem Thêm
Tải bản đầy đủ (.pdf) (71 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×