forked from fingers10/ExcelExport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSVResult.cs
57 lines (46 loc) · 1.79 KB
/
CSVResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Fingers10.ExcelExport.Extensions;
using Fingers10.ExcelExport.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Fingers10.ExcelExport.ActionResults
{
public class CSVResult<T> : IActionResult where T : class
{
private readonly IEnumerable<T> _data;
public string FileName { get; }
private List<ExcelColumnDefinition> Columns { get; set; } = new List<ExcelColumnDefinition>();
public CSVResult(IEnumerable<T> data, string fileName)
{
_data = data;
FileName = fileName;
}
public CSVResult(IEnumerable<T> data, string fileName, params (string name, string label, int order)[] definitions)
{
_data = data;
FileName = fileName;
Columns.SetDefinitions(definitions);
}
public CSVResult(IEnumerable<T> data, string fileName, List<ExcelColumnDefinition> definitions)
{
_data = data;
FileName = fileName;
Columns.SetDefinitions(definitions);
}
public async Task ExecuteResultAsync(ActionContext context)
{
byte[] csvBytes;
if (Columns != null && Columns.Count > 0)
csvBytes = await _data.GenerateCSVForDataAsync(Columns);
else
csvBytes = await _data.GenerateCSVForDataAsync();
WriteExcelFileAsync(context.HttpContext, csvBytes);
}
private async void WriteExcelFileAsync(HttpContext context, byte[] bytes)
{
context.Response.Headers["content-disposition"] = $"attachment; filename={FileName}.csv";
await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
}
}
}