13 Temmuz 2013 Cumartesi

Excell Add İn Geliştirme (WCF Destekli)

Merhaba Arkadaşlar

İlk Blog Yayınıma Başlıyorum

Konu : Excell Add İn Geliştirme (WCF Destekli)

Amaç:

Excell'in her açılış kapanışını yakalayan ve bunu bir wcf service aracılığıyla txt uzantılı dosyada loglayan sistem yapımı.




Öncelikle Visual Studioyu açıyoruz. New Project menusunden Excell 2007 Add-in i seçiyoruz
























proje oluştuğu ekranda

using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;

using  bloğunda yukardaki bilgileri görebiliriz. 


    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
     
        #endregion
    }

projenin ilk halinde kodlar yukarıda göründüğü gibi oluşmuştur.

Burdaki  AddIn_Startup event i excell projesi çalıştığında tetiklenir.

              AddIn_Shutdown  event i excell projesi kapandığında tetiklenir.

Yapacağımız projedeki amaç: Excell projesi açıldığında ve kapandığında d:\exceltemp.txt dosyasına loglar oluşturmaktır.

ilk olarak ikinci bir proje oluşturup wcf service application oluşturuyoruz.























oluşan projedeki service1.svc.cs dosyasının içindeki kodları

        public void writedata(string _data)
        {
            StreamWriter sw = new StreamWriter(@"D:\ExcelLog.Txt",true );
            try
            {
                sw.WriteLine("Gelen Veri: {0}",   _data);


            }
            catch (Exception)
            {
            }
            finally
            {
                sw.Close();
            }
         
        }

kodlarıyla değiştiriyoruz . burda basit olarak bir streamwriter nesnesi oluşturuluyor ve gelen string datayı belirtilen dosyaya yazıyoruz.

IService1.cs dosyasının içindeki kodlarıda aşağıdaki kodlarla değiştiriyoruz.

    [ServiceContract]
    public interface IService1
    {


        [OperationContract]
        void writedata(string _data);

        // TODO: Add your service operations here
    }

burda Service1.svc.cs dosyasında bulunması gereken yordamları interface olarak tanımlıyoruz

sonrasında wcf app projemizi çalıştırıyor ve açılan ekrandaki










Copy Addres menüsünü seçtiğimizde clipboard a http://localhost:54804/Service1.svc değeri kopyalanır bu proje çalışırken excell addin projemizdeki reference penceresine geçip add service reference ı tıklıyoruz
açılan pencereden


















address alanına kopyaladığımız değeri yapıştırıyoruz ve Go butonuna tıklıyoruz daha sonra yukarda görünen pencere gibi çalışan service app yi görebiliriz. Daha sonra Ok butonuna tıklıyoruz.

ve excell add-in projemizdeki kodları :

    public partial class ThisAddIn
    {
        ServiceReference1.Service1Client svc = new ServiceReference1.Service1Client();
        Stopwatch sw = new Stopwatch();
        TimeSpan ilksaat = new TimeSpan();
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            TimeSpan ts = new TimeSpan();
            ts = TimeSpan.FromTicks(DateTime.Now.Ticks);
            ilksaat = ts;
            Console.WriteLine("Sistem Başladı");
            if (svc.State!=System.ServiceModel.CommunicationState.Opened) svc.Open();
            //svc.writedata(string.Format("Sistemden Çıkış Yapıldı.Toplam Süre: {0}", string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)));
            svc.writedata(string.Format("Sistemden Giriş Yapıldı. Saat:{0}", string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)));
           // if (svc.State != System.ServiceModel.CommunicationState.Opened) svc.Close();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
         

            TimeSpan ts = new TimeSpan( DateTime.Now.Ticks);
            ts = ts - ilksaat;
            if (svc.State != System.ServiceModel.CommunicationState.Opened) svc.Open();
            svc.writedata(string.Format("Toplam Süre: {0}", string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)));
            ts = TimeSpan.FromTicks(DateTime.Now.Ticks);
            svc.writedata(string.Format("Sistemden Çıkış Yapıldı. Saat:{0}", string.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)));
            if (svc.State == System.ServiceModel.CommunicationState.Opened) svc.Close();
            sw.Stop();
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);          
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
        {


        }
     
        #endregion
    }

olarak değiştiriyoruz ve projemizi çalıştırıyoruz. karşımıze excell projesi açılıyor ve bu projeyi kapatıyoruz

kontrol için d sürücüsündeki  ExcelLog.Txt dosyasını açıp kontrol edebilirsiniz.

Projeyi çok basit bir mantıkla oluşturdum siz bunu kendi fikirlerinizle zenginleştirip çok farklı uygulamalar ortaya çıkartabilirsiniz.

Yeni Bir yayında tekrar görüşmek dileğiyle.

Vaktim Olmadığından dolayı hatalar varsa affola

Software Developer

Malik Bakacak

malikbakacak@gmail.com

Hiç yorum yok:

Yorum Gönder