發表文章

[.NET Core 6] NPOI 的 Cell 使用自訂前景色與背景色

圖片
POI 是產生 xlsx 檔是很方便的工具,網路上已經有很多範例和分享,最近練習想從前端傳送 Cell 的顏色到後端,來產生對應顏色格的檔案,搜集整理如下,以 .NET Core 6 實作,只筆記 model 中的邏輯。 最基本的前景色 有前景色和背景色 使用 RGB 做前景與背景 使用十六進位字串做前景背景 最基本的前景色 下面示範藍色字,就是新增一個 font ,設定 font 的 color 為藍色。 XSSFWorkbook wkBook = new XSSFWorkbook(); ISheet sheet = wkBook.CreateSheet("測試"); IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue("style0"); row = sheet.CreateRow(1); ICellStyle style0 = wkBook.CreateCellStyle(); IFont font0 = wkBook.CreateFont(); // 新增 IFont font0.Color = HSSFColor.Blue.Index; // 設定 font 的 color 即前景色 style0.SetFont(font0); // 使用 HSSFColor 的 Index 設定 ICell cell0 = row.CreateCell(0); cell0.CellStyle = style0; cell0.SetCellValue("測試藍字"); 有前景色和背景色 背景色的做法有點特別,雖然 ICellStyle 物件有 FillBackgroundColor 屬性,但是背景色其實是用 FillForegroundColor 搭配 FillPattern.SolidForeground 來填滿的。 XSSFWorkbook wkBook = new XSSFWorkbook(); ISheet sheet...

[ASP.NET Core 6 MVC] 使用 Dapper

圖片
這篇是參考自 Code Maze : Using Dapper with ASP.NET Core Web API 的教學,練習後自己做了一個筆記。以下使用 Visual Studio 2022,建立專案為 ASP.NET Core Web 應用程式 (Model-View-Controller)使用 .NET 6.0 架構,方案名稱為 DemoDapper。以下會講得比較瑣碎詳細,因為以前開始學 .NET 4.8 MVC 時欠缺這樣 step by step 的懶人包,希望可以幫到一些初學者。(以下忽略 null 值判斷與 try catch 處理) 安裝 Dapper 套件 取得連線字串和設定 appsettings.json 新增介面與類別檔案 Controller 與 View 安裝 Dapper 套件 在「方案總管」視窗,對「相依性」按滑鼠右鍵,點選「管理 NuGet 套件」(或是「工具」,「NuGet 套件管理員」點選「管理方案的 NuGet 套件」) 於「瀏覽」標籤頁搜尋以下兩個,選擇適合的版本安裝,下圖是已安裝好的標籤頁: Dapper 作者 Sam Safforn, Marc Gravell, Nick Craver System.Data.SqlClient 作者 Microsoft 常常看教學還要安裝一個和 Configuration 相關的,但我安裝以上兩個套件即可。 取得連線字串和設定 appsettings.json 以我的本機 MS SQL 為例,機器「.」使用Windows 驗證登入,有一個資料庫叫「dbtest」: 回到 Visual Studio,點選「檢視」,「伺服器總管」,在伺服器總管視窗的「資料連接」上或空白處按滑鼠右鍵,點選「加入連接」: 「選擇資料來源」頁面,「資料來源」選擇 Microsoft SQL Server,「資料提供者」選擇 .NET Framework Data Provider for SQL Server,點選繼續: 「加入連接」頁面在「伺服器名稱」填入「.」驗證選 Windows 驗證,然後下方「選取或輸入資料庫名稱」就可以下拉找資料庫了,我選了前面說的 dbtest 資料庫。當然如果 SQL Server 在...

[ASP.NET Core 6 MVC] 使用 Session

圖片
這篇是參考自 C# Corner : How To Use Sessions In ASP.NET Core 的教學,不過文中是 .NET 5,所以和 .NET 6 設定上有點差異,再加上和過去 .NET 4 使用 namespace 後就可以無腦使用 Session 不太一樣,就做了這個筆記,以下範例使用 Visual Studio 2022,建立專案為 ASP.NET Core Web 應用程式 (Model-View-Controller)使用 .NET 6.0 架構。 下載 Session 套件 設定 Program.cs 使用 Session ,存取字串和整數 用 Json 序列化使 Session 存取物件 將序列化寫成擴充方法 下載 Session 套件 在方案總管視窗對「相依性(Dependencies)」滑鼠右鍵,選擇「管理 NuGet 套件」: 在「瀏覽」標籤頁中搜尋「Session」,應該最多人下載的是 Microsoft 的「Microsoft.AspNetCore.Session」: 依版本點選「安裝」(這裡我選最新穩定版 2.2.0): 若有變更警告點選「OK」,授權提示點選「I Accept」: 裝好後剛剛的頁面應該會顯示目前版本,且原本的「安裝」變成「解除安裝」,就可以關掉 NuGet 分頁了。 設定 Program.cs 再來要設定 Program.cs 設定檔,至方案總管視窗雙擊此檔案: 原本檔案的最上面是: var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); 在此給容器加入 Session service: var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithView...

[ASP.NET MVC] 匯出 .csv 檔案

圖片
常見的匯出檔案有 .pdf .xls .ods .csv,下面筆記最簡單的 .csv 檔,可用常見的 Microsoft Office Excel 或 LibreOffice Calc 等報表軟體開啟: 產生檔案下載 列出清單結果 匯出 .csv 檔案 產生檔案下載 Controller 可以用 return File(); 來做到檔案下載,引數分別為 byte[] 的檔案內容,string 表示的檔案類型和 string 下載後檔案名稱,而 byte[] 類型可用 System.Text.Encoding.Default.GetBytes() 方法來獲得位元組序列,以下為名稱 Demo 的 Controller 程式碼: using System.Text; namespace prjMVC.Controllers { public class DemoController : Controller { public ActionResult GetFiles(string output) { string fileName = "output.txt"; // 存成檔名 byte[] fileContent = Encoding.Default.GetBytes(output); // 轉成位元組序列 return File(fileContent, "text/plain", fileName); } } } 以上 Action 在瀏覽器輸入 ~/Demo/GetFiles?output=xxxx 的網址,或是 View 中有 @Html.ActionLink("下載", "GetFiles", new { output = "xxxx" }) 產生的超連結被點擊時,瀏覽器就會下載一個文字檔案 output.txt: 下面就是傳入 output 字串 "測試文字" 得到的下載文字檔案: 列出清單結果 這個...

[ASP.NET MVC] Html.Partial() 和 Html.Action()

圖片
ASP.NET MVC 的 View 中可以有 Partial View (部份檢視),而且可以繫結不一樣的 Model,例如 View 繫結叫做 VM_Big 的物件,可以在 Partial View 繫結叫做 QM_Small 的物件,下面是專案 prjMvc 下的 ~/Models 裡的兩個類別: VM_Big namespace prjMVC.Models { public class VM_Big { public QM_Small Small { get; set; } } } 和 QM_Small namespace prjMVC.Models { public class QM_Small { public DateTime Time { get; set; } public string Description { get; set; } } } 這裡讓 VM_Big 類別下有一個屬性,是 QM_Small 物件,而 QM_Small 類別下有時間物件 Time 和字串 Description,然後在 Controller 中寫一個 Action 叫 Index: using prjMVC.Models; namespace prjMVC.Controllers { public class DemoController : Controller { public ActionResult Index() { VM_Big big = new VM_Big(); big.Small = new QM_Small(); big.Small.Time = DateTime.Now; big.Small.Description = "現在時間"; return View(big); } } } 程式碼如上,實體化一個 VM_Big 的物件叫 big,並將他的屬性 Small 用 QM_Small 實體化,並指定屬性時間物件 Time 為 DateTime....

[ASP.NET MVC] @Html.DisplayFor() 、@Html.DisplayTextFor()、@Html.DisplayNameFor() 和 @Html.EditorFor()

圖片
使用過 ASP.NET MVC 內建產生範本功能的人一定對標題的四個方法不陌生,如下有一 Model UserInfo 這樣設計: Model 各屬性的資料型態 using System.ComponentModel; namespace prjMVC.Models { public class UserInfo { [DisplayName("姓名")] public string Name { get; set; } [DisplayName("年齡")] public int Age { get; set; } [DisplayName("體重")] public double Weight { get; set; } [DisplayName("生日")] public DateTime Birthday { get; set; } [DisplayName("已婚")] public bool IsMarried { get; set; } } } 在 Controller 傳到 View: UserInfo user = new UserInfo { Name = "Billy", Age = 25, Weight = 74.5, Birthday = new DateTime(1997, 5, 2), IsMarried = true, }; return View(user); DisplayFor() 方法 在 View 中,@Html.DisplayFor() 會依據資料和型態來顯示: @model prjMvc.Models.UserInfo <h2>DisplayFor</h2> <div>@Html.DisplayFor(m => m.Name)</div> <div...

[ASP.NET MVC] @Html.BeginForm() 和 <form>

圖片
要產生 <form> 要使用 @Html.BeginForm() 方法,不過不能單獨呼叫,因為 <form> 要有 </form> ,所以要利用 @using() { } 包住。下面舉例,在 Home Controller 裡寫一個 Action 叫 Create : public ActionResult Create() { return View(); } 在 Create 的 View 中,如下使用 BeginForm() 方法: @using(Html.BeginForm()) { } // 會產生: <form action="/Home/Create" method="post"></form> 會發現 action 屬性已自動填上本身 Action 的位址,而且 method 自動使用 Post,這些是 BeginForm() 預設的選項,也可以更改: @using(Html.BeginForm("CreateDetail" // 目標 Action , "Order" // 目標 Controll , FormMethod.Get)) // 使用 method 方法 { } // 會產生: <form action="/Order/CreateDetail" method="get"><form> 如上,action URL 會更改,且 method 也改成使用 Get 方法。 也可以用五個引數,在第三個引數帶入 route value ,第五個引數帶入 HTML Attribute,都是使用匿名類別帶入,如下: @using(Html.BeginForm("CreateDetail" , "Order...