diff --git a/src/jQueryDatatableServerSideNetCore/Controllers/TestRegistersController.cs b/src/jQueryDatatableServerSideNetCore/Controllers/TestRegistersController.cs index 9f350a4..c4b8073 100644 --- a/src/jQueryDatatableServerSideNetCore/Controllers/TestRegistersController.cs +++ b/src/jQueryDatatableServerSideNetCore/Controllers/TestRegistersController.cs @@ -140,22 +140,23 @@ public async Task 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; diff --git a/src/jQueryDatatableServerSideNetCore/Data/ExportFormat.cs b/src/jQueryDatatableServerSideNetCore/Data/ExportFormat.cs index b263047..dacbbaa 100644 --- a/src/jQueryDatatableServerSideNetCore/Data/ExportFormat.cs +++ b/src/jQueryDatatableServerSideNetCore/Data/ExportFormat.cs @@ -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"; } } diff --git a/src/jQueryDatatableServerSideNetCore/Program.cs b/src/jQueryDatatableServerSideNetCore/Program.cs index 908b5f4..6ab8c09 100644 --- a/src/jQueryDatatableServerSideNetCore/Program.cs +++ b/src/jQueryDatatableServerSideNetCore/Program.cs @@ -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; @@ -27,6 +28,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Register the Swagger generator, defining 1 or more Swagger documents builder.Services.AddSwaggerGen(c => diff --git a/src/jQueryDatatableServerSideNetCore/Services/ExportService/ExportService.cs b/src/jQueryDatatableServerSideNetCore/Services/ExportService/ExportService.cs index 23cd37d..5ae9d21 100644 --- a/src/jQueryDatatableServerSideNetCore/Services/ExportService/ExportService.cs +++ b/src/jQueryDatatableServerSideNetCore/Services/ExportService/ExportService.cs @@ -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 @@ -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 ExportToExcel(List registers) @@ -26,5 +29,9 @@ public byte[] ExportToCsv(List registers) return _csvService.Write(registers); } + public byte[] ExportToHtml(List registers) + { + return _htmlService.Write(registers); + } } } diff --git a/src/jQueryDatatableServerSideNetCore/Services/ExportService/IExportService.cs b/src/jQueryDatatableServerSideNetCore/Services/ExportService/IExportService.cs index e1da1f9..3408da0 100644 --- a/src/jQueryDatatableServerSideNetCore/Services/ExportService/IExportService.cs +++ b/src/jQueryDatatableServerSideNetCore/Services/ExportService/IExportService.cs @@ -10,5 +10,7 @@ public interface IExportService Task ExportToExcel(List registers); byte[] ExportToCsv(List registers); + + byte[] ExportToHtml(List registers); } } diff --git a/src/jQueryDatatableServerSideNetCore/Services/HtmlService/HtmlService.cs b/src/jQueryDatatableServerSideNetCore/Services/HtmlService/HtmlService.cs new file mode 100644 index 0000000..90413c6 --- /dev/null +++ b/src/jQueryDatatableServerSideNetCore/Services/HtmlService/HtmlService.cs @@ -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(IList registers) + { + StringBuilder sb = new StringBuilder(); + + sb.Append("\n"); + sb.Append("\t\n"); + sb.Append("\t\t\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\n"); + } + + sb.Append("\t\t\n"); + sb.Append("\t\n"); + sb.Append("\t\n"); + + foreach (var register in registers) + { + sb.Append("\t\t\n"); + + for (var i = 0; i < properties.Length; i++) + { + sb.Append("\t\t\t\n"); + } + + sb.Append("\t\t\n"); + } + + sb.Append("\t\n"); + sb.Append("
"); + sb.Append(value); + sb.Append("
"); + sb.Append(properties[i].GetValue(register, null)); + sb.Append("
"); + + return Encoding.ASCII.GetBytes(sb.ToString()); + } + } +} diff --git a/src/jQueryDatatableServerSideNetCore/Services/HtmlService/IHtmlService.cs b/src/jQueryDatatableServerSideNetCore/Services/HtmlService/IHtmlService.cs new file mode 100644 index 0000000..744a8b6 --- /dev/null +++ b/src/jQueryDatatableServerSideNetCore/Services/HtmlService/IHtmlService.cs @@ -0,0 +1,7 @@ +namespace jQueryDatatableServerSideNetCore.Services.HtmlService +{ + public interface IHtmlService + { + byte[] Write(IList registers); + } +} diff --git a/src/jQueryDatatableServerSideNetCore/wwwroot/js/app.js b/src/jQueryDatatableServerSideNetCore/wwwroot/js/app.js index 29943ba..b624a2c 100644 --- a/src/jQueryDatatableServerSideNetCore/wwwroot/js/app.js +++ b/src/jQueryDatatableServerSideNetCore/wwwroot/js/app.js @@ -27,6 +27,12 @@ $(document).ready(function () { action: function () { exportToCsv(); } + }, + { + text: 'HTML', + action: function () { + exportToHtml(); + } } ], // Searching Setups @@ -135,5 +141,11 @@ function exportToExcel() { function exportToCsv() { renderDownloadForm("csv"); + $("#export-to-file-form").submit(); +} + +function exportToHtml() { + renderDownloadForm("html"); + $("#export-to-file-form").submit(); } \ No newline at end of file