2025年7月29日 星期二

[研究]ASP.NET WebForm 網站 iCal.Net 5.1.0 行事曆套件試用

[研究]ASP.NET WebForm 網站 iCal.Net 5.1.0 行事曆套件試用

2025-07-29

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

https://github.com/ical-org/ical.net

https://www.nuget.org/packages/Ical.Net

Ical.Net 是一套 .NET 函式庫,用來建立、讀取與處理 iCalendar (.ics) 格式的行事曆資料。這是 RFC 5545 標準格式,支援跨平台行事曆應用程式如 Google Calendar、Apple Calendar、Outlook 等。

Ical.Net 是目前 .NET 環境下最推薦的 iCalendar 處理函式庫,尤其適用於:

  • ASP.NET WebForm / MVC / Web API 專案
  • 無法使用 Office Interop 的後端或雲端服務
  • 需要完整 RFC 5545 支援的情境(如排程、會議邀請)

如果你想在 ASP.NET Web 應用程式中產生 .ics 讓使用者下載,Ical.Net 幾乎是最佳選擇。



WebApplication1

正在安裝:

System.Buffers.4.6.0
System.Numerics.Vectors.4.6.0
System.Runtime.CompilerServices.Unsafe.6.1.0
NodaTime.3.2.2
System.Memory.4.6.0
Portable.System.DateTimeOnly.9.0.0
Ical.Net.5.1.0




********************************************************************************

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>產生 iCalendar (.ics) 範例</h2>
            <asp:Button ID="btnGenerateICS" runat="server" Text="下載 .ics 檔" 
                OnClick="btnGenerateICS_Click" />
    </form>
</body>
</html>

Default.aspx.cs

using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using Ical.Net.Serialization;
using System;
using System.Text;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGenerateICS_Click(object sender, EventArgs e)
        {
            // 使用 UTC 時間(必須)
            DateTime utcNow = DateTime.UtcNow;
            var start = new CalDateTime(utcNow.AddHours(1)); // 正確
            var end = new CalDateTime(utcNow.AddHours(2));   // 正確

            // 建立事件
            var calendarEvent = new CalendarEvent
            {
                Summary = "測試事件",
                Description = "Ical.Net 測試 .ics",
                Location = "台北市政府",
                DtStart = start,
                DtEnd = end,
                DtStamp = new CalDateTime(utcNow),
                Uid = Guid.NewGuid().ToString()
            };

            // 建立行事曆並加入事件
            var calendar = new Ical.Net.Calendar();
            calendar.Events.Add(calendarEvent);

            // 序列化為 .ics
            var serializer = new CalendarSerializer();
            string serializedCalendar = serializer.SerializeToString(calendar);

            // 輸出為 .ics 檔案
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "text/calendar";
            Response.ContentEncoding = Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=event.ics");
            Response.Write(serializedCalendar);
            Response.End();
        }
    }
}


同類元件比較

元件 優點 缺點
Ical.Net 🌟開源、活躍維護中
🌟支援完整 RFC 5545 標準
🌟可寫入和解析 .ics
⚠️學習曲線稍高
⚠️缺乏 GUI 控制元件
DDay.iCal(Ical.Net 前身) ✅老牌函式庫,支援 RFC 標準
✅功能完整
❌已停止維護(最後更新 2013)
❌相容性差於新版本 .NET
Microsoft.Office.Interop.Outlook ✅整合 Outlook 強大功能(提醒、同步)
✅可存取 Outlook 所有日曆物件
❌需安裝 Outlook
❌無法部署到伺服器或無 Outlook 的環境
Calendar .NET(UI 控制元件) ✅內建月曆介面
✅可直接與使用者互動
❌僅限 WinForms
❌不支援標準 iCalendar 格式

(完)

相關



沒有留言:

張貼留言