水晶报表导出为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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值