水晶报表导出为word,excel和pdf格式(转载)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
namespace report1
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = " http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        private CrystalDecisions.CrystalReports.Engine.ReportDocument repDoc;
        private TableLogOnInfo logOnInfo;
        private DiskFileDestinationOptions FileOps;
        private ExportOptions Exops;
        public Service1()
        {
            repDoc = new ReportDocument();
            logOnInfo = new TableLogOnInfo();
            FileOps = new DiskFileDestinationOptions();
        }
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        /// <summary>
      /// 导出报表文件为PDF格式
      /// </summary>
      /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
      /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
      /// <param name="PDFFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
      /// <returns>bool型,成功返回true,失败返回false</returns>
            
        [WebMethod]
        public bool ExportTOPDF(string ReportFile, object ReportDataSource, string PDFFileName)
        {
            try
            {
                repDoc.Load(ReportFile);
                repDoc.SetDataSource(ReportDataSource);
                FileOps.DiskFileName = PDFFileName;
              
          
                Exops.ExportDestinationOptions = FileOps;
                Exops.ExportDestinationType  = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
                Exops.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
                repDoc.Export(Exops);

                return true;
            }
            catch
            {
                return false;
            }
        }
        
        /// <summary>
        /// 返回PDF文件到用户的IE浏览器中
        /// </summary>
        /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
        /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
        /// <param name="page">用于显示PDF WebForm</param>
        /// <returns></returns>
        [WebMethod]
        public bool ReturnPDF(string ReportFile, object ReportDataSource, System.Web.UI.Page page)
        {
            int temp;
            temp = System.Convert.ToInt32(System.DateTime.Now.Millisecond.ToString());
            System.Random ra = new Random(temp);
            int tmpNumber=ra.Next();
            string TempPDFFIleName = "c:\\" + Convert.ToString(tmpNumber) + ".pdf";
            if (ExportTOPDF(ReportFile, ReportDataSource, TempPDFFIleName))
            {
                page.Response.ClearContent();
                page.Response.ClearHeaders();
                page.Response.ContentType = "application/pdf";
                page.Response.WriteFile(TempPDFFIleName);
                page.Response.Flush();
                page.Response.Close();
                System.IO.File.Delete(TempPDFFIleName);
                return true;
            }
           else
           {
                return false;
           }        
        }
          
         /// <summary>
          /// 导出报表文件为xls格式
          /// </summary>
          /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
          /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
          /// <param name="XLSFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
          /// <returns>bool成功返回true,失败返回false</returns>
        [WebMethod]
        public bool ExportToXLS(string ReportFile, object ReportDataSource, string PDFFileName)
        {
            try
            {
                repDoc.Load(ReportFile);
                repDoc.SetDataSource(ReportDataSource);
                FileOps.DiskFileName = PDFFileName;

                Exops.ExportDestinationOptions = FileOps;
                Exops.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
                Exops.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
                repDoc.Export(Exops);

                return true;
            }
            catch
            {
                return false;
            }
        }
        [WebMethod]
        public bool DataDownTocsv(string sql, string filename, string tableheader1, string tableheader2, string columname, int columcount)
        {
            try
            {
                string strFileToOrg = "", strBufferLine = "", strBufferLine1 = "";
                int i;
                strFileToOrg = Server.MapPath(filename + ".csv");
                StreamWriter strmWriterObj = new StreamWriter(strFileToOrg, false, System.Text.Encoding.Default);//声明写入流对象  
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "file name=" + HttpContext.Current.Server.MapPath("conn.udl");//定位URL,获取连接字符串,根据需要,可以更改。
                SqlCommand com = new SqlCommand();
                com.CommandText = sql;
                com.Connection = con;
                SqlDataReader drGenFile = com.ExecuteReader();
                strmWriterObj.WriteLine(tableheader1);
                strmWriterObj.WriteLine(tableheader2);
                strmWriterObj.WriteLine(columname);
                while (drGenFile.Read())
                {
                    strBufferLine = "";
                    strBufferLine1 = Convert.ToString(drGenFile.GetValue(0));
                    strBufferLine = strBufferLine1;
                    for (i = 1; i <= (columcount - 1); i++)
                    {
                        strBufferLine1 = "";
                        strBufferLine1 = Convert.ToString(drGenFile.GetValue(i));
                        strBufferLine = strBufferLine + "," + strBufferLine1;
                    }
                    strmWriterObj.WriteLine(strBufferLine);
                }
                strmWriterObj.Close();
                drGenFile.Close();
                return true;
            }
            catch
            {
                return false;
            }          
        }
    }
}

转载于:https://www.cnblogs.com/hendy/archive/2009/11/15/1603442.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值