//Add Below given Namespaces
using AutoMapper;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System;
using ClosedXML.Excel;
using System.IO;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
[HttpGet("download-audit-logs")]
public async Task<FileResult> DownloadAuditLogs([FromQuery] QueryParameters queryParam)
{
string FileName = "AuditLogs";
var info = Helper.GetDetail;
var logs = await _auditlogService.ExportAuditLogs(info, queryParam);
//Export in CSV file
if (queryParam.ExportTo.ToString().Equals("CSV"))
{
var builder = new StringBuilder();
builder.AppendLine("Event, User ID,Event Date,Service Name,Change Summmary,Created,Change Object");
foreach (var log in logs.Items)
{
builder.AppendLine($"{log.EventName},{log.UserId},{log.EventDate},{log.ServiceName},{log.ChangeSummary},{log.Created},{log.ChangeObjectinJson}");
}
return File(Encoding.UTF8.GetBytes(builder.ToString()), "text/csv", FileName + ".csv");
}
//Export in Excel File
else if (queryParam.ExportTo.ToString().Equals("XLS"))
{
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("AuditLogs");
var currentRow = 1;
worksheet.Cell(currentRow, 1).Value = "Event";
worksheet.Cell(currentRow, 2).Value = "User ID";
worksheet.Cell(currentRow, 3).Value = "Event Date";
worksheet.Cell(currentRow, 4).Value = "Service Name";
worksheet.Cell(currentRow, 5).Value = "Change Summary";
worksheet.Cell(currentRow, 6).Value = "Created";
worksheet.Cell(currentRow, 7).Value = "Change Object";
foreach (var log in logs.Items)
{
currentRow++;
worksheet.Cell(currentRow, 1).Value = log.EventName;
worksheet.Cell(currentRow, 2).Value = log.UserId;
worksheet.Cell(currentRow, 3).Value = log.EventDate;
worksheet.Cell(currentRow, 4).Value = log.ServiceName;
worksheet.Cell(currentRow, 5).Value = log.ChangeSummary;
worksheet.Cell(currentRow, 6).Value = log.Created;
worksheet.Cell(currentRow, 7).Value = log.ChangeObjectinJson;
}
using (var stream = new MemoryStream())
{
workbook.SaveAs(stream);
var content = stream.ToArray();
return File(
content,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
FileName + ".xlsx");
}
}
}
//Export in PDF
else if(queryParam.ExportTo.ToString().Equals("PDF"))
{
string contentType = "application/pdf";
StringBuilder sb = new StringBuilder();
sb.Append("<table border='1' cellpadding='5' cellspacing='0' style='solid #ccc;font-size: 9pt;font-family:Arial'>");
sb.Append("<thead >");
sb.Append("<tr>");
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Event</th>");
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>User ID</th>");
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Event Date</th>");
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Service Name</th>");
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Change Summary</th>");
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Created</th>");
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Change Object</th>");
sb.Append("</tr>");
sb.Append("</thead>");
sb.Append("<tbody>");
var html = "";
foreach (var log in logs.Items)
{
html += "<tr>";
html += "<td>" + log.EventName + "</td>";
html += "<td>" + log.UserId + "</td>";
html += "<td>" + log.EventDate + "</td>";
html += "<td>" + log.ServiceName + "</td>";
html += "<td>" + log.ChangeSummary + "</td>";
html += "<td>" + log.Created + "</td>";
html += "<td>" + log.ChangeObjectinJson + "</td>";
html += "</tr>";
}
sb.Append(html);
sb.Append("</tbody>");
sb.Append("</table>");
StringReader sr = new StringReader(sb.ToString());
iTextSharp.text.Document pdfDoc;
pdfDoc = new iTextSharp.text.Document(PageSize.A4.Rotate(), 3, 3, 5, 3);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
writer.CloseStream = false;
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
return File(bytes, contentType, FileName + ".pdf");
}
}
else
{
var content = "No Data Found";
return File(content,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",FileName + ".xlsx");
}
}
Input Parameter Model
___________________________
public class QueryParameters
{
public string search { get; set; }
[Required]
public ExportType ExportTo { get; set; }
public ExportSize PDFPageSize { get; set; }
public DateTime? From { get; set; }
public DateTime? To { get; set; }
}
public enum ExportType
{
CSV,
PDF,
XLS
}
public enum ExportSize
{
A5,
A4,
A3,
A2,
A1
}
No comments:
Post a Comment