/
OpenXmlExcel.cs
671 lines (590 loc) · 23.5 KB
/
OpenXmlExcel.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using DocumentFormat.OpenXml;
using System.Collections;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Text.RegularExpressions;
using System.Globalization;
namespace OpenXmlExcel
{
public class ExcelDocument : IDisposable
{
public SpreadsheetDocument document;
public Stylesheet stylesheet;
public ExcelSheetCollection Sheets;
public static uint DateTypeIndex;
private bool disposed = false;
public ExcelDocument(string filename)
{
if (File.Exists(filename))
{
document = SpreadsheetDocument.Open(filename, true);
if (document.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().Count() == 0)
{
WorkbookStylesPart stylespart = document.WorkbookPart.AddNewPart<WorkbookStylesPart>();
stylespart.Stylesheet = GetStylesheet();
DateTypeIndex = 1;
stylesheet = stylespart.Stylesheet;
}
else
{
WorkbookStylesPart stylespart = document.WorkbookPart.GetPartsOfType<WorkbookStylesPart>().First();
IEnumerable<CellFormat> cellformats = stylespart.Stylesheet.CellFormats.Elements<CellFormat>();
if (cellformats.Where(f => f.NumberFormatId == 14).Count() > 0)
{
uint dateformatindex = 0;
foreach (CellFormat format in cellformats)
if (format.NumberFormatId == 14) break; else dateformatindex++;
DateTypeIndex = dateformatindex;
}
else
{
stylespart.Stylesheet.CellFormats.Append(new CellFormat()
{
BorderId = 0,
FillId = 0,
FontId = 0,
NumberFormatId = 14,
FormatId = 0,
ApplyNumberFormat = true
});
stylespart.Stylesheet.CellFormats.Count = (uint)stylespart.Stylesheet.CellFormats.ChildElements.Count;
DateTypeIndex = stylespart.Stylesheet.CellFormats.Count - 1;
}
stylesheet = stylespart.Stylesheet;
}
}
else
{
document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook);
WorkbookPart workbookpart = document.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
WorkbookStylesPart stylespart = workbookpart.AddNewPart<WorkbookStylesPart>();
stylespart.Stylesheet = GetStylesheet();
DateTypeIndex = 1;
stylesheet = stylespart.Stylesheet;
}
Sheets = new ExcelSheetCollection(this);
}
public int InsertSharedStringItem(string text)
{
SharedStringTablePart shareStringPart;
if (document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
shareStringPart = document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
else
shareStringPart = document.WorkbookPart.AddNewPart<SharedStringTablePart>();
if (shareStringPart.SharedStringTable == null)
shareStringPart.SharedStringTable = new SharedStringTable();
int i = 0;
foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>())
{
if (item.InnerText == text)
return i;
i++;
}
shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));
return i;
}
private static Stylesheet GetStylesheet()
{
Stylesheet stylesheet = new Stylesheet();
Fonts fonts = new Fonts();
fonts.Append(new Font()
{
FontName = new FontName() { Val = "Calibri" },
FontSize = new FontSize() { Val = 11 },
FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 },
});
fonts.Count = (uint)fonts.ChildElements.Count;
Fills fills = new Fills();
fills.Append(new Fill() { PatternFill = new PatternFill() { PatternType = PatternValues.None } });
fills.Append(new Fill() { PatternFill = new PatternFill() { PatternType = PatternValues.Gray125 } });
fills.Count = (uint)fills.ChildElements.Count;
Borders borders = new Borders();
borders.Append(new Border()
{
LeftBorder = new LeftBorder(),
RightBorder = new RightBorder(),
TopBorder = new TopBorder(),
BottomBorder = new BottomBorder(),
DiagonalBorder = new DiagonalBorder()
});
borders.Count = (uint)borders.ChildElements.Count;
CellStyleFormats cellstyleformats = new CellStyleFormats();
cellstyleformats.Append(new CellFormat()
{
NumberFormatId = 0,
FontId = 0,
FillId = 0,
BorderId = 0
});
cellstyleformats.Count = (uint)cellstyleformats.ChildElements.Count;
CellFormats cellformats = new CellFormats();
cellformats.Append(new CellFormat()
{
BorderId = 0,
FillId = 0,
FontId = 0,
NumberFormatId = 0,
FormatId = 0
});
cellformats.Append(new CellFormat()
{
BorderId = 0,
FillId = 0,
FontId = 0,
NumberFormatId = 14,
FormatId = 0,
ApplyNumberFormat = true
});
cellformats.Count = (uint)cellformats.ChildElements.Count;
CellStyles cellstyles = new CellStyles();
cellstyles.Append(new CellStyle()
{
Name = "Normal",
FormatId = 0,
BuiltinId = 0
});
cellstyles.Count = (uint)cellstyles.ChildElements.Count;
stylesheet.Append(fonts);
stylesheet.Append(fills);
stylesheet.Append(borders);
stylesheet.Append(cellstyleformats);
stylesheet.Append(cellformats);
stylesheet.Append(cellstyles);
return stylesheet;
}
public void Close()
{
this.Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
document.Close();
}
disposed = true;
}
}
public class ExcelSheetCollection
{
private SortedDictionary<uint, ExcelSheet> List;
public ExcelDocument document;
public int Length { get { return this.List.Count; } }
public ExcelSheet this[string name]
{
get { return this.List.Values.Where(p => String.Compare(p.Name, name, true) == 0).FirstOrDefault(); }
}
public ExcelSheet this[uint id]
{
get { if (this.List.ContainsKey(id)) return this.List[id]; else return null; }
}
public ExcelSheetCollection(ExcelDocument doc)
{
this.document = doc;
this.List = new SortedDictionary<uint, ExcelSheet>();
foreach (Sheet sheet in doc.document.WorkbookPart.Workbook.Descendants<Sheet>())
this.List.Add(sheet.SheetId, new ExcelSheet(sheet, doc));
}
public ExcelSheet AddNew(string name = "")
{
ExcelSheet sheet = new ExcelSheet(document, name);
this.List.Add(sheet.Id, sheet);
return sheet;
}
}
public class ExcelSheet
{
public string Name { get { return sheet.Name; } set { sheet.Name.Value = value; } }
public uint Id { get { return sheet.SheetId; } }
public ExcelDocument document;
public Worksheet worksheet;
public Sheet sheet;
private ExcelRowCollection _rows;
public ExcelRowCollection Rows { get { return _rows; } }
public ExcelCell this[string address]
{
get
{
return _rows[ExcelCell.GetRowIndex(address)].Cells[ExcelCell.GetColumnName(address)];
}
}
public ExcelCell this[uint row, uint col]
{
get
{
return _rows[row].Cells[col];
}
}
public ExcelSheet(ExcelDocument doc, string name)
{
this.document = doc;
WorksheetPart newWorksheetPart = doc.document.WorkbookPart.AddNewPart<WorksheetPart>();
worksheet = new Worksheet(new SheetData());
newWorksheetPart.Worksheet = worksheet;
Sheets sheets = doc.document.WorkbookPart.Workbook.GetFirstChild<Sheets>();
if (sheets == null)
sheets = doc.document.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
string relationshipId = doc.document.WorkbookPart.GetIdOfPart(newWorksheetPart);
// Get a unique ID for the new worksheet.
uint sheetId = 1;
if (sheets.Elements<Sheet>().Count() > 0)
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
// Give the new worksheet a name.
string sheetName = "Sheet";
if (String.IsNullOrEmpty(name))
sheetName = getSheetName(sheetName, sheets.Elements<Sheet>(), 1);
else
sheetName = getSheetName(name, sheets.Elements<Sheet>());
// Append the new worksheet and associate it with the workbook.
sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
_rows = new ExcelRowCollection(this);
}
public ExcelSheet(Sheet sheet, ExcelDocument doc)
{
this.sheet = sheet;
this.document = doc;
this.worksheet = ((WorksheetPart)(doc.document.WorkbookPart.GetPartById(sheet.Id))).Worksheet;
_rows = new ExcelRowCollection(this);
}
private string getSheetName(string name, IEnumerable<Sheet> sheets, int number = 0)
{
string sheetName = name + (number > 0 ? number.ToString() : String.Empty);
if(sheets.Count(s => String.Compare(s.Name, sheetName, true) == 0) > 0)
sheetName = getSheetName(name, sheets, number + 1);
return sheetName;
}
public void RemoveCell(uint rowIndex, uint colIndex)
{
if (_rows.Contains(rowIndex))
_rows[rowIndex].Cells.Remove(colIndex);
}
public void RemoveCell(string address)
{
RemoveCell(ExcelCell.GetRowIndex(address), ExcelCell.GetColumnIndex(ExcelCell.GetColumnName(address)));
}
}
public class ExcelRowCollection
{
private SortedDictionary<uint, ExcelRow> List;
public ExcelSheet sheet;
public ExcelRow this[UInt32 rowIndex]
{
get
{
if (rowIndex < 1) throw new ArgumentException("Index row can not be less than 1!");
return this.GetRow(rowIndex);
}
}
public int Length { get { return this.List.Count; } }
public bool Contains(uint rowIndex)
{
return this.List.ContainsKey(rowIndex);
}
public ExcelRowCollection(ExcelSheet sheet)
{
this.sheet = sheet;
this.List = new SortedDictionary<uint, ExcelRow>();
foreach(Row row in sheet.worksheet.GetFirstChild<SheetData>().Elements<Row>())
this.List.Add(row.RowIndex, new ExcelRow(row, sheet));
}
public ExcelRow GetRow(UInt32 rowIndex)
{
if (rowIndex < 1) throw new ArgumentException("Index row can not be less than 1!");
if (this.List.ContainsKey(rowIndex))
return this.List[rowIndex];
Row newRow = new Row() { RowIndex = rowIndex };
ExcelRow nextRow = this.List.FirstOrDefault(p => p.Key > rowIndex).Value;
if(nextRow != null)
sheet.worksheet.GetFirstChild<SheetData>().InsertBefore(newRow, nextRow.row);
else
sheet.worksheet.GetFirstChild<SheetData>().Append(newRow);
ExcelRow retVal = new ExcelRow(newRow, sheet);
this.List.Add(rowIndex, retVal);
return retVal;
}
}
public class ExcelRow
{
public UInt32 index;
public Row row;
public ExcelCellCollection Cells;
public ExcelSheet sheet;
public ExcelRow(Row row, ExcelSheet sheet)
{
this.row = row;
this.index = row.RowIndex.Value;
this.sheet = sheet;
this.Cells = new ExcelCellCollection(this);
}
}
public class ExcelCellCollection
{
private SortedDictionary<uint, ExcelCell> List;
public ExcelRow row;
public int Length { get { return this.List.Count; } }
public bool Contains(uint colIndex)
{
return this.List.ContainsKey(colIndex);
}
public bool Contains(string colName)
{
return this.List.ContainsKey(ExcelCell.GetColumnIndex(colName));
}
public ExcelCell this[string name]
{
get
{
uint index = ExcelCell.GetColumnIndex(name);
if (index < 1) throw new ArgumentException("Index column cannot be less than 1!");
if(this.List.ContainsKey(index))
return this.List[index];
return GetCell(index);
}
}
public ExcelCell this[uint index]
{
get
{
if (index < 1) throw new ArgumentException("Index column cannot be less than 1!");
if (this.List.ContainsKey(index))
return this.List[index];
return GetCell(index);
}
}
public ExcelCellCollection(ExcelRow row)
{
this.row = row;
this.List = new SortedDictionary<uint, ExcelCell>();
foreach (Cell cell in row.row.Elements<Cell>())
{
ExcelCell newCell = new ExcelCell(cell, row);
this.List.Add(newCell.Index, newCell);
}
}
public void Remove(uint index)
{
if (this.List.ContainsKey(index))
{
this.List[index].cell.Remove();
this.List.Remove(index);
}
}
public void Remove(string colName)
{
Remove(ExcelCell.GetColumnIndex(colName));
}
public ExcelCell GetCell(uint index)
{
if (index < 1) throw new ArgumentException("Index column cannot be less than 1!");
if (this.List.ContainsKey(index))
return this.List[index];
string address = ExcelCell.ConvertToLetter(index) + row.index.ToString();
Cell newCell = new Cell() { CellReference = address };
ExcelCell nextCell = this.List.FirstOrDefault(p => p.Key > index).Value;
if (nextCell != null)
row.row.InsertBefore(newCell, nextCell.cell);
else
row.row.Append(newCell);
ExcelCell retVal = new ExcelCell(newCell, row);
this.List.Add(retVal.Index, retVal);
return retVal;
}
}
public class ExcelCell
{
public string Address { get { return cell.CellReference.Value; } }
public uint Index { get { return GetColumnIndex(this.Name); } }
public string Name { get { return GetColumnName(cell.CellReference.Value); } }
public ExcelRow row;
public Cell cell;
public object Value
{
get { return getCellValue(); }
set { setCellValue(value); }
}
public ExcelCell(Cell cell, ExcelRow row)
{
this.row = row;
this.cell = cell;
}
private object getCellValue()
{
string cellText = cell.InnerText;
object value = cellText;
if (cell.DataType != null)
{
switch (cell.DataType.Value)
{
case CellValues.SharedString:
var stringTable = row.sheet.document.document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (stringTable != null)
value = stringTable.SharedStringTable.ElementAt(int.Parse(cellText)).InnerText;
break;
case CellValues.Boolean:
if(cellText == "0") value = false; else value = true;
break;
case CellValues.Number:
value = Convert.ToDecimal(cellText, CultureInfo.InvariantCulture);
break;
case CellValues.Date:
value = DateTime.FromOADate(Convert.ToDouble(cellText, CultureInfo.InvariantCulture));
break;
default:
value = cellText;
break;
}
}
return value;
}
private void setCellValue(object value)
{
string text = Convert.ToString(value, CultureInfo.InvariantCulture);
if (String.IsNullOrEmpty(text)) return;
if(value is string)
{
int index = row.sheet.document.InsertSharedStringItem(text);
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
}
else if(ExcelCell.IsNumericType(value.GetType()))
{
cell.CellValue = new CellValue(text);
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
}
else
{
cell.CellValue = new CellValue(text);
cell.DataType = new EnumValue<CellValues>(CellValues.String);
}
}
public string GetString()
{
string value = cell.InnerText;
if (cell.DataType != null)
{
if (cell.DataType.Value == CellValues.SharedString)
{
var stringTable = row.sheet.document.document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (stringTable != null)
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
}
}
return value;
}
public bool GetBoolean()
{
return (cell.InnerText != "0");
}
public decimal GetNumber()
{
return Convert.ToDecimal(cell.InnerText, CultureInfo.InvariantCulture);
}
public DateTime GetDate()
{
return DateTime.FromOADate(Convert.ToDouble(cell.InnerText, CultureInfo.InvariantCulture));
}
public void SetValue(object value, ExcelCellType type)
{
string text = Convert.ToString(value, CultureInfo.InvariantCulture);
if (String.IsNullOrEmpty(text)) return;
switch (type)
{
case ExcelCellType.String:
cell.CellValue = new CellValue(text);
cell.DataType = new EnumValue<CellValues>(CellValues.String);
break;
case ExcelCellType.SharedString:
int index = row.sheet.document.InsertSharedStringItem(text);
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
break;
case ExcelCellType.Number:
cell.CellValue = new CellValue(text.Replace(',', '.'));
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
break;
case ExcelCellType.Date:
text = Convert.ToDateTime(value).ToOADate().ToString(CultureInfo.InvariantCulture);
cell.CellValue = new CellValue(text);
cell.DataType = CellValues.Number;
cell.StyleIndex = ExcelDocument.DateTypeIndex;
break;
case ExcelCellType.Boolean:
text = Convert.ToInt32(value, CultureInfo.InvariantCulture).ToString();
cell.CellValue = new CellValue(text);
cell.DataType = new EnumValue<CellValues>(CellValues.Boolean);
break;
}
}
public enum ExcelCellType { String, SharedString, Number, Date, Boolean };
public static string ConvertToLetter(uint iCol)
{
uint iAlpha, iRemainder;
iAlpha = iCol / 27;
iRemainder = iCol - (iAlpha * 26);
string result = String.Empty;
if(iAlpha > 0)
result = ((char)(iAlpha + 64)).ToString();
if(iRemainder > 0)
result = result + ((char)(iRemainder + 64));
return result;
}
public static string GetColumnName(string cellName)
{
Match match = Regex.Match(cellName, @"[A-Za-z]+");
return match.Value;
}
public static uint GetRowIndex(string cellName)
{
Match match = Regex.Match(cellName, @"\d+");
return uint.Parse(match.Value);
}
public static uint GetColumnIndex(string leters)
{
uint result = 0;
foreach (char leter in leters)
result = (((uint)leter) - 64) + (result * 26);
return result;
}
private static bool IsNumericType(Type type)
{
if (type == null)
return false;
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
case TypeCode.Object:
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return IsNumericType(Nullable.GetUnderlyingType(type));
}
return false;
}
return false;
}
}
}