From a79acd72a98d9f2d2b5b64cfe3ea3c42d598e9a6 Mon Sep 17 00:00:00 2001 From: wei Date: Sat, 8 May 2021 18:18:25 +0800 Subject: [PATCH] webform doc and test --- README.md | 55 +++++++++++++++++++ README.zh-CN.md | 48 +++++++++++++++- README.zh-Hant.md | 42 +++++++++++++- .../WebFormTest/TestIssue223.aspx | 2 + .../WebFormTest/TestIssue223.aspx.cs | 37 +++++++++++-- .../WebFormTest/TestIssue223.aspx.designer.cs | 18 ++++++ 6 files changed, 192 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2ec3437..7c4cd1c 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,37 @@ using (var stream = File.Create(path)) } ``` +#### 5. SaveAs to MemoryStream [[Try it]](https://dotnetfiddle.net/JOen0e) + +```csharp +using (var stream = new MemoryStream()) //support FileStream,MemoryStream ect. +{ + stream.SaveAs(values); +} +``` + +e.g : api of export excel + +```csharp +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" + }; +} +``` + + + #### 6. Support IDataReader value parameter ```csharp @@ -788,6 +819,30 @@ public static IEnumerable Page(IEnumerable en, int pageSize, int page) ![20210419](https://user-images.githubusercontent.com/12729184/114679083-6ef4c400-9d3e-11eb-9f78-a86daa45fe46.gif) + + +#### 4. WebForm export Excel by memorystream + +```csharp +var fileName = "Demo.xlsx"; +var sheetName = "Sheet1"; +HttpResponse response = HttpContext.Current.Response; +response.Clear(); +response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; +response.AddHeader("Content-Disposition", $"attachment;filename=\"{fileName}\""); +var values = new[] { + new { Column1 = "MiniExcel", Column2 = 1 }, + new { Column1 = "Github", Column2 = 2} +}; +var memoryStream = new MemoryStream(); +memoryStream.SaveAs(values, sheetName: sheetName); +memoryStream.Seek(0, SeekOrigin.Begin); +memoryStream.CopyTo(Response.OutputStream); +response.End(); +``` + + + ### FAQ #### Q: Excel header title not equal class property name, how to mapping? diff --git a/README.zh-CN.md b/README.zh-CN.md index 08a9a12..14b7224 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -315,15 +315,35 @@ output : | MiniExcel | 1 | | Github | 2 | -#### 5. SaveAs 支持 Stream [[Try it]](https://dotnetfiddle.net/JOen0e) +#### 5. SaveAs 支持 Stream,生成文件不落地 [[Try it]](https://dotnetfiddle.net/JOen0e) ```csharp -using (var stream = File.Create(path)) +using (var stream = new MemoryStream()) //支持 FileStream,MemoryStream..等 { stream.SaveAs(values); } ``` +像是 API 导出 Excel + +```csharp +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" + }; +} +``` + #### 6. 支持 IDataReader 参数 @@ -649,7 +669,7 @@ using (var connection = new SQLiteConnection(connectionString)) ![image](https://user-images.githubusercontent.com/12729184/111072579-2dda7b80-8516-11eb-9843-c01a1edc88ec.png) -#### 2. ASP.NET Core 3.1 or MVC 5 下载/上传 Excel Xlsx API Demo [Try it](tests/MiniExcel.Tests.AspNetCore) +#### 2. ASP.NET Core 3.1 下载/上传 Excel Xlsx API Demo [Try it](tests/MiniExcel.Tests.AspNetCore) ```csharp public class ApiController : Controller @@ -791,6 +811,28 @@ public static IEnumerable Page(IEnumerable en, int pageSize, int page) ![20210419](https://user-images.githubusercontent.com/12729184/114679083-6ef4c400-9d3e-11eb-9f78-a86daa45fe46.gif) +#### 4. WebForm不落地导出Excel + +```csharp +var fileName = "Demo.xlsx"; +var sheetName = "Sheet1"; +HttpResponse response = HttpContext.Current.Response; +response.Clear(); +response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; +response.AddHeader("Content-Disposition", $"attachment;filename=\"{fileName}\""); +var values = new[] { + new { Column1 = "MiniExcel", Column2 = 1 }, + new { Column1 = "Github", Column2 = 2} +}; +var memoryStream = new MemoryStream(); +memoryStream.SaveAs(values, sheetName: sheetName); +memoryStream.Seek(0, SeekOrigin.Begin); +memoryStream.CopyTo(Response.OutputStream); +response.End(); +``` + + + ### FAQ 常见问题 diff --git a/README.zh-Hant.md b/README.zh-Hant.md index 2949ca6..1e40099 100644 --- a/README.zh-Hant.md +++ b/README.zh-Hant.md @@ -317,15 +317,35 @@ output : | MiniExcel | 1 | | Github | 2 | -#### 5. SaveAs 支援 Stream [[Try it]](https://dotnetfiddle.net/JOen0e) +#### 5. SaveAs 支持 Stream,生成文件不落地 [[Try it]](https://dotnetfiddle.net/JOen0e) ```csharp -using (var stream = File.Create(path)) +using (var stream = new MemoryStream()) //支持 FileStream,MemoryStream..等 { stream.SaveAs(values); } ``` +像是 API 導出 Excel + +```csharp +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" + }; +} +``` + #### 6. 支持 IDataReader 參數 ```csharp @@ -790,7 +810,25 @@ public static IEnumerable Page(IEnumerable en, int pageSize, int page) ![20210419](https://user-images.githubusercontent.com/12729184/114679083-6ef4c400-9d3e-11eb-9f78-a86daa45fe46.gif) +#### 4. WebForm不落地導出Excel +```csharp +var fileName = "Demo.xlsx"; +var sheetName = "Sheet1"; +HttpResponse response = HttpContext.Current.Response; +response.Clear(); +response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; +response.AddHeader("Content-Disposition", $"attachment;filename=\"{fileName}\""); +var values = new[] { + new { Column1 = "MiniExcel", Column2 = 1 }, + new { Column1 = "Github", Column2 = 2} +}; +var memoryStream = new MemoryStream(); +memoryStream.SaveAs(values, sheetName: sheetName); +memoryStream.Seek(0, SeekOrigin.Begin); +memoryStream.CopyTo(Response.OutputStream); +response.End(); +``` ### FAQ 常見問題 diff --git a/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx b/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx index e24262f..6112851 100644 --- a/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx +++ b/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx @@ -12,6 +12,8 @@ + + diff --git a/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.cs b/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.cs index 5617955..1e73991 100644 --- a/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.cs +++ b/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.cs @@ -1,6 +1,8 @@ -using System; +using MiniExcelLibs; +using System; using System.Collections.Generic; using System.Data; +using System.IO; using System.Linq; using System.Web; using System.Web.UI; @@ -12,15 +14,40 @@ namespace WebFormTest { protected void Page_Load(object sender, EventArgs e) { - var path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/TestIssue223.xlsx"); - var dt = MiniExcelLibs.MiniExcel.QueryAsDataTable(path); - this.GridView1.DataSource = dt; - this.GridView1.DataBind(); + } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } + + protected void Button1_Click(object sender, EventArgs e) + { + var fileName = "Demo.xlsx"; + var sheetName = "Sheet1"; + HttpResponse response = HttpContext.Current.Response; + response.Clear(); + response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + response.AddHeader("Content-Disposition", $"attachment;filename=\"{fileName}\""); + var values = new[] { + new { Column1 = "MiniExcel", Column2 = 1 }, + new { Column1 = "Github", Column2 = 2} + }; + var memoryStream = new MemoryStream(); + memoryStream.SaveAs(values, sheetName: sheetName); + memoryStream.Seek(0, SeekOrigin.Begin); + memoryStream.CopyTo(Response.OutputStream); + response.End(); + } + + + protected void Button2_Click(object sender, EventArgs e) + { + var path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/TestIssue223.xlsx"); + var dt = MiniExcelLibs.MiniExcel.QueryAsDataTable(path); + this.GridView1.DataSource = dt; + this.GridView1.DataBind(); + } } } \ No newline at end of file diff --git a/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.designer.cs b/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.designer.cs index 795a22f..48421d4 100644 --- a/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.designer.cs +++ b/tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.designer.cs @@ -31,5 +31,23 @@ namespace WebFormTest /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.GridView GridView1; + + /// + /// Button1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Button Button1; + + /// + /// Button2 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Button Button2; } }