Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
[Back] SharedStringTable
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloento committed Feb 7, 2024
1 parent 8deda59 commit cac538e
Showing 1 changed file with 93 additions and 27 deletions.
120 changes: 93 additions & 27 deletions TSystems.LoveOTC/AdminHub/Order/Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public async IAsyncEnumerable<byte[]> ExportOrder() {
};
sheets.Append(sheet);

var timestamp = new Row();
sheetData.AppendChild(timestamp);
timestamp.Append(new Cell {
DataType = CellValues.String,
CellValue = new($"This order sheet was exported on {DateTime.Now:yyyy-MM-dd HH:mm}")
});

var headerRow = new Row();
sheetData.AppendChild(headerRow);
headerRow.Append(headers.Select(x => new Cell {
Expand All @@ -69,6 +76,12 @@ public async IAsyncEnumerable<byte[]> ExportOrder() {
.Distinct()
.ToArrayAsync();

var sharedStringTablePart = workbookPart.GetPartsOfType<SharedStringTablePart>().Any()
? workbookPart.GetPartsOfType<SharedStringTablePart>().First()
: workbookPart.AddNewPart<SharedStringTablePart>();

sharedStringTablePart.SharedStringTable ??= new();

foreach (var userId in userIds) {
var records = this.Db.OrderCombos
.Where(x => x.Order.UserId == userId)
Expand All @@ -86,18 +99,26 @@ public async IAsyncEnumerable<byte[]> ExportOrder() {
.AsAsyncEnumerable();

var prevId = 0u;
var first = true;

await foreach (var record in records) {
var order = record.Order;
var user = order.User;
var combo = record.Combo;
var currId = order.OrderId;

var data = new List<string>(12) {
currId.ToString(),
order.CreateAt.ToString("yyyy-MM-dd HH:mm"),
combo.Product.Name
var data = new List<OpenXmlElement>(12) {
new Cell {
DataType = CellValues.Number,
CellValue = new((decimal)currId)
},
new Cell {
DataType = CellValues.String,
CellValue = new(order.CreateAt.ToString("yyyy-MM-dd HH:mm"))
},
new Cell {
DataType = CellValues.SharedString,
CellValue = new(shared(combo.Product.Name))
}
};

var types = combo.Types.Aggregate(
Expand All @@ -112,24 +133,48 @@ public async IAsyncEnumerable<byte[]> ExportOrder() {
.ToString();

data.AddRange([
types,
record.Quantity.ToString(),
order.Status.ToString(),
order.TrackingNumber ?? "/",
user.Name
new Cell {
DataType = CellValues.String,
CellValue = new(types)
},
new Cell {
DataType = CellValues.Number,
CellValue = new((decimal)record.Quantity)
},
new Cell {
DataType = CellValues.SharedString,
CellValue = new(shared(order.Status.ToString()))
}
]);

if (first) {
data.AddRange([
user.EMail,
user.Phone,
user.Address
]);
first = false;
} else
data.AddRange([
"-", "-", "-"
]);
data.Add(string.IsNullOrWhiteSpace(order.TrackingNumber)
? new Cell {
DataType = CellValues.String,
CellValue = new("/")
}
: new() {
DataType = CellValues.SharedString,
CellValue = new(shared(order.TrackingNumber))
});

data.AddRange([
new Cell {
DataType = CellValues.SharedString,
CellValue = new(shared(user.Name))
},
new Cell {
DataType = CellValues.SharedString,
CellValue = new(shared(user.EMail))
},
new Cell {
DataType = CellValues.SharedString,
CellValue = new(shared(user.Phone))
},
new Cell {
DataType = CellValues.SharedString,
CellValue = new(shared(user.Address))
}
]);

if (prevId != currId) {
var cmts = order.Comments
Expand All @@ -147,16 +192,19 @@ public async IAsyncEnumerable<byte[]> ExportOrder() {
})
.ToString();

data.Add(cmts);
data.Add(new Cell {
DataType = CellValues.String,
CellValue = new(cmts)
});
} else
data.Add("-");
data.Add(new Cell {
DataType = CellValues.String,
CellValue = new("-")
});

var row = new Row();
sheetData.AppendChild(row);
row.Append(data.Select(x => new Cell {
DataType = CellValues.String,
CellValue = new(x)
}));
row.Append(data);

prevId = currId;
}
Expand All @@ -171,5 +219,23 @@ public async IAsyncEnumerable<byte[]> ExportOrder() {

while ((bytesRead = await stream.ReadAsync(buffer)) > 0)
yield return buffer[..bytesRead];

yield break;

int shared(string text) {
var i = 0;

foreach (var item in sharedStringTablePart!.SharedStringTable.Elements<SharedStringItem>()) {
if (item.InnerText == text)
return i;

i++;
}

sharedStringTablePart.SharedStringTable.AppendChild(new SharedStringItem(new Text(text)));
sharedStringTablePart.SharedStringTable.Save();

return i;
}
}
}

0 comments on commit cac538e

Please sign in to comment.