<%
@ Register TagPrefix="cr" Namespace="CrystalDecisions.Web" Assembly="CrystalDecisions.Web, Version=10.0.3300.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
%>
<% @ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" Inherits="CrystalPush.WebForm1" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title >WebForm1 </ title >
< meta content ="Microsoft Visual Studio 7.0" name ="GENERATOR" >
< meta content ="C#" name ="CODE_LANGUAGE" >
< meta content ="JavaScript" name ="vs_defaultClientScript" >
< meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< FONT face ="宋体" >
< CR:CRYSTALREPORTVIEWER id ="Crv" style ="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 54px" runat ="server" Height ="50px" Width ="350px" HasZoomFactorList ="False" HasDrillUpButton ="False" HasGotoPageButton ="False" HasPageNavigationButtons ="False" HasSearchButton ="False" ></ CR:CRYSTALREPORTVIEWER >
< asp:Button id ="btnPrint" style ="Z-INDEX: 103; LEFT: 548px; POSITION: absolute; TOP: 15px" runat ="server" Width ="78px" Text ="打印" ></ asp:Button >
< asp:Button id ="btnExport" style ="Z-INDEX: 102; LEFT: 434px; POSITION: absolute; TOP: 14px" runat ="server" Width ="78px" Text ="导出" ></ asp:Button >
< asp:Label id ="Label1" style ="Z-INDEX: 104; LEFT: 26px; POSITION: absolute; TOP: 19px" runat ="server" >请选择导出格式: </ asp:Label >
< asp:DropDownList id ="ddlFormat" style ="Z-INDEX: 105; LEFT: 188px; POSITION: absolute; TOP: 16px" runat ="server" >
< asp:ListItem Value ="Rich Text (RTF)" >Rich Text (RTF) </ asp:ListItem >
< asp:ListItem Value ="Portable Document (PDF)" >Portable Document (PDF) </ asp:ListItem >
< asp:ListItem Value ="MS Word (DOC)" >MS Word (DOC) </ asp:ListItem >
< asp:ListItem Value ="MS Excel (XLS)" >MS Excel (XLS) </ asp:ListItem >
</ asp:DropDownList ></ FONT ></ form >
</ body >
</ HTML >
<% @ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" Inherits="CrystalPush.WebForm1" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title >WebForm1 </ title >
< meta content ="Microsoft Visual Studio 7.0" name ="GENERATOR" >
< meta content ="C#" name ="CODE_LANGUAGE" >
< meta content ="JavaScript" name ="vs_defaultClientScript" >
< meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< FONT face ="宋体" >
< CR:CRYSTALREPORTVIEWER id ="Crv" style ="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 54px" runat ="server" Height ="50px" Width ="350px" HasZoomFactorList ="False" HasDrillUpButton ="False" HasGotoPageButton ="False" HasPageNavigationButtons ="False" HasSearchButton ="False" ></ CR:CRYSTALREPORTVIEWER >
< asp:Button id ="btnPrint" style ="Z-INDEX: 103; LEFT: 548px; POSITION: absolute; TOP: 15px" runat ="server" Width ="78px" Text ="打印" ></ asp:Button >
< asp:Button id ="btnExport" style ="Z-INDEX: 102; LEFT: 434px; POSITION: absolute; TOP: 14px" runat ="server" Width ="78px" Text ="导出" ></ asp:Button >
< asp:Label id ="Label1" style ="Z-INDEX: 104; LEFT: 26px; POSITION: absolute; TOP: 19px" runat ="server" >请选择导出格式: </ asp:Label >
< asp:DropDownList id ="ddlFormat" style ="Z-INDEX: 105; LEFT: 188px; POSITION: absolute; TOP: 16px" runat ="server" >
< asp:ListItem Value ="Rich Text (RTF)" >Rich Text (RTF) </ asp:ListItem >
< asp:ListItem Value ="Portable Document (PDF)" >Portable Document (PDF) </ asp:ListItem >
< asp:ListItem Value ="MS Word (DOC)" >MS Word (DOC) </ asp:ListItem >
< asp:ListItem Value ="MS Excel (XLS)" >MS Excel (XLS) </ asp:ListItem >
</ asp:DropDownList ></ FONT ></ form >
</ body >
</ HTML >
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
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
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;
myReport ReportDoc = new myReport();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strProvider = "Server=(local);DataBase=myDatabase;UID=sa;PWD=111";
SqlConnection MyConn = new SqlConnection(strProvider);
MyConn.Open();
string strSel = "Select * from SaleOfCuntry";
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
DataSet1 ds = new DataSet1();
MyAdapter.Fill(ds,"SaleOfCuntry");
ReportDoc.SetDataSource(ds);
Crv.ReportSource = ReportDoc;
}
Web Form Designer generated code
private void btnExport_Click(object sender, System.EventArgs e)
{
CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
ReportDoc.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
switch (ddlFormat.SelectedItem.Text)
{
case "Rich Text (RTF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;//
DiskOpts.DiskFileName = "c:\\Output.rtf";//
break;
case "Portable Document (PDF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;//
DiskOpts.DiskFileName = "c:\\Output.pdf";//
break;
case "MS Word (DOC)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;//
DiskOpts.DiskFileName = "c:\\Output.doc";//
break;
case "MS Excel (XLS)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;//
DiskOpts.DiskFileName = "c:\\Output.xls";//
break;
default:
break;
}
ReportDoc.ExportOptions.DestinationOptions = DiskOpts;
ReportDoc.Export();
}
private void btnPrint_Click(object sender, System.EventArgs e)
{
// 指定打印机名称,这里是网络工作站Gigi上的打印机Hp Jet 6
string strPrinterName;
strPrinterName = @"Canon Bubble-Jet BJC-210SP";
// 设置打印页边距
PageMargins margins;
margins = ReportDoc.PrintOptions.PageMargins;
margins.bottomMargin = 250;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 450;
ReportDoc.PrintOptions.ApplyPageMargins(margins);
//应用打印机名称
ReportDoc.PrintOptions.PrinterName = strPrinterName;
// 打印 // 打印报表。将 startPageN 和 endPageN
// 参数设置为 0 表示打印所有页。
ReportDoc.PrintToPrinter(1, false,0,0);
}
}
}
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
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
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;
myReport ReportDoc = new myReport();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strProvider = "Server=(local);DataBase=myDatabase;UID=sa;PWD=111";
SqlConnection MyConn = new SqlConnection(strProvider);
MyConn.Open();
string strSel = "Select * from SaleOfCuntry";
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
DataSet1 ds = new DataSet1();
MyAdapter.Fill(ds,"SaleOfCuntry");
ReportDoc.SetDataSource(ds);
Crv.ReportSource = ReportDoc;
}
Web Form Designer generated code
private void btnExport_Click(object sender, System.EventArgs e)
{
CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
ReportDoc.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
switch (ddlFormat.SelectedItem.Text)
{
case "Rich Text (RTF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.RichText;//
DiskOpts.DiskFileName = "c:\\Output.rtf";//
break;
case "Portable Document (PDF)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;//
DiskOpts.DiskFileName = "c:\\Output.pdf";//
break;
case "MS Word (DOC)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;//
DiskOpts.DiskFileName = "c:\\Output.doc";//
break;
case "MS Excel (XLS)":
ReportDoc.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;//
DiskOpts.DiskFileName = "c:\\Output.xls";//
break;
default:
break;
}
ReportDoc.ExportOptions.DestinationOptions = DiskOpts;
ReportDoc.Export();
}
private void btnPrint_Click(object sender, System.EventArgs e)
{
// 指定打印机名称,这里是网络工作站Gigi上的打印机Hp Jet 6
string strPrinterName;
strPrinterName = @"Canon Bubble-Jet BJC-210SP";
// 设置打印页边距
PageMargins margins;
margins = ReportDoc.PrintOptions.PageMargins;
margins.bottomMargin = 250;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 450;
ReportDoc.PrintOptions.ApplyPageMargins(margins);
//应用打印机名称
ReportDoc.PrintOptions.PrinterName = strPrinterName;
// 打印 // 打印报表。将 startPageN 和 endPageN
// 参数设置为 0 表示打印所有页。
ReportDoc.PrintToPrinter(1, false,0,0);
}
}
}
本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/25/261677.html,如需转载请自行联系原作者