Skip to content

Commit 69f5b81

Browse files
authored
Merge pull request #68 from PandaTechAM/development
exporter of xlsx support table and header freeze
2 parents dbc8ac7 + c5a4292 commit 69f5b81

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

src/FileExporter/Exporters/XlsxExporter.cs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using FileExporter.Rules;
1212
using SpreadCheetah;
1313
using SpreadCheetah.Styling;
14+
using SpreadCheetah.Tables;
1415
using SpreadCheetah.Worksheets;
1516

1617
namespace FileExporter.Exporters;
@@ -76,8 +77,13 @@ private static async Task<byte[]> CreateXlsxFileAsync<T>(IEnumerable<T> dataSlic
7677
var options = new WorksheetOptions();
7778
ApplyColumnWidths(options, columns);
7879

80+
options.FrozenRows = 1;
81+
7982
await spreadsheet.StartWorksheetAsync(sheetName, options, token: cancellationToken);
8083

84+
var table = new Table(TableStyle.Medium2);
85+
spreadsheet.StartTable(table);
86+
8187
var headerStyleId = AddHeaderStyle(spreadsheet);
8288
await AddHeaderRowAsync(spreadsheet, columns, headerStyleId, cancellationToken);
8389

@@ -102,6 +108,7 @@ private static async Task<byte[]> CreateXlsxFileAsync<T>(IEnumerable<T> dataSlic
102108
return ms.ToArray();
103109
}
104110

111+
105112
private static List<ExportColumn> BuildColumns<T>(ExportRule<T> rule)
106113
where T : class
107114
{
@@ -309,26 +316,26 @@ private static Cell CreateCell(object value)
309316
{
310317
return value switch
311318
{
312-
string s => new Cell(s),
313-
bool b => new Cell(b),
314-
int i => new Cell(i),
315-
long l => new Cell(l),
316-
short sh => new Cell(sh),
317-
byte b8 => new Cell(b8),
318-
uint ui => new Cell((double)ui),
319-
ulong ul => new Cell((double)ul),
320-
float f => new Cell((double)f),
321-
double d => new Cell(d),
322-
decimal m => new Cell((double)m),
319+
string s => new Cell(s),
320+
bool b => new Cell(b),
321+
int i => new Cell(i),
322+
long l => new Cell(l),
323+
short sh => new Cell(sh),
324+
byte b8 => new Cell(b8),
325+
uint ui => new Cell((double)ui),
326+
ulong ul => new Cell((double)ul),
327+
float f => new Cell((double)f),
328+
double d => new Cell(d),
329+
decimal m => new Cell((double)m),
323330

324331
DateTime dt => CreateDateCell(dt),
325-
DateOnly d => CreateDateCell(d.ToDateTime(TimeOnly.MinValue)),
326-
TimeOnly t => CreateDateCell(DateTime.Today.Add(t.ToTimeSpan())),
332+
DateOnly d => CreateDateCell(d.ToDateTime(TimeOnly.MinValue)),
333+
TimeOnly t => CreateDateCell(DateTime.Today.Add(t.ToTimeSpan())),
327334

328335
_ => new Cell(value.ToString() ?? string.Empty)
329336
};
330337
}
331-
338+
332339
private static Cell CreateDateCell(DateTime dt)
333340
{
334341
try
@@ -355,9 +362,7 @@ private static async Task<byte[]> CreateMultiSheetXlsxFileAsync<T>(IList<T> list
355362

356363
await using (var spreadsheet = await Spreadsheet.CreateNewAsync(ms, cancellationToken: cancellationToken))
357364
{
358-
// Sanitize once for sheet names
359365
var sanitizedBaseName = rule.FileName.ToValidName(ExportLimits.MaxSheetNameLength);
360-
361366
var headerStyleId = AddHeaderStyle(spreadsheet);
362367

363368
var totalRows = list.Count;
@@ -366,13 +371,18 @@ private static async Task<byte[]> CreateMultiSheetXlsxFileAsync<T>(IList<T> list
366371
for (var offset = 0; offset < totalRows; offset += rowsPerSheet, sheetIndex++)
367372
{
368373
var take = Math.Min(rowsPerSheet, totalRows - offset);
369-
370374
var sheetName = BuildSheetName(sanitizedBaseName, sheetIndex);
371375

372376
var options = new WorksheetOptions();
373377
ApplyColumnWidths(options, columns);
374378

379+
options.FrozenRows = 1;
380+
375381
await spreadsheet.StartWorksheetAsync(sheetName, options, token: cancellationToken);
382+
383+
var table = new Table(TableStyle.Medium2);
384+
spreadsheet.StartTable(table);
385+
376386
await AddHeaderRowAsync(spreadsheet, columns, headerStyleId, cancellationToken);
377387

378388
for (var i = 0; i < take; i++)

src/FileExporter/FileExporter.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<PackageIcon>Logo.jpg</PackageIcon>
1313
<PackageReadmeFile>Readme.md</PackageReadmeFile>
1414

15-
<Version>5.0.1</Version>
15+
<Version>5.0.2</Version>
1616

1717
<Title>PandaTech File Exporter</Title>
1818

@@ -31,7 +31,7 @@
3131
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-file-exporter</RepositoryUrl>
3232

3333
<PackageReleaseNotes>
34-
In case of wrong dates xlsx export will not throw an exception anymore but will export that row with string value instead.
34+
In case of xlsx export, the data is in tables now and first row is freezed.
3535
</PackageReleaseNotes>
3636
</PropertyGroup>
3737

test/FileExporter.Demo/ExportRules/DummyExportRule.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,41 @@ public DummyExportRule()
1111
RuleFor(x => x.RelatedId)
1212
.WriteToColumn("Related Id")
1313
.HasOrder(1);
14-
14+
1515
RuleFor(x => x.Id)
1616
.HasOrder(2);
1717

1818
RuleFor(x => x.Name)
1919
.WriteToColumn("Custom Name - No Description")
20-
.HasOrder(7)
20+
.HasOrder(3)
2121
.WithDefaultValue("TEST");
2222

2323
RuleFor(x => x.CreationDate)
2424
.WriteToColumn("Created At")
25-
.HasWidth(1)
25+
.HasWidth(4)
2626
.HasFormat(ColumnFormatType.DateTime);
2727

2828
RuleFor(x => x.Dto)
2929
.WriteToColumn("Dto")
30+
.HasOrder(5)
3031
.WithDefaultValue("Default value");
3132

33+
3234
RuleFor(x => x.IntEnum)
35+
.HasOrder(6)
3336
.WithEnumFormat(EnumFormatMode.Int);
3437

3538
RuleFor(x => x.StringEnum)
39+
.HasOrder(7)
3640
.WithEnumFormat(EnumFormatMode.Name);
3741

3842
RuleFor(x => x.SomeRate)
43+
.HasOrder(8)
3944
.HasFormat(ColumnFormatType.Percentage)
4045
.HasPrecision(1);
41-
46+
4247
RuleFor(x => x.SomeDollars)
48+
.HasOrder(11)
4349
.HasFormat(ColumnFormatType.Currency)
4450
.HasPrecision(2);
4551
}

0 commit comments

Comments
 (0)