MiniWord 推薦一個(gè) .NET 開源的免費(fèi) Word 處理神器 MiniWord。這是一個(gè)非常簡(jiǎn)單有效的 .NET Word 模板庫(kù),甚至可以使用一行代碼處理 Word,非常方便。并且不需要安裝微軟 Word,無(wú)需 COM+ 和互操作支持 Linux 和 Mac,輕量級(jí) Word 處理神器。
快速使用 模板遵循“所見即所得”設(shè)計(jì),模板標(biāo)簽樣式完整保留。
var value = new Dictionary< string , object >(){[ "title" ] = "Hello MiniWord" }; MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value );
MiniWord 模板格式字符串如 Vue、React {{tag}} ,用戶只需確保 tag 和 value 參數(shù) key 相同,系統(tǒng)會(huì)自動(dòng)替換它們。
var value = new Dictionary< string , object >() { [ "Name" ] = "Jack" , [ "Department" ] = "IT Department" , [ "Purpose" ] = "Shanghai site needs a new system to control HR system." , [ "StartDate" ] = DateTime.Parse( "2022-09-07 08:30:00" ), [ "EndDate" ] = DateTime.Parse( "2022-09-15 15:30:00" ), [ "Approved" ] = true , [ "Total_Amount" ] = 123456 , }; MiniWord.SaveAsByTemplate(path, templatePath, value );
模板
結(jié)果
.NET Core 使用 public IActionResult DownloadWordFromTemplatePath ( ) { string templatePath = "TestTemplateComplex.docx" ; Dictionary< string , object > value = defaultValue; MemoryStream memoryStream = new MemoryStream(); MiniWord.SaveAsByTemplate(memoryStream, templatePath, value ); memoryStream.Seek( 0 , SeekOrigin.Begin); return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) { FileDownloadName = "demo.docx" }; }
public IActionResult DownloadWordFromTemplateBytes ( ) { byte [] bytes = TemplateBytesCache[ "TestTemplateComplex.docx" ]; Dictionary< string , object > value = defaultValue; MemoryStream memoryStream = new MemoryStream(); MiniWord.SaveAsByTemplate(memoryStream, bytes, value ); memoryStream.Seek( 0 , SeekOrigin.Begin); return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) { FileDownloadName = "demo.docx" }; }
MiniWord 的更多功能,等待您去發(fā)現(xiàn)!
- EOF -
閱讀原文:原文鏈接
該文章在 2025/5/26 12:29:10 編輯過(guò)