解決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(@"下載文件路徑");
        }

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页