解決ASP.NET CrystalReports 用戶限制的處理方法

由於ASP.NET CrystalReports不支援多用戶同時在線,此時可以用以下的方法來處理:

把WEB前端列印傳遞到WINDOWS後端來處理,這樣只有一個USER在使用CrystalReports.

 

一.WindowsService

1.使用VS2008新建項目:WindowsService

2.在Service1.cs上添加時間控件,一定是System.Timers.Timer,不是System.Windows.Forms.Timer,因為是WindowsService程式.

3.cs文件

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;

 

protected override void OnStart(string[] args)
        {
            Logs.Save("Start svervice.");
            int i = 1;
            try
            {
                timer1.Enabled = false;
                i = Convert.ToInt32(ConfigurationManager.AppSettings["RunTime"].ToString().Trim());
            }
            catch(Exception ex)
            {
                Logs.Save("Start fail," + ex.Message);
            }
            finally
            {
                timer1.Interval = 1000 * i;
                timer1.Enabled = true;
            }
        }

        protected override void OnStop()
        {
            Logs.Save("Stop svervice.");
            timer1.Enabled = false;
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer1.Enabled = false;
            try
            {
                Logs.Save("----------------------");
                Logs.Save("開始創建");
                PrintReport();
                Logs.Save("創建完成");
            }
            catch (Exception ex)
            {
                Logs.Save(ex.Message);
            }

            timer1.Enabled = true;
        }

        private void PrintReport()
        {
            Model.dsRpt001.EMPLOYEEDataTable dt = new Model.dsRpt001.EMPLOYEEDataTable();
            Model.dsRpt001TableAdapters.EMPLOYEETableAdapter myGet = new Model.dsRpt001TableAdapters.EMPLOYEETableAdapter();
            dt = myGet.GetData();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sEmplid = dt.Rows[i]["EMPLID"].ToString();
                string filePath = Application.StartupPath + @"/TEMP/" + System.DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);
                string fileName = filePath + @"/" + System.DateTime.Now.ToString("HHmmssffffff") + ".PDF";
                if (File.Exists(fileName))
                    fileName = filePath + @"/" + System.DateTime.Now.ToString("HHmmssfffffff") + ".PDF";

                DataTable _dt = new DataTable();
                _dt = dt.Clone();
                DataRow[] rows = dt.Select("EMPLID='" + sEmplid + "'");
                foreach (DataRow row in rows)
                    _dt.ImportRow(row);
                DataSet myDS = new DataSet();
                myDS.Merge(_dt);

                ReportDocument doc = new ReportDocument();
                string reportPath = Application.StartupPath + (@"/RPT/RPT001.rpt");
                doc.Load(reportPath);
                doc.SetDataSource(myDS);

                ExportOptions crExportOptions = new ExportOptions();
                DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();
                crDiskFileDestinationOptions.DiskFileName = fileName;
                crExportOptions = doc.ExportOptions;
                crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
                crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;

                doc.Export();

                doc.Close();
                doc.Dispose();
                _dt.Dispose();
                myDS.Dispose();
            }
            dt.Dispose();
        }

4.添加安裝程式ProjectInstaller.cs

serviceProcessInstaller1: Account-->LocalSystem  

serviceInstaller1: Description-->服務描述;DisplayName/ServiceName-->服務名,一般設置相同就可,最好用英文;StartType-->Automatic

 

二.WebService

1.創建WebService:Service1.asmx

[WebMethod]
        public void DownloadFile(string _filePath)
        {
            try
            {
                string filePath = Server.MapPath(_filePath);
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Exists)
                {
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ClearHeaders();
                    HttpContext.Current.Response.Buffer = false;
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.ASCII));
                    HttpContext.Current.Response.AppendHeader("Content-Length", fileInfo.Length.ToString());
                    HttpContext.Current.Response.WriteFile(fileInfo.FullName);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.Close();                   
                }
                else
                { //文件不存在
                    HttpContext.Current.Response.Write("This file is not exists!");
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("Download error," + ex.Message);
            }
            finally
            {
                HttpContext.Current.Response.End();
            }
        }

2.創建WebForm:WebForm1.aspx

        protected void Button1_Click(object sender, EventArgs e)
        {

            Service1 myService = new Service1();
            myService.DownloadFile(@"下載文件路徑");
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值