Skip to content

Commit

Permalink
Added HtmlService
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSuescunPelegay committed Jul 30, 2022
1 parent c3f2f28 commit 055d18e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,23 @@ public async Task<IActionResult> ExportTable([FromQuery] string format, [FromFor

var resultList = await result.ToListAsync();

byte[] dataByteArray;

switch (format)
{
case ExportFormat.Excel:
dataByteArray = await _exportService.ExportToExcel(resultList);

return File(
dataByteArray,
await _exportService.ExportToExcel(resultList),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"data.xlsx");

case ExportFormat.Csv:
dataByteArray = _exportService.ExportToCsv(resultList);

return File(dataByteArray, "application/csv", "data.csv");
return File(_exportService.ExportToCsv(resultList),
"application/csv",
"data.csv");

case ExportFormat.Html:
return File(_exportService.ExportToHtml(resultList),
"application/csv",
"data.html");
}

return null;
Expand Down
1 change: 1 addition & 0 deletions src/jQueryDatatableServerSideNetCore/Data/ExportFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class ExportFormat
public const string Excel = "excel";
public const string Csv = "csv";
public const string CsvDelimiter = ",";
public const string Html = "html";
}
}
2 changes: 2 additions & 0 deletions src/jQueryDatatableServerSideNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using jQueryDatatableServerSideNetCore.Services.CsvService;
using jQueryDatatableServerSideNetCore.Services.ExcelService;
using jQueryDatatableServerSideNetCore.Services.ExportService;
using jQueryDatatableServerSideNetCore.Services.HtmlService;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
Expand All @@ -27,6 +28,7 @@
builder.Services.AddScoped<IExportService, ExportService>();
builder.Services.AddScoped<IExcelService, ExcelService>();
builder.Services.AddScoped<ICsvService, CsvService>();
builder.Services.AddScoped<IHtmlService, HtmlService>();

// Register the Swagger generator, defining 1 or more Swagger documents
builder.Services.AddSwaggerGen(c =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using jQueryDatatableServerSideNetCore.Models.DatabaseModels;
using jQueryDatatableServerSideNetCore.Services.CsvService;
using jQueryDatatableServerSideNetCore.Services.ExcelService;
using jQueryDatatableServerSideNetCore.Services.HtmlService;
using System.Text;

namespace jQueryDatatableServerSideNetCore.Services.ExportService
Expand All @@ -9,11 +10,13 @@ public class ExportService : IExportService
{
private readonly IExcelService _excelService;
private readonly ICsvService _csvService;
private readonly IHtmlService _htmlService;

public ExportService(IExcelService excelService, ICsvService csvService)
public ExportService(IExcelService excelService, ICsvService csvService, IHtmlService htmlService)
{
_excelService = excelService;
_csvService = csvService;
_htmlService = htmlService;
}

public async Task<byte[]> ExportToExcel(List<TestRegister> registers)
Expand All @@ -26,5 +29,9 @@ public byte[] ExportToCsv(List<TestRegister> registers)
return _csvService.Write(registers);
}

public byte[] ExportToHtml(List<TestRegister> registers)
{
return _htmlService.Write(registers);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface IExportService
Task<byte[]> ExportToExcel(List<TestRegister> registers);

byte[] ExportToCsv(List<TestRegister> registers);

byte[] ExportToHtml(List<TestRegister> registers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Text;

namespace jQueryDatatableServerSideNetCore.Services.HtmlService
{
public class HtmlService : IHtmlService
{
public byte[] Write<T>(IList<T> registers)
{
StringBuilder sb = new StringBuilder();

sb.Append("<table>\n");
sb.Append("\t<thead>\n");
sb.Append("\t\t<tr>\n");

Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();

for (var i = 0; i < properties.Length; i++)
{
string value = properties[i].Name;

var attribute = properties[i].GetCustomAttribute(typeof(DisplayAttribute));
if (attribute != null)
{
value = (attribute as DisplayAttribute).Name;
}

sb.Append("\t\t\t<td>");
sb.Append(value);
sb.Append("</td>\n");
}

sb.Append("\t\t</tr>\n");
sb.Append("\t</thead>\n");
sb.Append("\t<tbody>\n");

foreach (var register in registers)
{
sb.Append("\t\t<tr>\n");

for (var i = 0; i < properties.Length; i++)
{
sb.Append("\t\t\t<td>");
sb.Append(properties[i].GetValue(register, null));
sb.Append("</td>\n");
}

sb.Append("\t\t</tr>\n");
}

sb.Append("\t</tbody>\n");
sb.Append("</table>");

return Encoding.ASCII.GetBytes(sb.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace jQueryDatatableServerSideNetCore.Services.HtmlService
{
public interface IHtmlService
{
byte[] Write<T>(IList<T> registers);
}
}
12 changes: 12 additions & 0 deletions src/jQueryDatatableServerSideNetCore/wwwroot/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ $(document).ready(function () {
action: function () {
exportToCsv();
}
},
{
text: 'HTML',
action: function () {
exportToHtml();
}
}
],
// Searching Setups
Expand Down Expand Up @@ -135,5 +141,11 @@ function exportToExcel() {
function exportToCsv() {
renderDownloadForm("csv");

$("#export-to-file-form").submit();
}

function exportToHtml() {
renderDownloadForm("html");

$("#export-to-file-form").submit();
}

0 comments on commit 055d18e

Please sign in to comment.