using System;
using System.Collections;
using System.ComponentModel;
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;
using System.IO;
using System.Data.SqlClient;
using UseCrystal.CrystalPush;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace CrystalPush
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
/**//// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
protected System.Web.UI.WebControls.Button btnExport;
protected System.Web.UI.WebControls.Button btnPrint;
protected CrystalDecisions.Web.CrystalReportViewer Crv;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList ddlFormat;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
myReport ReportDoc =
new myReport();
private void Page_Load(
object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
// 在此处放置用户代码以初始化页面
string strProvider = "Server=(local);DataBase=myDatabase;UID=sa;PWD=111";
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
SqlConnection MyConn =
new SqlConnection(strProvider);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
MyConn.Open();
string strSel = "Select * from SaleOfCuntry";
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
SqlDataAdapter MyAdapter =
new SqlDataAdapter(strSel,MyConn);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
DataSet1 ds =
new DataSet1();
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
MyAdapter.Fill(ds,"SaleOfCuntry");
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.SetDataSource(ds);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
Crv.ReportSource = ReportDoc;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
InitializeComponent();
base.OnInit(e);
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.btnExport.Click +=
new System.EventHandler(
this.btnExport_Click);
this.btnPrint.Click +=
new System.EventHandler(
this.btnPrint_Click);
this.Load +=
new System.EventHandler(
this.Page_Load);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
}
#endregion![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
private void btnExport_Click(
object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts =
new CrystalDecisions.Shared.DiskFileDestinationOptions();
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
switch (ddlFormat.SelectedItem.Text)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
case "Rich Text (RTF)":
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;
//
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
DiskOpts.DiskFileName = "c:\\Output.rtf";
//
break;
case "Portable Document (PDF)":
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
//
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
DiskOpts.DiskFileName = "c:\\Output.pdf";
//
break;
case "MS Word (DOC)":
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
//
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
DiskOpts.DiskFileName = "c:\\Output.doc";
//
break;
case "MS Excel (XLS)":
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
//
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
DiskOpts.DiskFileName = "c:\\Output.xls";
//
break;
default:
break;
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.ExportOptions.DestinationOptions = DiskOpts;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.Export();
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
}
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
private void btnPrint_Click(
object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![ContractedSubBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/f2671b7f42ce505d9bf55a7a0ca257fb.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
// 指定打印机名称,这里是网络工作站Gigi上的打印机Hp Jet 6
string strPrinterName;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
strPrinterName = @"Canon Bubble-Jet BJC-210SP";
// 设置打印页边距
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
PageMargins margins;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
margins = ReportDoc.PrintOptions.PageMargins;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
margins.bottomMargin = 250;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
margins.leftMargin = 350;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
margins.rightMargin = 350;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
margins.topMargin = 450;
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.PrintOptions.ApplyPageMargins(margins);
//应用打印机名称
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.PrintOptions.PrinterName = strPrinterName;
// 打印 // 打印报表。将 startPageN 和 endPageN
// 参数设置为 0 表示打印所有页。
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ReportDoc.PrintToPrinter(1,
false,0,0);
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
}
![ExpandedSubBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
}
![ExpandedBlockEnd.gif](https://i-blog.csdnimg.cn/blog_migrate/4fd96b3cf02f4c7b5c8964ac8167f7af.gif)
}