-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathStringCells.cs
220 lines (185 loc) · 6.61 KB
/
StringCells.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using ClosedXML.Excel;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using OfficeOpenXml;
using System.IO.Packaging;
using OpenXmlCell = DocumentFormat.OpenXml.Spreadsheet.Cell;
using OpenXmlCellValue = DocumentFormat.OpenXml.Spreadsheet.CellValues;
namespace SpreadCheetah.Benchmark.Benchmarks;
[SimpleJob(RuntimeMoniker.Net48)]
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net90)]
[MemoryDiagnoser]
public class StringCells : IDisposable
{
private const string SheetName = "Book1";
public Stream Stream { get; set; } = null!;
public IList<IList<string>> Values { get; private set; } = null!;
[Params(20000)]
public int NumberOfRows { get; set; }
public int NumberOfColumns { get; set; } = 10;
[GlobalSetup]
public void GlobalSetup()
{
Values = new List<IList<string>>(NumberOfRows);
for (var row = 1; row <= NumberOfRows; ++row)
{
var cells = new List<string>(NumberOfColumns);
Values.Add(cells);
for (var col = 1; col <= NumberOfColumns; ++col)
{
cells.Add($"{col}-{row}");
}
}
}
[IterationSetup]
public void IterationSetup()
{
Stream = new MemoryStream(2_000_000);
}
[IterationCleanup]
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing) return;
Stream?.Dispose();
Stream = null!;
}
[Benchmark]
public async Task SpreadCheetah()
{
var options = new SpreadCheetahOptions { DefaultDateTimeFormat = null };
await using var spreadsheet = await Spreadsheet.CreateNewAsync(Stream, options);
await spreadsheet.StartWorksheetAsync(SheetName);
var cells = new DataCell[NumberOfColumns];
for (var row = 0; row < Values.Count; ++row)
{
var rowValues = Values[row];
for (var col = 0; col < rowValues.Count; ++col)
{
cells[col] = new DataCell(rowValues[col]);
}
await spreadsheet.AddRowAsync(cells);
}
await spreadsheet.FinishAsync();
}
[Benchmark]
public void EpPlus4()
{
using var package = new ExcelPackage(Stream) { Compression = CompressionLevel.BestSpeed };
var worksheet = package.Workbook.Worksheets.Add(SheetName);
for (var row = 0; row < Values.Count; ++row)
{
var rowValues = Values[row];
for (var col = 0; col < rowValues.Count; ++col)
{
worksheet.Cells[row + 1, col + 1].Value = rowValues[col];
}
}
package.Save();
}
[Benchmark]
public void OpenXmlSax()
{
using var xl = SpreadsheetDocument.Create(Stream, SpreadsheetDocumentType.Workbook);
xl.CompressionOption = CompressionOption.SuperFast;
var workbookPart = xl.AddWorkbookPart();
var wsp = workbookPart.AddNewPart<WorksheetPart>();
var oxw = OpenXmlWriter.Create(wsp);
oxw.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Worksheet());
oxw.WriteStartElement(new SheetData());
var rowObject = new Row();
var cellAttributes = new[] { new OpenXmlAttribute("t", "", "inlineStr") };
var cell = new OpenXmlCell();
var inlineString = new InlineString();
var rowAttributes = new OpenXmlAttribute[1];
for (var row = 0; row < NumberOfRows; ++row)
{
rowAttributes[0] = new OpenXmlAttribute("r", "", (row + 1).ToString());
oxw.WriteStartElement(rowObject, rowAttributes);
var rowValues = Values[row];
for (var col = 0; col < rowValues.Count; ++col)
{
oxw.WriteStartElement(cell, cellAttributes);
oxw.WriteStartElement(inlineString);
oxw.WriteElement(new Text(rowValues[col]));
oxw.WriteEndElement();
oxw.WriteEndElement();
}
oxw.WriteEndElement();
}
oxw.WriteEndElement();
oxw.WriteEndElement();
oxw.Close();
oxw = OpenXmlWriter.Create(workbookPart);
oxw.WriteStartElement(new Workbook());
oxw.WriteStartElement(new Sheets());
oxw.WriteElement(new Sheet()
{
Name = "Sheet1",
SheetId = 1,
Id = workbookPart.GetIdOfPart(wsp)
});
oxw.WriteEndElement();
oxw.WriteEndElement();
oxw.Close();
}
[Benchmark]
public void OpenXmlDom()
{
using var xl = SpreadsheetDocument.Create(Stream, SpreadsheetDocumentType.Workbook);
xl.CompressionOption = CompressionOption.SuperFast;
var workbookpart = xl.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();
worksheetPart.Worksheet.AppendChild(sheetData);
var sheets = workbookpart.Workbook.AppendChild(new Sheets());
var sheet = new Sheet
{
Id = workbookpart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = SheetName
};
var cells = new OpenXmlCell[NumberOfColumns];
for (var row = 0; row < Values.Count; ++row)
{
var rowValues = Values[row];
for (var col = 0; col < rowValues.Count; ++col)
{
var inlineString = new InlineString();
inlineString.AppendChild(new Text(rowValues[col]));
var cell = new OpenXmlCell { DataType = OpenXmlCellValue.InlineString };
cell.AppendChild(inlineString);
cells[col] = cell;
}
var rowObject = new Row(cells) { RowIndex = (uint)row + 1 };
sheetData.AppendChild(rowObject);
}
sheets.AppendChild(sheet);
workbookpart.Workbook.Save();
}
[Benchmark]
public void ClosedXml()
{
using var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add(SheetName);
for (var row = 0; row < Values.Count; ++row)
{
var rowValues = Values[row];
for (var col = 0; col < rowValues.Count; ++col)
{
worksheet.Cell(row + 1, col + 1).SetValue(rowValues[col]);
}
}
workbook.SaveAs(Stream);
}
}