-
Notifications
You must be signed in to change notification settings - Fork 135
/
ExcelWriter.cs
266 lines (235 loc) · 8.97 KB
/
ExcelWriter.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using GridBlazor.Resources;
using GridShared;
using GridShared.Columns;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace GridBlazor
{
public class ExcelCell
{
private string _content;
public string Content {
get { return _content; }
set {
_content = RemoveInvalidXmlChars(value);
}
}
public int ColumnIndex { get; set; }
public int RowIndex { get; set; }
public int ColSpan { get; set; }
public int RowSpan { get; set; }
public CellValues Type { get; set; }
public ExcelCell()
{
}
public ExcelCell(string value, Type type = null)
{
Content = value;
ColSpan = 1;
RowSpan = 1;
if(type == null)
Type = CellValues.InlineString;
else if (type == typeof(Int32) || type == typeof(double) || type == typeof(decimal) || type == typeof(byte)
|| type == typeof(Single) || type == typeof(float) || type == typeof(Int64) || type == typeof(Int16)
|| type == typeof(UInt64) || type == typeof(UInt32) || type == typeof(UInt16))
Type = CellValues.Number;
else if (type == typeof(DateTime) || type == typeof(DateTimeOffset) || type == typeof(decimal) || type == typeof(byte)
|| type == typeof(Single) || type == typeof(float) || type == typeof(Int64) || type == typeof(Int16)
|| type == typeof(UInt64) || type == typeof(UInt32) || type == typeof(UInt16))
Type = CellValues.Date;
else if(type == typeof(bool))
Type = CellValues.Boolean;
else
Type = CellValues.InlineString;
}
private string RemoveInvalidXmlChars(string content)
{
return new string(content?.Where(ch => System.Xml.XmlConvert.IsXmlChar(ch)).ToArray());
}
}
public class ExcelStatus
{
public string Message { get; set; }
public bool Success
{
get { return string.IsNullOrWhiteSpace(Message); }
}
}
public class ExcelData
{
public ExcelStatus Status { get; set; }
public DocumentFormat.OpenXml.Spreadsheet.Columns ColumnConfigurations { get; set; }
public List<List<ExcelCell>> Cells { get; set; }
public string SheetName { get; set; }
public bool ExtendedSheet { get; set; }
public ExcelData() : this(false)
{ }
// extended sheet allows merged cells and enforce column and row index use
public ExcelData(bool extendedSheet)
{
Status = new ExcelStatus();
Cells = new List<List<ExcelCell>>();
ExtendedSheet = extendedSheet;
}
}
public class ExcelWriter
{
private string ColumnLetter(int colIndex)
{
int div = colIndex + 1;
string colLetter = string.Empty;
int mod = 0;
while (div > 0)
{
mod = (div - 1) % 26;
colLetter = (char)(65 + mod) + colLetter;
div = (int)((div - mod) / 26);
}
return colLetter;
}
private Cell CreateCell(string header, UInt32 index, string text, CellValues type)
{
Cell cell;
if (type == CellValues.Number && double.TryParse(text, out double number))
{
cell = new Cell
{
DataType = CellValues.Number,
CellReference = header + index,
CellValue = new CellValue(number.ToString(CultureInfo.InvariantCulture))
};
}
else
{
cell = new Cell
{
DataType = CellValues.InlineString,
CellReference = header + index
};
var istring = new InlineString();
var t = new Text { Text = text };
istring.AppendChild(t);
cell.AppendChild(istring);
}
return cell;
}
private MergeCell CreateMergeCell(ExcelCell excelCell)
{
MergeCell mergeCell = new MergeCell
{
Reference = new StringValue(ColumnLetter(excelCell.ColumnIndex)
+ (excelCell.RowIndex + 1) + ":"
+ ColumnLetter(excelCell.ColumnIndex + excelCell.ColSpan - 1) +
+(excelCell.RowIndex + excelCell.RowSpan)),
};
return mergeCell;
}
public byte[] GenerateExcel<T>(IGridColumnCollection<T> columns, IEnumerable<T> items)
{
ExcelData excelData = new ExcelData();
excelData.SheetName = Strings.Items;
var header = new List<ExcelCell>();
foreach (IGridColumn column in columns)
{
if (!(column.ExcelHidden ?? column.Hidden))
{
header.Add(new ExcelCell(column.Title));
}
}
excelData.Cells.Add(header);
foreach (var item in items)
{
List<ExcelCell> row = new List<ExcelCell>();
foreach (IGridColumn column in columns)
{
if (!(column.ExcelHidden ?? column.Hidden))
{
var cell = column.GetExcelCell(item) as GridCell;
cell.Encode = false;
var type = ((IGridColumn<T>)column).GetTypeAndValue(item).Type;
row.Add(new ExcelCell(cell.ToString(), type));
}
}
excelData.Cells.Add(row);
}
return GenerateExcel(excelData);
}
public byte[] GenerateExcel(ExcelData data)
{
var stream = new MemoryStream();
using (var document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
{
var workbookpart = document.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new Worksheet(sheetData);
var sheets = document.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
var sheet = new Sheet()
{
Id = document.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = data.SheetName ?? "Sheet 1"
};
sheets.AppendChild(sheet);
if (data.ExtendedSheet)
AppendDataToExtendedSheet(worksheetPart.Worksheet, sheetData, data);
else
AppendDataToSheet(sheetData, data);
workbookpart.Workbook.Save();
}
return stream.ToArray();
}
private void AppendDataToSheet(SheetData sheetData, ExcelData data)
{
UInt32 rowIdex = 0;
Row row;
var cellIdex = 0;
// Add sheet data
foreach (var rowData in data.Cells)
{
cellIdex = 0;
row = new Row { RowIndex = ++rowIdex };
sheetData.AppendChild(row);
foreach (var cellData in rowData)
{
var cell = CreateCell(ColumnLetter(cellIdex++), rowIdex,
cellData.Content ?? string.Empty, cellData.Type);
row.AppendChild(cell);
}
}
}
private void AppendDataToExtendedSheet(Worksheet worksheet, SheetData sheetData,
ExcelData data)
{
UInt32 rowIdex = 0;
Row row;
MergeCells mergeCells = new MergeCells();
// Add sheet data
foreach (var rowData in data.Cells)
{
row = new Row { RowIndex = ++rowIdex };
sheetData.AppendChild(row);
foreach (var excelCell in rowData)
{
var cell = CreateCell(ColumnLetter(excelCell.ColumnIndex),
(uint)(excelCell.RowIndex + 1), excelCell.Content ?? string.Empty, excelCell.Type);
row.AppendChild(cell);
if (excelCell.ColSpan > 1 || excelCell.RowSpan > 1)
{
var mergeCell = CreateMergeCell(excelCell);
mergeCells.Append(mergeCell);
}
}
}
worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetData>().First());
}
}
}