水晶报表导出为word,excel和pdf格式

 

  1 using  System;
  2 using  System.Collections;
  3 using  System.ComponentModel;
  4 using  System.Data;
  5 using  System.Diagnostics;
  6 using  System.Web;
  7 using  System.Web.Services;
  8 using  System.Web.UI.WebControls;
  9 using  CrystalDecisions.Shared;
 10 using  CrystalDecisions.CrystalReports.Engine;
 11 using  System.IO;
 12 using  System.Data.OleDb;
 13 using  System.Web.UI;
 14
 15 namespace  WorkWebService
 16 {
 17 /**//// <summary>
 18 /// PrintService 的摘要说明。
 19 /// </summary>
 20 /// 

 21 [WebService (Namespace="http://LocalHost/WorkWebService/", Description="A service displaying catalogs of Deepthoughts Publications")]
 22 public class PrintService : System.Web.Services.WebService
 23  
 24 {    
 25  private CrystalDecisions.CrystalReports.Engine.ReportDocument ReportDoc;
 26  private TableLogOnInfo logOnInfo;
 27  private DiskFileDestinationOptions FileOPS;
 28  private ExportOptions ExOPS;
 29  public PrintService()
 30  {
 31   //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
 32   InitializeComponent();
 33   ReportDoc=new ReportDocument();
 34   logOnInfo=new TableLogOnInfo();
 35   FileOPS=new DiskFileDestinationOptions();
 36
 37  }

 38
 39  组件设计器生成的代码#region 组件设计器生成的代码
 40  
 41  //Web 服务设计器所必需的
 42  private IContainer components = null;
 43    
 44  /**//// <summary>
 45  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 46  /// 此方法的内容。
 47  /// </summary>

 48  private void InitializeComponent()
 49  {
 50  }

 51
 52  /**//// <summary>
 53  /// 清理所有正在使用的资源。
 54  /// </summary>

 55  protected override void Dispose( bool disposing )
 56  {
 57   if(disposing && components != null)
 58   {
 59    components.Dispose();
 60   }

 61   base.Dispose(disposing);  
 62  }

 63  
 64  #endregion

 65
 66  // WEB 服务示例
 67  // HelloWorld() 示例服务返回字符串 Hello World
 68  // 若要生成,请取消注释下列行,然后保存并生成项目
 69  // 若要测试此 Web 服务,请按 F5 键
 70
 71  [WebMethod]
 72  public string HelloWorld()
 73  {
 74   return "Hello World";
 75  }

 76
 77  /**//// <summary>
 78  /// 导出报表文件为PDF格式
 79  /// </summary>
 80  /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
 81  /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
 82  /// <param name="PDFFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
 83  /// <returns>bool型,成功返回true,失败返回false</returns>

 84  [WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
 85  public bool ExportToPDF(string ReportFile,object ReportDataSource,string PDFFileName)
 86  {
 87   try
 88   {
 89    ReportDoc.Load(ReportFile);
 90    ReportDoc.SetDataSource(ReportDataSource);
 91    FileOPS.DiskFileName=PDFFileName;
 92    ExOPS=ReportDoc.ExportOptions;
 93    ExOPS.DestinationOptions=FileOPS;
 94          ExOPS.ExportDestinationType=CrystalDecisions.Shared.ExportDestinationType.DiskFile;
 95       ExOPS.ExportFormatType=CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
 96    ReportDoc.Export();                                 
 97    return true;
 98   }

 99   catch
100   {    
101    return false;
102   }
     
103  }

104
105  /**//// <summary>
106  /// 返回PDF文件到用户的IE浏览器中
107  /// </summary>
108  /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
109  /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
110  /// <param name="page">用于显示PDF WebForm</param>
111  /// <returns></returns>

112  [WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
113  public bool ReturnPDF(string ReportFile,object ReportDataSource,System.Web.UI.Page page)
114  {
115   int temp;
116   temp=System.Convert.ToInt32(System.DateTime.Now.Millisecond.ToString());
117   System.Random ra=new System.Random(temp);
118   int TmpNumber=ra.Next();
119   string TmpPDFFileName="c:\\"+System.Convert.ToString(TmpNumber)+".pdf";
120   if (ExportToPDF(ReportFile,ReportDataSource,TmpPDFFileName)==true)
121   {
122    page.Response.ClearContent();
123    page.Response.ClearHeaders();
124    page.Response.ContentType="application/pdf";
125    page.Response.WriteFile(TmpPDFFileName);
126    page.Response.Flush();
127    page.Response.Close();
128    System.IO.File.Delete(TmpPDFFileName);
129    return true;
130   }
   else
131   {
132    return false;
133   }

134  }

135  /**//// <summary>
136  /// 导出报表文件为xls格式
137  /// </summary>
138  /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
139  /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
140  /// <param name="XLSFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
141  /// <returns>bool成功返回true,失败返回false</returns>

142  [WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
143  public bool ExportToXLS(string ReportFile,object ReportDataSource,string XLSFileName)
144  {
145   try
146   {
147    ReportDoc.Load(ReportFile);
148    ReportDoc.SetDataSource(ReportDataSource);
149    FileOPS.DiskFileName=XLSFileName;
150    ExOPS=ReportDoc.ExportOptions;
151    ExOPS.DestinationOptions=FileOPS;
152    ExOPS.ExportDestinationType=CrystalDecisions.Shared.ExportDestinationType.DiskFile;
153    ExOPS.ExportFormatType=CrystalDecisions.Shared.ExportFormatType.Excel;    
154    ReportDoc.Export();                                 
155    return true;
156   }

157   catch
158   {    
159    return false;
160   }
     
161  }

162  [WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
163  public bool DataDownTOcsv(string sql,string filename,string tableheader1,string tableheader2,string columname,int columcount )
164  {
165   try
166   {
167    string strFileToOrg="",strBufferLine="",strBufferLine1=""
168                int i;           
169    strFileToOrg = Server.MapPath("..\\pdf\\"+filename+".csv");
170    StreamWriter strmWriterObj=new StreamWriter(strFileToOrg,false,System.Text.Encoding.Default);//声明写入流对象            
171    OleDbConnection OleDbConnection1=new OleDbConnection();
172    OleDbConnection1.ConnectionString="File Name="+HttpContext.Current.Server.MapPath("conn.udl");
173    OleDbConnection1.Open();
174    OleDbCommand cmdGenFile=new OleDbCommand();
175    cmdGenFile.Connection=OleDbConnection1;
176    cmdGenFile.CommandText=sql;
177    OleDbDataReader drGenFile=cmdGenFile.ExecuteReader();
178    strmWriterObj.WriteLine(tableheader1);
179    strmWriterObj.WriteLine(tableheader2);
180    strmWriterObj.WriteLine(columname);    
181    while (drGenFile.Read())
182    
183     strBufferLine="";
184     strBufferLine1=Convert.ToString(drGenFile.GetValue(0));
185     strBufferLine=strBufferLine1;
186     for (i=1;i<=(columcount-1);i++)
187     {
188      strBufferLine1="";
189      strBufferLine1=Convert.ToString(drGenFile.GetValue(i));
190      strBufferLine=strBufferLine+","+strBufferLine1;
191     }
     
192     strmWriterObj.WriteLine(strBufferLine);    
193    }

194    strmWriterObj.Close();
195    drGenFile.Close();                                
196    return true;
197   }

198   catch
199   {    
200    return false;
201   }
     
202  }

203
204 }

205}

转载于:https://www.cnblogs.com/LiJun027/archive/2007/05/23/756788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
水晶报表一个功能强大的表工具,现在已经被Microsoft Visual Studio 2005(下文以VS2005简称)集成在一起。喜欢水晶报表的朋友可以方便使用了。我把水晶报表在vs2005的使用方法总结一下,供大家参考。 首先介绍一下我用的软件环境:Microsoft Visual Studio 2005;Microsoft SQL Server 2005 【数据用例】 服务器:SQLEXPRESS 数据库名:Test 数据库表:T 数据: 图1 【说明】 水晶报表在应用时分两种方法,分别是拉模式(PULL)、推模式(PUSH)。拉模式:在水晶报表生成时的数据源是从水晶报表文件中的SQL语句从数据库中提取的,在编程时不用重写SQL语句,但要加上登录信息(具体方法,后面介绍)。推模式:在水晶报表生成时的数据源,是用编程时重写水晶报表中SQL语句而生成的dataset对像。也就是说,推模式是用dataset组装水晶报表水晶报表组件介绍。水晶报表在VS2005中有两种组件,在WEB项目是分别是CrystalReportSource,CrystalReportViewer。在FORM项目里是分别是crystalReport,CrystalReportViewer。 CrystalReportSource,crystalReport是水晶报表的数据提供者;CrystalReportViewer是水晶报表的浏览器。另外还要介绍一下水的表的文件是以rpt为扩展名的文件,该文件可以用VS2005生成。 下面分别介绍具体操作方法: 拉模式(PULL): 在拉模式中如要在水晶报表中的SQL语句加上条件参数时要用{?参数名}方式给出。例:“Select T1, T2, T3 FROM T Where T1='{?parm}'” parm就是参数名 以下例子中所用到的水晶报表文件中使用的SQL语句是“Select T1, T2, T3 FROM T Where T1='{?parm}'” parm就是参数名。 【WEB方式下】 using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; ///<summary> ///功能:拉模式提取水晶报表 ///个人主页:http://www.dzend.com/ ///</summary> ///<param ></param> ///<param ></param> protected void Button_pull_Click(object sender, EventArgs e) { // CrystalReport.rpt是水晶报表文件的名称;CrystalReportSource1是从工具箱加到页面上的水晶报表数据源对像。 CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt")); // SetDatabaseLogon 拉模式中必须用这个方法来设置登录信息,参数一:用户名;参数二:密码;参数三:服务器;参数四:数据库名 CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "123456", @"SYWZSWL\SQLEXPRESS", "Test"); //给水晶报表传参数,参数一:是参数名,参数二:参数值; CrystalReportSource1.ReportDocument.SetParameterValue("Title", "这是一个测试表"); CrystalReportSource1.ReportDocument.SetParameterValue("Parm", "1"); //绑定水晶报表数据源。 CrystalReportSource1.DataBind(); // CrystalReportViewer1是水晶报表浏览器,下面是给该浏览器赋上对像 CrystalReportViewer1.ReportSource = CrystalReportSource1; CrystalReportViewer1.DataBind(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值