121 lines
5.0 KiB
C#
121 lines
5.0 KiB
C#
using System.Globalization;
|
|
using ClosedXML.Excel;
|
|
using Inventory.Core;
|
|
using QuestPDF.Fluent;
|
|
using QuestPDF.Helpers;
|
|
using QuestPDF.Infrastructure;
|
|
|
|
namespace Inventory.Api.Services;
|
|
|
|
public static class ReportGenerator
|
|
{
|
|
public static byte[] GenerateExcel(IEnumerable<Device> devices)
|
|
{
|
|
using var workbook = new XLWorkbook();
|
|
var worksheet = workbook.Worksheets.Add("Devices");
|
|
|
|
// Headers
|
|
worksheet.Cell(1, 1).Value = "Computer Name";
|
|
worksheet.Cell(1, 2).Value = "Device Type";
|
|
worksheet.Cell(1, 3).Value = "Location";
|
|
worksheet.Cell(1, 4).Value = "Processor";
|
|
worksheet.Cell(1, 5).Value = "RAM";
|
|
worksheet.Cell(1, 6).Value = "OS Version";
|
|
worksheet.Cell(1, 7).Value = "Serial Number";
|
|
worksheet.Cell(1, 8).Value = "Last Seen";
|
|
|
|
var header = worksheet.Row(1);
|
|
header.Style.Font.Bold = true;
|
|
header.Style.Fill.BackgroundColor = XLColor.LightGray;
|
|
|
|
// Data
|
|
var row = 2;
|
|
foreach (var device in devices)
|
|
{
|
|
worksheet.Cell(row, 1).Value = device.ComputerName;
|
|
worksheet.Cell(row, 2).Value = device.DeviceType;
|
|
worksheet.Cell(row, 3).Value = device.Location;
|
|
worksheet.Cell(row, 4).Value = device.Processor;
|
|
worksheet.Cell(row, 5).Value = device.RAM;
|
|
worksheet.Cell(row, 6).Value = device.OSVersion;
|
|
worksheet.Cell(row, 7).Value = device.SerialNumber;
|
|
worksheet.Cell(row, 8).Value = device.LastSeen.ToString("g", CultureInfo.InvariantCulture);
|
|
row++;
|
|
}
|
|
|
|
worksheet.Columns().AdjustToContents();
|
|
|
|
using var stream = new MemoryStream();
|
|
workbook.SaveAs(stream);
|
|
return stream.ToArray();
|
|
}
|
|
|
|
public static byte[] GeneratePdf(IEnumerable<Device> devices)
|
|
{
|
|
return Document.Create(container =>
|
|
{
|
|
container.Page(page =>
|
|
{
|
|
page.Size(PageSizes.A4.Landscape());
|
|
page.Margin(30);
|
|
|
|
page.Header()
|
|
.Text("Device Inventory Report")
|
|
.SemiBold().FontSize(20).FontColor(Colors.Blue.Medium);
|
|
|
|
page.Content()
|
|
.Table(table =>
|
|
{
|
|
table.ColumnsDefinition(columns =>
|
|
{
|
|
columns.RelativeColumn(2); // Computer Name
|
|
columns.RelativeColumn(1); // Type
|
|
columns.RelativeColumn(1.5f); // Location
|
|
columns.RelativeColumn(3); // Processor
|
|
columns.RelativeColumn(1); // RAM
|
|
columns.RelativeColumn(2); // OS
|
|
columns.RelativeColumn(2); // Last Seen
|
|
});
|
|
|
|
table.Header(header =>
|
|
{
|
|
// This is the correct way to style a cell's container.
|
|
// This function takes an IContainer and returns a styled IContainer.
|
|
static IContainer HeaderCellStyle(IContainer container)
|
|
{
|
|
return container.Background(Colors.Grey.Lighten3).PaddingVertical(5).PaddingHorizontal(10);
|
|
}
|
|
|
|
// Apply the style to the cell's element, then add the bolded text.
|
|
header.Cell().Element(HeaderCellStyle).Text("Computer Name").Bold();
|
|
header.Cell().Element(HeaderCellStyle).Text("Type").Bold();
|
|
header.Cell().Element(HeaderCellStyle).Text("Location").Bold();
|
|
header.Cell().Element(HeaderCellStyle).Text("Processor").Bold();
|
|
header.Cell().Element(HeaderCellStyle).Text("RAM").Bold();
|
|
header.Cell().Element(HeaderCellStyle).Text("OS").Bold();
|
|
header.Cell().Element(HeaderCellStyle).Text("Last Seen").Bold();
|
|
});
|
|
|
|
foreach (var device in devices)
|
|
{
|
|
table.Cell().Text(device.ComputerName);
|
|
table.Cell().Text(device.DeviceType);
|
|
table.Cell().Text(device.Location);
|
|
table.Cell().Text(device.Processor);
|
|
table.Cell().Text(device.RAM);
|
|
table.Cell().Text(device.OSVersion);
|
|
table.Cell().Text(device.LastSeen.ToString("g", CultureInfo.InvariantCulture));
|
|
}
|
|
});
|
|
|
|
page.Footer()
|
|
.AlignCenter()
|
|
.Text(x =>
|
|
{
|
|
x.Span("Page ");
|
|
x.CurrentPageNumber();
|
|
});
|
|
});
|
|
}).GeneratePdf();
|
|
}
|
|
} |