mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
webform doc and test
This commit is contained in:
parent
64071b196e
commit
a79acd72a9
55
README.md
55
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<T> Page<T>(IEnumerable<T> 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?
|
||||
|
@ -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<T> Page<T>(IEnumerable<T> 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 常见问题
|
||||
|
@ -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<T> Page<T>(IEnumerable<T> 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 常見問題
|
||||
|
||||
|
@ -12,6 +12,8 @@
|
||||
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
|
||||
</asp:GridView>
|
||||
</div>
|
||||
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Download Excel" />
|
||||
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="GridViewBind" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -31,5 +31,23 @@ namespace WebFormTest
|
||||
/// To modify move field declaration from designer file to code-behind file.
|
||||
/// </remarks>
|
||||
protected global::System.Web.UI.WebControls.GridView GridView1;
|
||||
|
||||
/// <summary>
|
||||
/// Button1 control.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Auto-generated field.
|
||||
/// To modify move field declaration from designer file to code-behind file.
|
||||
/// </remarks>
|
||||
protected global::System.Web.UI.WebControls.Button Button1;
|
||||
|
||||
/// <summary>
|
||||
/// Button2 control.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Auto-generated field.
|
||||
/// To modify move field declaration from designer file to code-behind file.
|
||||
/// </remarks>
|
||||
protected global::System.Web.UI.WebControls.Button Button2;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user