您的位置:首页 > 编程语言

WORD文档导出 水晶报表代码加载

2007-07-09 11:53 531 查看
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 CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;

namespace lianxiaspnet.office
{
/// <summary>
/// test_export 的摘要说明。
/// </summary>
public class test_export : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;

ReportDocument crReportDocument = new ReportDocument();
ExportOptions crExportOptions = new ExportOptions();
DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
/*TableLogOnInfo log;
ReportDocument rd;
ExportOptions eo;
DiskFileDestinationOptions dd;
eo = new ExportOptions();
dd = new DiskFileDestinationOptions();

log = new TableLogOnInfo();
log.ConnectionInfo.ServerName = "lanwei";
log.ConnectionInfo.Password="11111111";
log.ConnectionInfo.DatabaseName="lianxi";
log.ConnectionInfo.UserID = "sa";

rd = new ReportDocument();

string path = Server.MapPath("Report4.rpt");

rd.Load(path);

//rd.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
rd.Database.Tables[0].ApplyLogOnInfo(log);
//CrystalReportViewer1.ReportSource = rpdMy0;
CrystalReportViewer1.ReportSource= rd;*/
rpt_load("11111111","sa","lanwei","lianxi","Report4.rpt",CrystalReportViewer1);

}

#region 在页面代码加载报表

private void rpt_load(string logpassword,string logname,string server,string databasename,string rptname,CrystalDecisions.Web.CrystalReportViewer cr)
{
TableLogOnInfo log = new TableLogOnInfo();

log.ConnectionInfo.ServerName = server;
log.ConnectionInfo.Password=logname;
log.ConnectionInfo.DatabaseName=databasename;
log.ConnectionInfo.UserID = logpassword;

//string path = Server.MapPath(rptname);
string path = Server.MapPath(".")+"//" + "rpt//"+rptname;
crReportDocument.Load(path);

crReportDocument.Database.Tables[0].ApplyLogOnInfo(log);
cr.ReportSource= crReportDocument;
}

#endregion

#region 导出为.doc格式
private void rpt_expot()
{

string ExportPath;
ExportPath = Server.MapPath(".")+"//" + "Exported//";
if (Directory.Exists(ExportPath) == false) Directory.CreateDirectory(Server.MapPath(".")+"//" + "Exported//");

//ExportPath = Request.PhysicalApplicationPath + "Exported//";
//if (Directory.Exists(ExportPath) == false) Directory.CreateDirectory(Request.PhysicalApplicationPath + "Exported//");

crExportOptions = crReportDocument.ExportOptions;

string Fname;
string filename;
filename = GetUniqueString();
Fname= ExportPath + filename+ ".doc";
crDiskFileDestinationOptions.DiskFileName =Fname;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
try
{
// Export the report
crReportDocument.Export();
//Response.Write("<script lange成功输出!");
Response.Write("<SCRIPT language=JavaScript>alert('成功输出Word文档!.');</Script>");

}
catch (Exception err)
{
Response.Write("<BR>");
Response.Write(err.Message.ToString());
}

}
#endregion

#region GetUniqueString获取一个不重复的文件名
public static string GetUniqueString()
{
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
#endregion

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
rpt_expot();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: