Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Column names in Excel Export can be configured using the below attributes
To customize the Excel Column display in your report, use the following attribute
* `[IncludeInReport]`
* `[NestedIncludeInReport]`
* `[IncludeAllInReportAttribute]`
* `[ExcludeFromReportAttribute]`

And here are the options,

Expand Down Expand Up @@ -145,6 +147,135 @@ public async Task<IActionResult> OnGetCSVAsync()
}
```

## Include All Modal Columns In Report using IncludeAllInReportAttribute

```c#
[IncludeAllInReport]
public class DemoExcel
{
public int Id { get; set; }

public string Name { get; set; }

public string Position { get; set; }

public string Offices { get; set; }
}
```

## Exclude some Columns from Report using ExcludeFromReportAttribute

for example here offices column is excluded
```c#
[IncludeAllInReport]
public class DemoExcel
{
public int Id { get; set; }

public string Name { get; set; }

public string Position { get; set; }

[ExcludeFromReport]
public string Offices { get; set; }
}
```



## Pass columns definitions as a parameter to the constructor

For example if you have this Modal class
```c#
public class Employee
{
public string EmployeeName { get; set; }
public DateTime? StartDate { get; set; }
public long? BasicSalary { get; set; }
}
```

## You have two ways to pass columns definitions

## 1 - Pass columns definitions as tuple params

Excel
```c#
public async Task<IActionResult> OnGetExcelAsync()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new ExcelResult<Employee>(
data: results,
sheetName: "Fingers10",
fileName: "Fingers10",
("EmployeeName", "Employee Name", 1), // prop name, label, order
("StartDate", "Start Date", 2),
("BasicSalary", "Basic Salary", 3)
);
}
```

* CSV
```c#
public async Task<IActionResult> OnGetExcelAsync()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new CSVResult<Employee>(
data: results,
fileName: "Fingers10",
("EmployeeName", "Employee Name", 1), // prop name, label, order
("StartDate", "Start Date", 2),
("BasicSalary", "Basic Salary", 3)
);
}
```

## 2 - Pass columns definitions as List of ExcelColumnDefinition

## in this way the columns are ordered based on the order of the passed list

Excel
```c#
public async Task<IActionResult> OnGetExcelAsync()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new ExcelResult<Employee>(
data: results,
sheetName: "Fingers10",
fileName: "Fingers10",
new List<ExcelColumnDefinition>()
{
new ("EmployeeName", "Employee Name"),
new ("StartDate", "Start Date"),
new ("BasicSalary", "Basic Salary")
}
);
}
```

* CSV
```c#
public async Task<IActionResult> OnGetExcelAsync()
{
// Get you IEnumerable<T> data
var results = await _demoService.GetDataAsync();
return new CSVResult<Employee>(
data: results,
fileName: "Fingers10",
new List<ExcelColumnDefinition>()
{
new ("EmployeeName", "Employee Name"),
new ("StartDate", "Start Date"),
new ("BasicSalary", "Basic Salary")
}
);
}
```


# Target Platform
* .Net Standard 2.0

Expand Down
42 changes: 27 additions & 15 deletions src/ApplicationCore/ActionResults/CSVResult.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Fingers10.ExcelExport.Extensions;
using Fingers10.ExcelExport.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand All @@ -10,30 +10,42 @@ 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 string FileName { get; }
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)
{
try
{
var csvBytes = await _data.GenerateCSVForDataAsync();
WriteExcelFileAsync(context.HttpContext, csvBytes);

}
catch (Exception e)
{
Console.WriteLine(e);

var errorBytes = await new List<T>().GenerateCSVForDataAsync();
WriteExcelFileAsync(context.HttpContext, errorBytes);
}
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)
Expand Down
45 changes: 29 additions & 16 deletions src/ApplicationCore/ActionResults/ExcelResult.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Fingers10.ExcelExport.Extensions;
using Fingers10.ExcelExport.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand All @@ -10,6 +10,9 @@ namespace Fingers10.ExcelExport.ActionResults
public class ExcelResult<T> : IActionResult where T : class
{
private readonly IEnumerable<T> _data;
public string SheetName { get; }
public string FileName { get; }
private List<ExcelColumnDefinition> Columns { get; set; } = new List<ExcelColumnDefinition>();

public ExcelResult(IEnumerable<T> data, string sheetName, string fileName)
{
Expand All @@ -18,24 +21,34 @@ public ExcelResult(IEnumerable<T> data, string sheetName, string fileName)
FileName = fileName;
}

public string SheetName { get; }
public string FileName { get; }
public ExcelResult(IEnumerable<T> data, string sheetName, string fileName, List<ExcelColumnDefinition> definitions)
{
_data = data;
SheetName = sheetName;
FileName = fileName;

Columns.SetDefinitions(definitions);
}

public ExcelResult(IEnumerable<T> data, string sheetName, string fileName, params (string name, string label, int order)[] definitions)
{
_data = data;
SheetName = sheetName;
FileName = fileName;

Columns.SetDefinitions(definitions);
}

public async Task ExecuteResultAsync(ActionContext context)
{
try
{
var excelBytes = await _data.GenerateExcelForDataTableAsync(SheetName);
WriteExcelFileAsync(context.HttpContext, excelBytes);

}
catch (Exception e)
{
Console.WriteLine(e);

var errorBytes = await new List<T>().GenerateExcelForDataTableAsync(SheetName);
WriteExcelFileAsync(context.HttpContext, errorBytes);
}
byte[] excelBytes;

if (Columns != null && Columns.Count > 0)
excelBytes = await _data.GenerateExcelForDataTableAsync(SheetName, Columns);
else
excelBytes = await _data.GenerateExcelForDataTableAsync(SheetName);

WriteExcelFileAsync(context.HttpContext, excelBytes);
}

private async void WriteExcelFileAsync(HttpContext context, byte[] bytes)
Expand Down
12 changes: 6 additions & 6 deletions src/ApplicationCore/ApplicationCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
<Description>Classes to generate Excel Report in ASP.NET</Description>
<PackageTags>Excel Export, CSV Export</PackageTags>
<PackageProjectUrl>https://github.com/fingers10/ExcelExport</PackageProjectUrl>
<RepositoryUrl>https://github.com/fingers10/ExcelExport</RepositoryUrl>
<RepositoryUrl>https://github.com/haithambasim/ExcelExport</RepositoryUrl>
<RepositoryType>Public</RepositoryType>
<PackageReleaseNotes>1. Added support for CSV Export
2. Updated Nuget Dependencies
3. Removed Package Icon</PackageReleaseNotes>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageLicenseExpression></PackageLicenseExpression>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<Copyright>Copyright (c) 2019 Abdul Rahman (@fingers10)</Copyright>
<Version>3.0.0</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<Version>1.0.3</Version>
<AssemblyVersion>0.0.0.0</AssemblyVersion>
<PackageId>HBH.Fingers10.ExcelExport</PackageId>
<Product>HBH.Fingers10.ExcelExport</Product>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/ApplicationCore/Attributes/ExcludeFromReportAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Fingers10.ExcelExport.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class ExcludeFromReportAttribute : Attribute
{
}
}
9 changes: 9 additions & 0 deletions src/ApplicationCore/Attributes/IncludeAllInReportAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Fingers10.ExcelExport.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class IncludeAllInReportAttribute : Attribute
{
}
}
Loading