Skip to content

Commit

Permalink
webform doc and test
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed May 8, 2021
1 parent 64071b1 commit a79acd7
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 10 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
48 changes: 45 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 参数
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 常见问题
Expand Down
42 changes: 40 additions & 2 deletions README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 常見問題

Expand Down
2 changes: 2 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
37 changes: 32 additions & 5 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,15 +14,40 @@ public partial class WebForm1 : System.Web.UI.Page
{
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();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a79acd7

Please sign in to comment.