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;
![](/Images/OutliningIndicators/None.gif)
namespace WebApplication1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected System.Data.SqlClient.SqlConnection sqlConn;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Web.UI.WebControls.Button btnGo;
protected CrystalDecisions.Web.CrystalReportViewer cryViewPubs;
![](/Images/OutliningIndicators/InBlock.gif)
private CryAuthors oRpt = null;
private CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts;
private void Page_Load(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在此处放置用户代码以初始化页面
//实例化架构
DSYoyo ds = new DSYoyo();
sqlDataAdapter1.Fill(ds);
//实例化报表
oRpt = new CryAuthors();
oRpt.SetDataSource(ds);
//设置报表viewer源
cryViewPubs.ReportSource = oRpt;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.sqlConn = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
//
// sqlConn
//
this.sqlConn.ConnectionString = "workstation id=YEKD;packet size=4096;integrated security=SSPI;data source=".";per" +
"sist security info=False;initial catalog=pubs";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new System.Data.Common.DataTableMapping("Table", "authors", new System.Data.Common.DataColumnMapping[]
{
new System.Data.Common.DataColumnMapping("au_id", "au_id"),
new System.Data.Common.DataColumnMapping("au_lname", "au_lname"),
new System.Data.Common.DataColumnMapping("au_fname", "au_fname")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT au_id, au_lname, au_fname FROM authors";
this.sqlSelectCommand1.Connection = this.sqlConn;
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
this.Load += new System.EventHandler(this.Page_Load);
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
private void btnGo_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fileCreat("doc");
}
public void fileCreat(string cFileType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (cFileType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "pdf":
//pdf文件格式
DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
oRpt.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
DiskOpts.DiskFileName = Server.MapPath("\WebApplication1\Output.pdf"); //将导出文件写入web目录,必须为WebApplication1文件夹设置asp.net的完全控制权限
oRpt.ExportOptions.DestinationOptions = DiskOpts;
oRpt.Export(); //文件导出
Session["fileTypeFlag"] = "1";
//LinkButton1_Click(this, new EventArgs()); //触发//LinkButton1_Click事件
![](/Images/OutliningIndicators/InBlock.gif)
break;
case "doc":
//Word文件格式
DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
![](/Images/OutliningIndicators/InBlock.gif)
oRpt.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.WordForWindows;
![](/Images/OutliningIndicators/InBlock.gif)
DiskOpts.DiskFileName = Server.MapPath("\WebApplication1\Output.doc");
![](/Images/OutliningIndicators/InBlock.gif)
oRpt.ExportOptions.DestinationOptions = DiskOpts;
![](/Images/OutliningIndicators/InBlock.gif)
oRpt.Export();
![](/Images/OutliningIndicators/InBlock.gif)
Session["fileTypeFlag"] = "2";
![](/Images/OutliningIndicators/InBlock.gif)
//LinkButton1_Click(this, new EventArgs());
![](/Images/OutliningIndicators/InBlock.gif)
break;
case "excel":
//Excel文件格式
DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
![](/Images/OutliningIndicators/InBlock.gif)
oRpt.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
![](/Images/OutliningIndicators/InBlock.gif)
DiskOpts.DiskFileName = Server.MapPath("\WebApplication1\Output.xls");
![](/Images/OutliningIndicators/InBlock.gif)
oRpt.ExportOptions.DestinationOptions = DiskOpts;
![](/Images/OutliningIndicators/InBlock.gif)
oRpt.Export();
![](/Images/OutliningIndicators/InBlock.gif)
Session["fileTypeFlag"] = "3";
![](/Images/OutliningIndicators/InBlock.gif)
//LinkButton1_Click(this, new EventArgs());
break;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
}
![](/Images/OutliningIndicators/None.gif)