- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[C#] Hướng dẫn ghi file trực tiếp excel sử dụng RTD (Real Time Data) Server
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.
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
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.
Tiếp tục vào, trong file excel các bạn gõ câu lệnh sau:
=RTD("rtdserver.laptrinhvb",,"name")
Have Fun :)