ReportsPrintLogExcelHelper.cs
1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using ClosedXML.Excel;
using FoodLabeling.Application.Contracts.Dtos.Reports;
namespace FoodLabeling.Application.Helpers;
/// <summary>
/// Reports — Print Log 全量导出 Excel(列与 Web Print Log 表头对齐)
/// </summary>
public static class ReportsPrintLogExcelHelper
{
/// <summary>导出表头(与 UI 一致)</summary>
public static readonly string[] Headers =
{
"Label ID", "Product Name", "Product Category", "Label Category", "Template", "Printed at", "Printed by", "Location", "Expiration"
};
/// <summary>
/// 将 Print Log 行写入 xlsx 内存流(工作表名 <c>Print Log</c>)。
/// </summary>
public static MemoryStream BuildWorkbook(IReadOnlyList<ReportsPrintLogListItemDto> rows)
{
var ms = new MemoryStream();
using var wb = new XLWorkbook();
var ws = wb.AddWorksheet("Print Log");
for (var i = 0; i < Headers.Length; i++)
{
ws.Cell(1, i + 1).Value = Headers[i];
ws.Cell(1, i + 1).Style.Font.Bold = true;
}
var r = 2;
foreach (var x in rows)
{
ws.Cell(r, 1).Value = x.LabelCode ?? string.Empty;
ws.Cell(r, 2).Value = x.ProductName ?? string.Empty;
ws.Cell(r, 3).Value = x.ProductCategoryName ?? string.Empty;
ws.Cell(r, 4).Value = x.LabelCategoryName ?? string.Empty;
ws.Cell(r, 5).Value = x.TemplateText ?? string.Empty;
ws.Cell(r, 6).Value = ReportsDateTimeDisplayHelper.FormatPrintedAt(x.PrintedAt);
ws.Cell(r, 7).Value = x.PrintedByName ?? string.Empty;
ws.Cell(r, 8).Value = x.LocationText ?? string.Empty;
ws.Cell(r, 9).Value = x.ExpiryDateText ?? string.Empty;
r++;
}
ws.Columns().AdjustToContents();
wb.SaveAs(ms);
ms.Position = 0;
return ms;
}
}