-
Notifications
You must be signed in to change notification settings - Fork 61
/
EPPlusBuilder.cs
156 lines (135 loc) · 5.86 KB
/
EPPlusBuilder.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OfficeOpenXml;
namespace Blazor.FileReader.Demo.Common
{
public class EPPlusBuilder
{
static EPPlusBuilder()
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
}
public static TablesModel Parse(ExcelPackage xlPackage, bool treatFirstRowAsHeader)
{
var tableModel = new TablesModel();
var workbook = xlPackage.Workbook;
if (workbook != null)
{
foreach (var worksheet in workbook.Worksheets)
{
var table = tableModel.Add();
table.Name = worksheet.Name;
if (worksheet.Dimension == null)
{
continue;
}
var rowCount = 0;
var firstIteration = rowCount;
var maxColumnNumber = worksheet.Dimension.End.Column;
var rows = worksheet.Cells.ToLookup(c => c.Start.Row);
var tableRows = Enumerable.Range(1, rows.Max(x => x.Key));
var worksheet1 = worksheet;
foreach (var rowNumber in tableRows)
{
var isFirstRow = rowCount == firstIteration;
rowCount++;
var rowModel = new TablesModel.RowModel();
if (isFirstRow && treatFirstRowAsHeader)
{
table.Header = rowModel;
}
else
{
table.Body.Add(rowModel);
}
var row = rows[rowNumber];
if (!row.Any())
{
for (var i = 0; i < maxColumnNumber; i++)
{
rowModel.AddCell();
}
continue;
}
var cells = row.OrderBy(cell => cell.Start.Column).ToList();
rowModel.Height = worksheet1.Row(rowCount).Height;
for (var i = 1; i <= maxColumnNumber; i++)
{
var cell = rowModel.AddCell();
var currentCell = cells.SingleOrDefault(c => c.Start.Column == i);
if (currentCell == null)
{
continue;
}
var colSpan = 1;
var rowSpan = 1;
var cellAddress = new ExcelAddress(currentCell.Address);
var mCellsResult = worksheet1.MergedCells
.Select(mCell => new { c = mCell, addr = new ExcelAddress(mCell) })
.Where(mCell =>
cellAddress.Start.Row >= mCell.addr.Start.Row &&
cellAddress.End.Row <= mCell.addr.End.Row &&
cellAddress.Start.Column >= mCell.addr.Start.Column &&
cellAddress.End.Column <= mCell.addr.End.Column)
.Select(mCell => mCell.addr).ToList();
if (mCellsResult.Any())
{
var mCells = mCellsResult.First();
//if the cell and the merged cell do not share a common start address then skip this cell as it's already been covered by a previous item
if (mCells.Start.Address != cellAddress.Start.Address)
continue;
if (mCells.Start.Column != mCells.End.Column)
{
colSpan += mCells.End.Column - mCells.Start.Column;
}
if (mCells.Start.Row != mCells.End.Row)
{
rowSpan += mCells.End.Row - mCells.Start.Row;
}
}
cell.Colspan = colSpan;
cell.Rowspan = rowSpan;
cell.Value = currentCell.Value.ToString();
}
}
}
}
return tableModel;
}
public class TablesModel
{
public IList<TableModel> Tables { get; set; } = new List<TableModel>();
public TableModel Add()
{
var tableModel = new TableModel();
Tables.Add(tableModel);
return tableModel;
}
public class TableModel
{
public RowModel Header { get; set; }
public IList<RowModel> Body { get; set; } = new List<RowModel>();
public string Name { get; set; }
}
public class RowModel
{
public IList<CellModel> Cells { get; set; } = new List<CellModel>();
public double Height { get; set; }
public CellModel AddCell()
{
var cell = new CellModel();
Cells.Add(cell);
return cell;
}
}
public class CellModel
{
public string Value { get; set; } = string.Empty;
public int Colspan { get; set; } = 1;
public int Rowspan { get; set; } = 1;
}
}
}
}