1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using OfficeOpenXml.FormulaParsing;
|
---|
6 | using OfficeOpenXml.FormulaParsing.ExcelUtilities;
|
---|
7 | using OfficeOpenXml.FormulaParsing.Excel.Functions;
|
---|
8 | using OfficeOpenXml.FormulaParsing.LexicalAnalysis;
|
---|
9 | using OfficeOpenXml.Utils;
|
---|
10 | using OfficeOpenXml.Style.XmlAccess;
|
---|
11 |
|
---|
12 | namespace OfficeOpenXml.FormulaParsing
|
---|
13 | {
|
---|
14 | public class EpplusExcelDataProvider : ExcelDataProvider
|
---|
15 | {
|
---|
16 | public class RangeInfo : IRangeInfo
|
---|
17 | {
|
---|
18 | internal ExcelWorksheet _ws;
|
---|
19 | CellsStoreEnumerator<object> _values = null;
|
---|
20 | int _fromRow, _toRow, _fromCol, _toCol;
|
---|
21 | int _cellCount = 0;
|
---|
22 | ExcelAddressBase _address;
|
---|
23 | ICellInfo _cell;
|
---|
24 |
|
---|
25 | public RangeInfo(ExcelWorksheet ws, int fromRow, int fromCol, int toRow, int toCol)
|
---|
26 | {
|
---|
27 | _ws = ws;
|
---|
28 | _fromRow = fromRow;
|
---|
29 | _fromCol = fromCol;
|
---|
30 | _toRow = toRow;
|
---|
31 | _toCol = toCol;
|
---|
32 | _address = new ExcelAddressBase(_fromRow, _fromCol, _toRow, _toCol);
|
---|
33 | _address._ws = ws.Name;
|
---|
34 | _values = new CellsStoreEnumerator<object>(ws._values, _fromRow, _fromCol, _toRow, _toCol);
|
---|
35 | _cell = new CellInfo(_ws, _values);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public int GetNCells()
|
---|
39 | {
|
---|
40 | return ((_toRow - _fromRow) + 1) * ((_toCol - _fromCol) + 1);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public bool IsEmpty
|
---|
44 | {
|
---|
45 | get
|
---|
46 | {
|
---|
47 | if (_cellCount > 0)
|
---|
48 | {
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | else if (_values.Next())
|
---|
52 | {
|
---|
53 | _values.Reset();
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 | else
|
---|
57 | {
|
---|
58 | return true;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | public bool IsMulti
|
---|
63 | {
|
---|
64 | get
|
---|
65 | {
|
---|
66 | if (_cellCount == 0)
|
---|
67 | {
|
---|
68 | if (_values.Next() && _values.Next())
|
---|
69 | {
|
---|
70 | _values.Reset();
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | _values.Reset();
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | else if (_cellCount > 1)
|
---|
80 | {
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public ICellInfo Current
|
---|
88 | {
|
---|
89 | get { return _cell; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public ExcelWorksheet Worksheet
|
---|
93 | {
|
---|
94 | get { return _ws; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void Dispose()
|
---|
98 | {
|
---|
99 | //_values = null;
|
---|
100 | //_ws = null;
|
---|
101 | //_cell = null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | object System.Collections.IEnumerator.Current
|
---|
105 | {
|
---|
106 | get
|
---|
107 | {
|
---|
108 | return this;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public bool MoveNext()
|
---|
113 | {
|
---|
114 | _cellCount++;
|
---|
115 | return _values.MoveNext();
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void Reset()
|
---|
119 | {
|
---|
120 | _values.Init();
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | public bool NextCell()
|
---|
125 | {
|
---|
126 | _cellCount++;
|
---|
127 | return _values.MoveNext();
|
---|
128 | }
|
---|
129 |
|
---|
130 | public IEnumerator<ICellInfo> GetEnumerator()
|
---|
131 | {
|
---|
132 | Reset();
|
---|
133 | return this;
|
---|
134 | }
|
---|
135 |
|
---|
136 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
137 | {
|
---|
138 | return this;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public ExcelAddressBase Address
|
---|
142 | {
|
---|
143 | get { return _address; }
|
---|
144 | }
|
---|
145 |
|
---|
146 | public object GetValue(int row, int col)
|
---|
147 | {
|
---|
148 | return _ws.GetValue(row, col);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public object GetOffset(int rowOffset, int colOffset)
|
---|
152 | {
|
---|
153 | if (_values.Row < _fromRow || _values.Column < _fromCol)
|
---|
154 | {
|
---|
155 | return _ws.GetValue(_fromRow + rowOffset, _fromCol + colOffset);
|
---|
156 | }
|
---|
157 | else
|
---|
158 | {
|
---|
159 | return _ws.GetValue(_values.Row + rowOffset, _values.Column + colOffset);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | public class CellInfo : ICellInfo
|
---|
165 | {
|
---|
166 | ExcelWorksheet _ws;
|
---|
167 | CellsStoreEnumerator<object> _values;
|
---|
168 | internal CellInfo(ExcelWorksheet ws, CellsStoreEnumerator<object> values)
|
---|
169 | {
|
---|
170 | _ws = ws;
|
---|
171 | _values = values;
|
---|
172 | }
|
---|
173 | public string Address
|
---|
174 | {
|
---|
175 | get { return _values.CellAddress; }
|
---|
176 | }
|
---|
177 |
|
---|
178 | public int Row
|
---|
179 | {
|
---|
180 | get { return _values.Row; }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public int Column
|
---|
184 | {
|
---|
185 | get { return _values.Column; }
|
---|
186 | }
|
---|
187 |
|
---|
188 | public string Formula
|
---|
189 | {
|
---|
190 | get
|
---|
191 | {
|
---|
192 | return _ws.GetFormula(_values.Row, _values.Column);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | public object Value
|
---|
197 | {
|
---|
198 | get { return _values.Value; }
|
---|
199 | }
|
---|
200 |
|
---|
201 | public double ValueDouble
|
---|
202 | {
|
---|
203 | get { return ConvertUtil.GetValueDouble(_values.Value, true); }
|
---|
204 | }
|
---|
205 | public double ValueDoubleLogical
|
---|
206 | {
|
---|
207 | get { return ConvertUtil.GetValueDouble(_values.Value, false); }
|
---|
208 | }
|
---|
209 | public bool IsHiddenRow
|
---|
210 | {
|
---|
211 | get
|
---|
212 | {
|
---|
213 | var row=_ws._values.GetValue(_values.Row, 0) as RowInternal;
|
---|
214 | if(row != null)
|
---|
215 | {
|
---|
216 | return row.Hidden || row.Height==0;
|
---|
217 | }
|
---|
218 | else
|
---|
219 | {
|
---|
220 | return false;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | public bool IsExcelError
|
---|
226 | {
|
---|
227 | get { return ExcelErrorValue.Values.IsErrorValue(_values.Value); }
|
---|
228 | }
|
---|
229 |
|
---|
230 | public IList<Token> Tokens
|
---|
231 | {
|
---|
232 | get
|
---|
233 | {
|
---|
234 | return _ws._formulaTokens.GetValue(_values.Row, _values.Column);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | }
|
---|
239 | public class NameInfo : ExcelDataProvider.INameInfo
|
---|
240 | {
|
---|
241 | public ulong Id { get; set; }
|
---|
242 | public string Worksheet { get; set; }
|
---|
243 | public string Name { get; set; }
|
---|
244 | public string Formula { get; set; }
|
---|
245 | public IList<Token> Tokens { get; internal set; }
|
---|
246 | public object Value { get; set; }
|
---|
247 | }
|
---|
248 |
|
---|
249 | private readonly ExcelPackage _package;
|
---|
250 | private ExcelWorksheet _currentWorksheet;
|
---|
251 | private RangeAddressFactory _rangeAddressFactory;
|
---|
252 | private Dictionary<ulong, INameInfo> _names=new Dictionary<ulong,INameInfo>();
|
---|
253 |
|
---|
254 | public EpplusExcelDataProvider(ExcelPackage package)
|
---|
255 | {
|
---|
256 | _package = package;
|
---|
257 |
|
---|
258 | _rangeAddressFactory = new RangeAddressFactory(this);
|
---|
259 | }
|
---|
260 |
|
---|
261 | public override ExcelNamedRangeCollection GetWorksheetNames(string worksheet)
|
---|
262 | {
|
---|
263 | var ws=_package.Workbook.Worksheets[worksheet];
|
---|
264 | if (ws != null)
|
---|
265 | {
|
---|
266 | return ws.Names;
|
---|
267 | }
|
---|
268 | else
|
---|
269 | {
|
---|
270 | return null;
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | public override ExcelNamedRangeCollection GetWorkbookNameValues()
|
---|
275 | {
|
---|
276 | return _package.Workbook.Names;
|
---|
277 | }
|
---|
278 |
|
---|
279 | public override IRangeInfo GetRange(string worksheet, int fromRow, int fromCol, int toRow, int toCol)
|
---|
280 | {
|
---|
281 | SetCurrentWorksheet(worksheet);
|
---|
282 | var wsName = string.IsNullOrEmpty(worksheet) ? _currentWorksheet.Name : worksheet;
|
---|
283 | var ws = _package.Workbook.Worksheets[wsName];
|
---|
284 | return new RangeInfo(ws, fromRow, fromCol, toRow, toCol);
|
---|
285 | }
|
---|
286 | public override IRangeInfo GetRange(string worksheet, int row, int column, string address)
|
---|
287 | {
|
---|
288 | var addr = new ExcelAddress(worksheet, address);
|
---|
289 | if (addr.Table != null)
|
---|
290 | {
|
---|
291 | addr.SetRCFromTable(_package, new ExcelAddressBase(row, column, row, column));
|
---|
292 | }
|
---|
293 | //SetCurrentWorksheet(addr.WorkSheet);
|
---|
294 | var wsName = string.IsNullOrEmpty(addr.WorkSheet) ? _currentWorksheet.Name : addr.WorkSheet;
|
---|
295 | var ws = _package.Workbook.Worksheets[wsName];
|
---|
296 | //return new CellsStoreEnumerator<object>(ws._values, addr._fromRow, addr._fromCol, addr._toRow, addr._toCol);
|
---|
297 | return new RangeInfo(ws, addr._fromRow, addr._fromCol, addr._toRow, addr._toCol);
|
---|
298 | }
|
---|
299 | public override INameInfo GetName(string worksheet, string name)
|
---|
300 | {
|
---|
301 | ExcelNamedRange nameItem;
|
---|
302 | ulong id;
|
---|
303 | ExcelWorksheet ws;
|
---|
304 | if (string.IsNullOrEmpty(worksheet))
|
---|
305 | {
|
---|
306 | if(_package._workbook.Names.ContainsKey(name))
|
---|
307 | {
|
---|
308 | nameItem = _package._workbook.Names[name];
|
---|
309 | }
|
---|
310 | else
|
---|
311 | {
|
---|
312 | return null;
|
---|
313 | }
|
---|
314 | ws = null;
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | ws = _package._workbook.Worksheets[worksheet];
|
---|
319 | if (ws !=null && ws.Names.ContainsKey(name))
|
---|
320 | {
|
---|
321 | nameItem = ws.Names[name];
|
---|
322 | }
|
---|
323 | else if (_package._workbook.Names.ContainsKey(name))
|
---|
324 | {
|
---|
325 | nameItem = _package._workbook.Names[name];
|
---|
326 | }
|
---|
327 | else
|
---|
328 | {
|
---|
329 | return null;
|
---|
330 | }
|
---|
331 | }
|
---|
332 | id = ExcelAddressBase.GetCellID(nameItem.LocalSheetId, nameItem.Index, 0);
|
---|
333 |
|
---|
334 | if (_names.ContainsKey(id))
|
---|
335 | {
|
---|
336 | return _names[id];
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | var ni = new NameInfo()
|
---|
341 | {
|
---|
342 | Id = id,
|
---|
343 | Name = name,
|
---|
344 | Worksheet = nameItem.Worksheet==null ? nameItem._ws : nameItem.Worksheet.Name,
|
---|
345 | Formula = nameItem.Formula
|
---|
346 | };
|
---|
347 | if (nameItem._fromRow > 0)
|
---|
348 | {
|
---|
349 | ni.Value = new RangeInfo(nameItem.Worksheet ?? ws, nameItem._fromRow, nameItem._fromCol, nameItem._toRow, nameItem._toCol);
|
---|
350 | }
|
---|
351 | else
|
---|
352 | {
|
---|
353 | ni.Value = nameItem.Value;
|
---|
354 | }
|
---|
355 | _names.Add(id, ni);
|
---|
356 | return ni;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | public override IEnumerable<object> GetRangeValues(string address)
|
---|
360 | {
|
---|
361 | SetCurrentWorksheet(ExcelAddressInfo.Parse(address));
|
---|
362 | var addr = new ExcelAddress(address);
|
---|
363 | var wsName = string.IsNullOrEmpty(addr.WorkSheet) ? _currentWorksheet.Name : addr.WorkSheet;
|
---|
364 | var ws = _package.Workbook.Worksheets[wsName];
|
---|
365 | return (new CellsStoreEnumerator<object>(ws._values, addr._fromRow, addr._fromCol, addr._toRow, addr._toCol));
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | public object GetValue(int row, int column)
|
---|
370 | {
|
---|
371 | return _currentWorksheet._values.GetValue(row, column);
|
---|
372 | }
|
---|
373 |
|
---|
374 | public bool IsMerged(int row, int column)
|
---|
375 | {
|
---|
376 | //return _currentWorksheet._flags.GetFlagValue(row, column, CellFlags.Merged);
|
---|
377 | return _currentWorksheet.MergedCells[row, column] != null;
|
---|
378 | }
|
---|
379 |
|
---|
380 | public bool IsHidden(int row, int column)
|
---|
381 | {
|
---|
382 | return _currentWorksheet.Column(column).Hidden || _currentWorksheet.Column(column).Width == 0 ||
|
---|
383 | _currentWorksheet.Row(row).Hidden || _currentWorksheet.Row(column).Height == 0;
|
---|
384 | }
|
---|
385 |
|
---|
386 | public override object GetCellValue(string sheetName, int row, int col)
|
---|
387 | {
|
---|
388 | SetCurrentWorksheet(sheetName);
|
---|
389 | return _currentWorksheet._values.GetValue(row, col);
|
---|
390 | }
|
---|
391 |
|
---|
392 | public override ExcelCellAddress GetDimensionEnd(string worksheet)
|
---|
393 | {
|
---|
394 | ExcelCellAddress address = null;
|
---|
395 | try
|
---|
396 | {
|
---|
397 | address = _package.Workbook.Worksheets[worksheet].Dimension.End;
|
---|
398 | }
|
---|
399 | catch{}
|
---|
400 |
|
---|
401 | return address;
|
---|
402 | }
|
---|
403 |
|
---|
404 | private void SetCurrentWorksheet(ExcelAddressInfo addressInfo)
|
---|
405 | {
|
---|
406 | if (addressInfo.WorksheetIsSpecified)
|
---|
407 | {
|
---|
408 | _currentWorksheet = _package.Workbook.Worksheets[addressInfo.Worksheet];
|
---|
409 | }
|
---|
410 | else if (_currentWorksheet == null)
|
---|
411 | {
|
---|
412 | _currentWorksheet = _package.Workbook.Worksheets.First();
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | private void SetCurrentWorksheet(string worksheetName)
|
---|
417 | {
|
---|
418 | if (!string.IsNullOrEmpty(worksheetName))
|
---|
419 | {
|
---|
420 | _currentWorksheet = _package.Workbook.Worksheets[worksheetName];
|
---|
421 | }
|
---|
422 | else
|
---|
423 | {
|
---|
424 | _currentWorksheet = _package.Workbook.Worksheets.First();
|
---|
425 | }
|
---|
426 |
|
---|
427 | }
|
---|
428 |
|
---|
429 | //public override void SetCellValue(string address, object value)
|
---|
430 | //{
|
---|
431 | // var addressInfo = ExcelAddressInfo.Parse(address);
|
---|
432 | // var ra = _rangeAddressFactory.Create(address);
|
---|
433 | // SetCurrentWorksheet(addressInfo);
|
---|
434 | // //var valueInfo = (ICalcEngineValueInfo)_currentWorksheet;
|
---|
435 | // //valueInfo.SetFormulaValue(ra.FromRow + 1, ra.FromCol + 1, value);
|
---|
436 | // _currentWorksheet.Cells[ra.FromRow + 1, ra.FromCol + 1].Value = value;
|
---|
437 | //}
|
---|
438 |
|
---|
439 | public override void Dispose()
|
---|
440 | {
|
---|
441 | _package.Dispose();
|
---|
442 | }
|
---|
443 |
|
---|
444 | public override int ExcelMaxColumns
|
---|
445 | {
|
---|
446 | get { return ExcelPackage.MaxColumns; }
|
---|
447 | }
|
---|
448 |
|
---|
449 | public override int ExcelMaxRows
|
---|
450 | {
|
---|
451 | get { return ExcelPackage.MaxRows; }
|
---|
452 | }
|
---|
453 |
|
---|
454 | public override string GetRangeFormula(string worksheetName, int row, int column)
|
---|
455 | {
|
---|
456 | SetCurrentWorksheet(worksheetName);
|
---|
457 | return _currentWorksheet.GetFormula(row, column);
|
---|
458 | }
|
---|
459 |
|
---|
460 | public override object GetRangeValue(string worksheetName, int row, int column)
|
---|
461 | {
|
---|
462 | SetCurrentWorksheet(worksheetName);
|
---|
463 | return _currentWorksheet.GetValue(row, column);
|
---|
464 | }
|
---|
465 | public override string GetFormat(object value, string format)
|
---|
466 | {
|
---|
467 | var styles = _package.Workbook.Styles;
|
---|
468 | ExcelNumberFormatXml.ExcelFormatTranslator ft=null;
|
---|
469 | foreach(var f in styles.NumberFormats)
|
---|
470 | {
|
---|
471 | if(f.Format==format)
|
---|
472 | {
|
---|
473 | ft=f.FormatTranslator;
|
---|
474 | break;
|
---|
475 | }
|
---|
476 | }
|
---|
477 | if(ft==null)
|
---|
478 | {
|
---|
479 | ft=new ExcelNumberFormatXml.ExcelFormatTranslator(format, -1);
|
---|
480 | }
|
---|
481 | return ExcelRangeBase.FormatValue(value, ft,format, ft.NetFormat);
|
---|
482 | }
|
---|
483 | public override List<LexicalAnalysis.Token> GetRangeFormulaTokens(string worksheetName, int row, int column)
|
---|
484 | {
|
---|
485 | return _package.Workbook.Worksheets[worksheetName]._formulaTokens.GetValue(row, column);
|
---|
486 | }
|
---|
487 |
|
---|
488 | public override bool IsRowHidden(string worksheetName, int row)
|
---|
489 | {
|
---|
490 | var b = _package.Workbook.Worksheets[worksheetName].Row(row).Height == 0 ||
|
---|
491 | _package.Workbook.Worksheets[worksheetName].Row(row).Hidden;
|
---|
492 |
|
---|
493 | return b;
|
---|
494 | }
|
---|
495 |
|
---|
496 | public override void Reset()
|
---|
497 | {
|
---|
498 | _names = new Dictionary<ulong, INameInfo>(); //Reset name cache.
|
---|
499 | }
|
---|
500 | }
|
---|
501 | }
|
---|
502 | |
---|