NEWS

[C#] Hướng dẫn ghi file trực tiếp excel sử dụng RTD (Real Time Data) Server

[C#] Hướng dẫn ghi file trực tiếp excel sử dụng RTD (Real Time Data) Server
Đăng bởi: Thảo Meo - Lượt xem: 7066 08:25:56, 08/11/2018DEVEXPRESS   In bài viết

Xin chào các bạn bài viết hôm nay, mình sẽ hướng dẫn các bạn cách tạo RTD (Real Time Data) Server cho Excel bằng ngôn ngữ lập trình C#.

[C#] Tutorial create RTD (Real Time Data) server Excel

Trong lập trình ứng dụng, các bạn muốn khi dữ liệu ở database thay đổi, thì file excel của bạn sẽ đồng thời cập nhật dữ liệu mới nhất và hiển thị trực tiếp trên file Excel.

Trong Visual Studio, thư viện Excel Interop giúp ta dễ dàng tạo một RTD Server cho Excel.

rtd_server_excel

Giao diện demo load dữ liệu trong Excel sử dụng RTD server.

Đầu tiên, các bạn cần import thư viện Microsoft.Office.Interop.Excel

Các bạn có thể cài đặt từ Nuget:

Ở dưới này mình đang sử dụng phiên bản Office 2016.

PM> Install-Package Microsoft.Office.Interop.Excel -Version 15.0.4795.1000
  • Tiếp theo, khi build ứng dụng các bạn cần tạo plugin cho excel, vì vậy các bạn cần cấu hình trong Project Setting như hình ảnh bên dưới (và khi build các bạn cần chạy Visual Studio dưới quyền Administrator) thì ứng dụng mới có thể đăng ký được.
cd "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
"regasm" "$(TargetPath)" /codebase

Trong bài viết này, mình đang sử dụng thư viện Framework 4.0

config_rtd_server

Trong Sqlserver Database các bạn import dữ liệu demo của mình vào:

CREATE TABLE tbl_demo ( [name] nvarchar(50), [toan] int, [ly] int, [hoa] int )
INSERT INTO tbl_demo
VALUES
( N'Nguyễn Thảo', 30, 20, 50 ), 
( N'Cái Trí Minh', 60, 20, 10 ), 
( N'Võ Sơn Băng', 19, 21, 60 ), 
( N'Hoàng Thị Thảo', 40, 20, 40 ), 
( N'Thảo Meo', 20, 30, 50 ), 
( N'Fernaldo Torres', 80, 10, 10 ), 
( N'Hoàng Dược Sư', 40, 10, 50 ), 
( N'Quách Tĩnh', 10, 50, 40 ), 
( N'Hoàng Dung', 60, 5, 45 )

Full Source code create RTD Server Excel C#:

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

using System.Threading.Tasks;
using System.Timers;

namespace RtdServer
{
    [
    Guid("9e55d78e-0f29-4d7e-884d-5d2f8e7d22ea"),
    ProgId("RtdServer.laptrinhvb")
    ]
    public class RtdDemoServer : IRtdServer
    {
        private IRTDUpdateEvent _callback;
        private Timer _timer;     
        private Dictionary<int, string> _topics;
        sqlserver provider = new sqlserver(@"NGUYENTHAO\NGUYENTHAO", "sa", "minh123", "master");
        // System.Data.DataTable table;

        public RtdDemoServer()
        {
          
        }


        public int ServerStart(IRTDUpdateEvent callback)
        {
            Console.WriteLine("RtdServer Server Start");
            _callback = callback;
            _timer = new Timer();
            _timer.Elapsed += new ElapsedEventHandler(TimerEventHandler);
            _timer.Interval = 1000;
            _topics = new Dictionary<int, string>();
            return 1;
        }


        public void ServerTerminate()
        {
            if (null != _timer)
            {
                _timer.Dispose();
                _timer = null;
            }
        }


        public int Heartbeat()
        {
            return 1;
        }

        public object ConnectData(int topicId,
                          ref Array strings,
                          ref bool newValues)
        {
           
            string symbol = strings.GetValue(0).ToString();
            _topics.Add(topicId, symbol);
            _timer.Start();
            return GetData(symbol);
        }

        public void DisconnectData(int topicId)
        {            
            _topics.Remove(topicId);
        }
        private void TimerEventHandler(object sender,
                                         EventArgs args)
        {
            _timer.Stop();
            _callback.UpdateNotify();
        }

      
        public Array RefreshData(ref int topicCount)
        {
            string symbol;
            object[,] data = new object[10, 10];          
            
            for (int i = 0; i < 4; i++)
            {
                data[0, i] = i;
                _topics.TryGetValue(i, out symbol);
                data[1, i] = GetData(symbol);
                topicCount = i + 1;
                
            }   

        
            
            _timer.Start();
            return data;
        }

      
        private string GetData(string symbol)
        {
            try
            {
                
                var table = provider.ExecuteQuery("SELECT TOP 1 *  FROM dbo.tbl_demo ORDER BY NEWID()");

                return table.Rows[0][symbol].ToString();
             
            }
            catch (Exception ex)
            {

                return ex.Message +"";
            }
         

        }


    }
}

Sau khi các bạn code xong, các bạn vào Build => Rebuild RTDServer.

Nếu giao diện hiển thị như hình bên dưới là bạn đã đăng ký thành công.

ef8da759a0cc409219dd

Tiếp tục vào, trong file excel các bạn gõ câu lệnh sau:

=RTD("rtdserver.laptrinhvb",,"name")

rtd_server_csharp

Have Fun :)

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn ghi file trực tiếp excel sử dụng RTD (Real Time Data) Server
Đăng bởi: Thảo Meo - Lượt xem: 7066 08:25:56, 08/11/2018DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.