ASP.NET Core 3.1 or MVC 5 Download Excel Xlsx API Demo

This commit is contained in:
wei 2021-04-19 15:15:06 +08:00
parent c2bb4d9591
commit 7c2a4dd337
7 changed files with 243 additions and 164 deletions

View File

@ -525,8 +525,22 @@ performance:
#### 2. ASP.NET Core 3.1 or MVC 5 Download Excel Xlsx API Demo [Try it](tests/MiniExcel.Tests.AspNetCore)
```C#
public class ExcelController : Controller
public class HomeController : Controller
{
public IActionResult Index()
{
return new ContentResult
{
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
Content = @"<html><body>
<a href='Home/DownloadExcel'>DownloadExcel</a><br>
<a href='Home/DownloadExcelFromTemplatePath'>DownloadExcelFromTemplatePath</a><br>
<a href='Home/DownloadExcelFromTemplateBytes'>DownloadExcelFromTemplateBytes</a>
</body></html>"
};
}
public IActionResult DownloadExcel()
{
var values = new[] {
@ -542,10 +556,11 @@ public class ExcelController : Controller
};
}
public IActionResult DownloadExcelFromTmplate()
public IActionResult DownloadExcelFromTemplatePath()
{
var templatePath = "TestTemplateComplex.xlsx";
var value = new Dictionary<string, object>()
string templatePath = "TestTemplateComplex.xlsx";
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
@ -559,7 +574,8 @@ public class ExcelController : Controller
new {name="Keaton",department="IT"}
}
};
var memoryStream = new MemoryStream();
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(templatePath, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@ -568,11 +584,20 @@ public class ExcelController : Controller
};
}
public IActionResult DownloadExcelFromTmplate_StremVersion()
private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>();
static HomeController()
{
var templatePath = "TestTemplateComplex.xlsx";
var tytes = System.IO.File.ReadAllBytes(templatePath);
var value = new Dictionary<string, object>()
string templatePath = "TestTemplateComplex.xlsx";
byte[] bytes = System.IO.File.ReadAllBytes(templatePath);
TemplateBytesCache.Add(templatePath, bytes);
}
public IActionResult DownloadExcelFromTemplateBytes()
{
byte[] bytes = TemplateBytesCache["TestTemplateComplex.xlsx"];
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
@ -586,8 +611,9 @@ public class ExcelController : Controller
new {name="Keaton",department="IT"}
}
};
var memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(tytes, value);
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(bytes, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{

View File

@ -357,10 +357,10 @@ MiniExcel.SaveAsByTemplate(path, templatePath, value);
模板:
![image](https://user-images.githubusercontent.com/12729184/114564652-14f2f080-9ca3-11eb-831f-09e3fedbc5fc.png)
最终效果:
最终效果:
![image](https://user-images.githubusercontent.com/12729184/114564204-b2015980-9ca2-11eb-900d-e21249f93f7c.png)
代码:
代码:
```C#
//1. By POCO
@ -398,15 +398,15 @@ MiniExcel.SaveAsByTemplate(path, templatePath, value);
> Note: 支持多 sheet 填充,并共用同一组参数
模板:
模板:
![image](https://user-images.githubusercontent.com/12729184/114565255-acf0da00-9ca3-11eb-8a7f-8131b2265ae8.png)
最终效果:
最终效果:
![image](https://user-images.githubusercontent.com/12729184/114565329-bf6b1380-9ca3-11eb-85e3-3969e8bf6378.png)
代码:
代码:
```C#
// 1. By POCO
@ -452,15 +452,15 @@ MiniExcel.SaveAsByTemplate(path, templatePath, value);
#### 5. Cell 值自动类别对应
模板
模板
![image](https://user-images.githubusercontent.com/12729184/114802504-64830a80-9dd0-11eb-8d56-8e8c401b3ace.png)
最终效果
最终效果
![image](https://user-images.githubusercontent.com/12729184/114802419-43221e80-9dd0-11eb-9ffe-a2ce34fe7076.png)
类别
类别
```C#
public class Poco
@ -495,16 +495,16 @@ MiniExcel.SaveAsByTemplate(path, templatePath, value);
#### 6. Example : 列出 Github 专案
模板
模板
![image](https://user-images.githubusercontent.com/12729184/115068623-12073280-9f25-11eb-9124-f4b3efcdb2a7.png)
最终效果
最终效果
![image](https://user-images.githubusercontent.com/12729184/115068639-1a5f6d80-9f25-11eb-9f45-27c434d19a78.png)
代码
代码
```C#
var projects = new[]
@ -609,8 +609,22 @@ using (var connection = new SQLiteConnection(connectionString))
#### 2. ASP.NET Core 3.1 or MVC 5 下载 Excel Xlsx API Demo [Try it](tests/MiniExcel.Tests.AspNetCore)
```C#
public class ExcelController : Controller
public class HomeController : Controller
{
public IActionResult Index()
{
return new ContentResult
{
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
Content = @"<html><body>
<a href='Home/DownloadExcel'>DownloadExcel</a><br>
<a href='Home/DownloadExcelFromTemplatePath'>DownloadExcelFromTemplatePath</a><br>
<a href='Home/DownloadExcelFromTemplateBytes'>DownloadExcelFromTemplateBytes</a>
</body></html>"
};
}
public IActionResult DownloadExcel()
{
var values = new[] {
@ -626,10 +640,11 @@ public class ExcelController : Controller
};
}
public IActionResult DownloadExcelFromTmplate()
public IActionResult DownloadExcelFromTemplatePath()
{
var templatePath = "TestTemplateComplex.xlsx";
var value = new Dictionary<string, object>()
string templatePath = "TestTemplateComplex.xlsx";
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
@ -643,7 +658,8 @@ public class ExcelController : Controller
new {name="Keaton",department="IT"}
}
};
var memoryStream = new MemoryStream();
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(templatePath, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@ -652,11 +668,20 @@ public class ExcelController : Controller
};
}
public IActionResult DownloadExcelFromTmplate_StremVersion()
private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>();
static HomeController()
{
var templatePath = "TestTemplateComplex.xlsx";
var tytes = System.IO.File.ReadAllBytes(templatePath);
var value = new Dictionary<string, object>()
string templatePath = "TestTemplateComplex.xlsx";
byte[] bytes = System.IO.File.ReadAllBytes(templatePath);
TemplateBytesCache.Add(templatePath, bytes);
}
public IActionResult DownloadExcelFromTemplateBytes()
{
byte[] bytes = TemplateBytesCache["TestTemplateComplex.xlsx"];
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
@ -670,8 +695,9 @@ public class ExcelController : Controller
new {name="Keaton",department="IT"}
}
};
var memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(tytes, value);
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(bytes, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{

View File

@ -360,7 +360,7 @@ MiniExcel.SaveAsByTemplate(path, templatePath, value);
模板:
![image](https://user-images.githubusercontent.com/12729184/114564652-14f2f080-9ca3-11eb-831f-09e3fedbc5fc.png)
最終效果:
最終效果:
![image](https://user-images.githubusercontent.com/12729184/114564204-b2015980-9ca2-11eb-900d-e21249f93f7c.png)
代碼:
@ -611,8 +611,22 @@ using (var connection = new SQLiteConnection(connectionString))
#### 2. ASP.NET Core 3.1 or MVC 5 下載 Excel Xlsx API Demo [Try it](tests/MiniExcel.Tests.AspNetCore)
```C#
public class ExcelController : Controller
public class HomeController : Controller
{
public IActionResult Index()
{
return new ContentResult
{
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
Content = @"<html><body>
<a href='Home/DownloadExcel'>DownloadExcel</a><br>
<a href='Home/DownloadExcelFromTemplatePath'>DownloadExcelFromTemplatePath</a><br>
<a href='Home/DownloadExcelFromTemplateBytes'>DownloadExcelFromTemplateBytes</a>
</body></html>"
};
}
public IActionResult DownloadExcel()
{
var values = new[] {
@ -628,10 +642,11 @@ public class ExcelController : Controller
};
}
public IActionResult DownloadExcelFromTmplate()
public IActionResult DownloadExcelFromTemplatePath()
{
var templatePath = "TestTemplateComplex.xlsx";
var value = new Dictionary<string, object>()
string templatePath = "TestTemplateComplex.xlsx";
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
@ -645,7 +660,8 @@ public class ExcelController : Controller
new {name="Keaton",department="IT"}
}
};
var memoryStream = new MemoryStream();
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(templatePath, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@ -654,11 +670,20 @@ public class ExcelController : Controller
};
}
public IActionResult DownloadExcelFromTmplate_StremVersion()
private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>();
static HomeController()
{
var templatePath = "TestTemplateComplex.xlsx";
var tytes = System.IO.File.ReadAllBytes(templatePath);
var value = new Dictionary<string, object>()
string templatePath = "TestTemplateComplex.xlsx";
byte[] bytes = System.IO.File.ReadAllBytes(templatePath);
TemplateBytesCache.Add(templatePath, bytes);
}
public IActionResult DownloadExcelFromTemplateBytes()
{
byte[] bytes = TemplateBytesCache["TestTemplateComplex.xlsx"];
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
@ -672,8 +697,9 @@ public class ExcelController : Controller
new {name="Keaton",department="IT"}
}
};
var memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(tytes, value);
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(bytes, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{

BIN
docs/MiniExcel_Logo.pptx Normal file

Binary file not shown.

View File

@ -5,11 +5,14 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MiniExcel\MiniExcelLibs.csproj" />
<Compile Remove="wwwroot\**" />
<Content Remove="wwwroot\**" />
<EmbeddedResource Remove="wwwroot\**" />
<None Remove="wwwroot\**" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
<ProjectReference Include="..\..\src\MiniExcel\MiniExcelLibs.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -1,23 +1,125 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Net;
public class Program
{
public static void Main(string[] args)
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)
{
CreateHostBuilder(args).Build().Run();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
}
}
public class HomeController : Controller
{
public IActionResult Index()
{
return new ContentResult
{
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
Content = @"<html><body>
<a href='Home/DownloadExcel'>DownloadExcel</a><br>
<a href='Home/DownloadExcelFromTemplatePath'>DownloadExcelFromTemplatePath</a><br>
<a href='Home/DownloadExcelFromTemplateBytes'>DownloadExcelFromTemplateBytes</a>
</body></html>"
};
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public IActionResult DownloadExcel()
{
var values = new[] {
new { Column1 = "MiniExcel", Column2 = 1 },
new { Column1 = "Github", Column2 = 2}
};
var memoryStream = new MemoryStream();
memoryStream.SaveAs(values);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "demo.xlsx"
};
}
public IActionResult DownloadExcelFromTemplatePath()
{
string templatePath = "TestTemplateComplex.xlsx";
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
new {name="Jack",department="HR"},
new {name="Loan",department="IT"}
},
["employees"] = new[] {
new {name="Wade",department="HR"},
new {name="Felix",department="HR"},
new {name="Eric",department="IT"},
new {name="Keaton",department="IT"}
}
};
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(templatePath, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "demo.xlsx"
};
}
private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>();
static HomeController()
{
string templatePath = "TestTemplateComplex.xlsx";
byte[] bytes = System.IO.File.ReadAllBytes(templatePath);
TemplateBytesCache.Add(templatePath, bytes);
}
public IActionResult DownloadExcelFromTemplateBytes()
{
byte[] bytes = TemplateBytesCache["TestTemplateComplex.xlsx"];
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
new {name="Jack",department="HR"},
new {name="Loan",department="IT"}
},
["employees"] = new[] {
new {name="Wade",department="HR"},
new {name="Felix",department="HR"},
new {name="Eric",department="IT"},
new {name="Keaton",department="IT"}
}
};
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(bytes, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "demo.xlsx"
};
}
}

View File

@ -1,104 +0,0 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using MiniExcelLibs;
using System;
using System.Net;
using System.Collections.Generic;
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.MapDefaultControllerRoute());
}
}
public class HomeController : Controller
{
public IActionResult Index()
{
return new ContentResult
{
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
Content = @"<html><body>
<a href='Home/DownloadExcel'>DownloadExcel</a><br>
<a href='Home/DownloadExcelFromTmplate'>DownloadExcelFromTmplate</a><br>
<a href='Home/DownloadExcelFromTmplate_StremVersion'>DownloadExcelFromTmplate_StremVersion</a>
</body></html>"
};
}
public IActionResult DownloadExcel()
{
var values = new[] {
new { Column1 = "MiniExcel", Column2 = 1 },
new { Column1 = "Github", Column2 = 2}
};
var memoryStream = new MemoryStream();
memoryStream.SaveAs(values);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "demo.xlsx"
};
}
public IActionResult DownloadExcelFromTmplate()
{
string templatePath = "TestTemplateComplex.xlsx";
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
new {name="Jack",department="HR"},
new {name="Loan",department="IT"}
},
["employees"] = new[] {
new {name="Wade",department="HR"},
new {name="Felix",department="HR"},
new {name="Eric",department="IT"},
new {name="Keaton",department="IT"}
}
};
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(templatePath, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "demo.xlsx"
};
}
public IActionResult DownloadExcelFromTmplate_StremVersion()
{
string templatePath = "TestTemplateComplex.xlsx";
byte[] bytes = System.IO.File.ReadAllBytes(templatePath);
Dictionary<string, object> value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
new {name="Jack",department="HR"},
new {name="Loan",department="IT"}
},
["employees"] = new[] {
new {name="Wade",department="HR"},
new {name="Felix",department="HR"},
new {name="Eric",department="IT"},
new {name="Keaton",department="IT"}
}
};
MemoryStream memoryStream = new MemoryStream();
memoryStream.SaveAsByTemplate(bytes, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "demo.xlsx"
};
}
}