Web Analytics

MiniWord

⭐ 704 stars Thai by mini-software

NuGet GitHub stars version Ask DeepWiki


version version version version version version version version version version version version version version version version version


การ Star และ dotnate ของคุณ สามารถทำให้ MiniWord ดียิ่งขึ้น


แนะนำ

MiniWord เป็นไลบรารีเทมเพลต Word สำหรับ .NET ที่ง่ายและมีประสิทธิภาพ

image

เริ่มต้นใช้งาน

การติดตั้ง

เริ่มต้นอย่างรวดเร็ว

เทมเพลตใช้การออกแบบ "WHAT you see is what you get" และรูปแบบของแท็กเทมเพลตจะถูกเก็บรักษาไว้อย่างสมบูรณ์

var value = new Dictionary(){["title"] = "Hello MiniWord"};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
image

อินพุต, เอาต์พุต

SaveAsByTemplate(string path, string templatePath, Dictionary value)
SaveAsByTemplate(string path, byte[] templateBytes, Dictionary value)
SaveAsByTemplate(this Stream stream, string templatePath, Dictionary value)
SaveAsByTemplate(this Stream stream, byte[] templateBytes, Dictionary value)

แท็ก

รูปแบบสตริงเทมเพลต MiniWord เช่นเดียวกับ Vue, React {{tag}},ผู้ใช้เพียงแค่ต้องให้แท็กและคีย์ของพารามิเตอร์ value เหมือนกัน จากนั้นระบบจะทำการแทนที่โดยอัตโนมัติ

ข้อความ

{{tag}}
##### ตัวอย่าง

var value = new Dictionary()
{
    ["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);
##### แม่แบบ

image

##### ผลลัพธ์

image

รูปภาพ

ชนิดของค่าเป็น MiniWordPicture

##### ตัวอย่าง

var value = new Dictionary()
{
    ["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("DemoLogo.png"), Width= 180, Height= 180 }
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
##### เทมเพลต

image

##### ผลลัพธ์

image

รายการ

ค่า tag เป็นประเภท string[] หรือ IList

##### ตัวอย่าง

var value = new Dictionary()
{
    ["managers"] = new[] { "Jack" ,"Alan"},
    ["employees"] = new[] { "Mike" ,"Henry"},
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
เทมเพลต

image

##### ผลลัพธ์

image

ตาราง

ค่าของแท็กเป็นชนิด IEmerable>

##### ตัวอย่าง

var value = new Dictionary()
{
    ["TripHs"] = new List>
    {
        new Dictionary
        {
            { "sDate",DateTime.Parse("2022-09-08 08:30:00")},
            { "eDate",DateTime.Parse("2022-09-08 15:00:00")},
            { "How","Discussion requirement part1"},
            { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }},
        },
        new Dictionary
        {
            { "sDate",DateTime.Parse("2022-09-09 08:30:00")},
            { "eDate",DateTime.Parse("2022-09-09 17:00:00")},
            { "How","Discussion requirement part2 and development"},
            { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }},
        },
    }
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
##### เทมเพลต

image

##### ผลลัพธ์

image

รายการซ้อนในรายการ

ค่าของแท็กคือชนิด IEnumerable จำเป็นต้องเพิ่มแท็ก {{foreach และ endforeach}} ในเทมเพลต

##### ตัวอย่าง

var value = new Dictionary()
{
    ["TripHs"] = new List>
    {
        new Dictionary
        {
            { "sDate", DateTime.Parse("2022-09-08 08:30:00") },
            { "eDate", DateTime.Parse("2022-09-08 15:00:00") },
            { "How", "Discussion requirement part1" },
            {
                "Details", new List()
                {
                    new MiniWordForeach()
                    {
                        Value = new Dictionary()
                        {
                            {"Text", "Air"},
                            {"Value", "Airplane"}
                        },
                        Separator = " | "
                    },
                    new MiniWordForeach()
                    {
                        Value = new Dictionary()
                        {
                            {"Text", "Parking"},
                            {"Value", "Car"}
                        },
                        Separator = " / "
                    }
                }
            }
        }
    }
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
##### แม่แบบ

before_foreach

Screenshot 2023-08-08 at 17 59 37

##### ผลลัพธ์

after_foreach

Screenshot 2023-08-08 at 18 00 15

คำสั่ง if ภายในแม่แบบ

สำหรับหลายย่อหน้า ให้ใช้แท็ก @if และ @endif สำหรับย่อหน้าเดียวและภายใน foreach ให้ใช้แท็ก {{if และ endif}} ตามที่แม่แบบต้องการ

##### ตัวอย่าง

var value = new Dictionary()
{
    ["Name"] = new List(){
        new MiniWordHyperLink(){
            Url = "https://google.com",
            Text = "測試連結22!!"
        },
        new MiniWordHyperLink(){
            Url = "https://google1.com",
            Text = "測試連結11!!"
        }
    },
    ["Company_Name"] = "MiniSofteware",
    ["CreateDate"] = new DateTime(2021, 01, 01),
    ["VIP"] = true,
    ["Points"] = 123,
    ["APP"] = "Demo APP",
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
##### เทมเพลตสำหรับหลายย่อหน้า

before_if

##### ผลลัพธ์ของหลายย่อหน้า

after_if

##### เทมเพลตสำหรับย่อหน้าเดียว

Screenshot 2023-08-08 at 17 55 46

##### ผลลัพธ์ของย่อหน้าเดียว

Screenshot 2023-08-08 at 17 56 47

ColorText

##### ตัวอย่าง

var value = new
{
    Company_Name = new MiniWordColorText { Text = "MiniSofteware", FontColor = "#eb70AB", },
    Name = new[] {
        new MiniWordColorText { Text = "Ja", HighlightColor = "#eb70AB" },
        new MiniWordColorText { Text = "ck", HighlightColor = "#a56abe" }
    },
    CreateDate = new MiniWordColorText
    {
        Text = new DateTime(2021, 01, 01).ToString(),
        HighlightColor = "#eb70AB",
        FontColor = "#ffffff",
    },
    VIP = true,
    Points = 123,
    APP = "Demo APP",
};
MiniWord.SaveAsByTemplate(path, templatePath, value);

อื่นๆ

POCO หรือพารามิเตอร์แบบไดนามิก

v0.5.0 รองรับ POCO หรือพารามิเตอร์แบบไดนามิก

var value = new { title = "Hello MiniWord" };
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);

สีตัวอักษรและสีเน้นข้อความ

var value = new
{
    Company_Name = new MiniWordColorText { Text = "MiniSofteware", FontColor = "#eb70AB" },
    Name = new MiniWordColorText { Text = "Jack", HighlightColor = "#eb70AB" },
    CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), HighlightColor = "#eb70AB", FontColor = "#ffffff" },
    VIP = true,
    Points = 123,
    APP = "Demo APP",
};

ไฮเปอร์ลิงก์

หากชนิดของค่าคือ MiniWordHyperLink ระบบจะทำการแทนที่สตริงเทมเพลตด้วยไฮเปอร์ลิงก์

var value = new
{
    ["Name"] = new MiniWordHyperLink(){
        Url = "https://google.com",
        Text = "Test Link!!"
    },
    ["Company_Name"] = "MiniSofteware",
    ["CreateDate"] = new DateTime(2021, 01, 01),
    ["VIP"] = true,
    ["Points"] = 123,
    ["APP"] = "Demo APP",
};
MiniWord.SaveAsByTemplate(path, templatePath, value);

ตัวอย่าง

#### การส่งออก API ด้วย ASP.NET Core 3.1

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using MiniSoftware;

public class Program { public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup()); }

public class Startup { public void ConfigureServices(IServiceCollection services) => services.AddMvc(); public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=api}/{action=Index}/{id?}"); }); } }

public class ApiController : Controller { public IActionResult Index() { return new ContentResult { ContentType = "text/html", StatusCode = (int)HttpStatusCode.OK, Content = @" DownloadWordFromTemplatePath
DownloadWordFromTemplateBytes
" }; }

static Dictionary defaultValue = new Dictionary() { ["title"] = "FooCompany", ["managers"] = new List> { new Dictionary{{"name","Jack"},{ "department", "HR" } }, new Dictionary {{ "name", "Loan"},{ "department", "IT" } } }, ["employees"] = new List> { new Dictionary{{ "name", "Wade" },{ "department", "HR" } }, new Dictionary {{ "name", "Felix" },{ "department", "HR" } }, new Dictionary{{ "name", "Eric" },{ "department", "IT" } }, new Dictionary {{ "name", "Keaton" },{ "department", "IT" } } } };

public IActionResult DownloadWordFromTemplatePath() { string templatePath = "TestTemplateComplex.docx";

Dictionary 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" }; }

private static Dictionary TemplateBytesCache = new Dictionary();

static ApiController() { string templatePath = "TestTemplateComplex.docx"; byte[] bytes = System.IO.File.ReadAllBytes(templatePath); TemplateBytesCache.Add(templatePath, bytes); }

public IActionResult DownloadWordFromTemplateBytes() { byte[] bytes = TemplateBytesCache["TestTemplateComplex.docx"];

Dictionary 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" }; } }

สนับสนุน : ลิงก์สำหรับบริจาค

--- Tranlated By Open Ai Tx | Last indexed: 2025-07-17 ---