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<Startup>()); } 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 = @"<html><body> <a href="https://gitee.com/dotnetchina/api/DownloadWordFromTemplatePath">DownloadWordFromTemplatePath</a><br> <a href="https://gitee.com/dotnetchina/api/DownloadWordFromTemplateBytes">DownloadWordFromTemplateBytes</a><br> </body></html>" }; } static Dictionary<string, object> defaultValue = new Dictionary<string, object>() { ["title"] = "FooCompany", ["managers"] = new List<Dictionary<string, object>> { new Dictionary<string, object>{{"name","Jack"},{ "department", "HR" } }, new Dictionary<string, object> {{ "name", "Loan"},{ "department", "IT" } } }, ["employees"] = new List<Dictionary<string, object>> { new Dictionary<string, object>{{ "name", "Wade" },{ "department", "HR" } }, new Dictionary<string, object> {{ "name", "Felix" },{ "department", "HR" } }, new Dictionary<string, object>{{ "name", "Eric" },{ "department", "IT" } }, new Dictionary<string, object> {{ "name", "Keaton" },{ "department", "IT" } } } }; 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" }; } private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>(); 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<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" }; } }
#NET #Worddocx #template #export #engine #require #installation #Office #Supports #Linux #Mac