This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ExcelDrawings.cs
630 lines (598 loc) · 25.3 KB
/
ExcelDrawings.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
/*******************************************************************************
* You may amend and distribute as you like, but don't remove this header!
*
* EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
* See https://github.com/JanKallman/EPPlus for details.
*
* Copyright (C) 2011 Jan Källman
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
*
* All code and executables are provided "as is" with no warranty either express or implied.
* The author accepts no liability for any damage or loss of business that this product may cause.
*
* Code change notes:
*
* Author Change Date
* ******************************************************************************
* Jan Källman Initial Release 2009-12-22
* Jan Källman License changed GPL-->LGPL 2011-12-16
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Linq;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Table.PivotTable;
using OfficeOpenXml.Utils;
namespace OfficeOpenXml.Drawing
{
/// <summary>
/// Collection for Drawing objects.
/// </summary>
public class ExcelDrawings : IEnumerable<ExcelDrawing>, IDisposable
{
private XmlDocument _drawingsXml=new XmlDocument();
private Dictionary<string, int> _drawingNames;
private List<ExcelDrawing> _drawings;
internal class ImageCompare
{
internal byte[] image { get; set; }
internal string relID { get; set; }
internal bool Comparer(byte[] compareImg)
{
if (compareImg.Length != image.Length)
{
return false;
}
for (int i = 0; i < image.Length; i++)
{
if (image[i] != compareImg[i])
{
return false;
}
}
return true; //Equal
}
}
//internal List<ImageCompare> _pics = new List<ImageCompare>();
internal Dictionary<string, string> _hashes = new Dictionary<string, string>();
internal ExcelPackage _package;
internal Packaging.ZipPackageRelationship _drawingRelation = null;
internal ExcelDrawings(ExcelPackage xlPackage, ExcelWorksheet sheet)
{
_drawingsXml = new XmlDocument();
_drawingsXml.PreserveWhitespace = false;
_drawings = new List<ExcelDrawing>();
_drawingNames = new Dictionary<string,int>(StringComparer.OrdinalIgnoreCase);
_package = xlPackage;
Worksheet = sheet;
XmlNode node = sheet.WorksheetXml.SelectSingleNode("//d:drawing", sheet.NameSpaceManager);
CreateNSM();
if (node != null)
{
_drawingRelation = sheet.Part.GetRelationship(node.Attributes["r:id"].Value);
_uriDrawing = UriHelper.ResolvePartUri(sheet.WorksheetUri, _drawingRelation.TargetUri);
_part = xlPackage.Package.GetPart(_uriDrawing);
XmlHelper.LoadXmlSafe(_drawingsXml, _part.GetStream());
AddDrawings();
}
}
internal ExcelWorksheet Worksheet { get; set; }
/// <summary>
/// A reference to the drawing xml document
/// </summary>
public XmlDocument DrawingXml
{
get
{
return _drawingsXml;
}
}
private void AddDrawings()
{
// Look inside all children for the drawings because they could be inside
// Markup Compatibility AlternativeContent/Choice or AlternativeContent/Fallback nodes.
// The code below currently pretends that loading all Choice alternative drawings doesn't cause a problem
// elsewhere. This seems to be ok for the time being as encountered drawing files so far only seem to have
// one Choice node (and no Fallback) underneath the AlternativeContent node. (Excel 2013 that is.)
// This change prevents CodePlex issue #15028 from occurring.
// (the drawing xml part (that ONLY contained AlternativeContent nodes) was incorrectly being garbage collected when the package was saved)
XmlNodeList list = _drawingsXml.SelectNodes("//*[self::xdr:twoCellAnchor or self::xdr:oneCellAnchor or self::xdr:absoluteAnchor]", NameSpaceManager);
foreach (XmlNode node in list)
{
ExcelDrawing dr;
switch(node.LocalName)
{
case "oneCellAnchor":
dr = ExcelDrawing.GetDrawing(this, node);
break;
case "twoCellAnchor":
dr = ExcelDrawing.GetDrawing(this, node);
break;
case "absoluteAnchor":
dr = ExcelDrawing.GetDrawing(this, node);
break;
default: //"absoluteCellAnchor":
dr = null;
break;
}
if (dr != null)
{
_drawings.Add(dr);
if (!_drawingNames.ContainsKey(dr.Name))
{
_drawingNames.Add(dr.Name, _drawings.Count - 1);
}
}
}
}
#region NamespaceManager
/// <summary>
/// Creates the NamespaceManager.
/// </summary>
private void CreateNSM()
{
NameTable nt = new NameTable();
_nsManager = new XmlNamespaceManager(nt);
_nsManager.AddNamespace("a", ExcelPackage.schemaDrawings);
_nsManager.AddNamespace("xdr", ExcelPackage.schemaSheetDrawings);
_nsManager.AddNamespace("c", ExcelPackage.schemaChart);
_nsManager.AddNamespace("r", ExcelPackage.schemaRelationships);
}
/// <summary>
/// Provides access to a namespace manager instance to allow XPath searching
/// </summary>
XmlNamespaceManager _nsManager=null;
public XmlNamespaceManager NameSpaceManager
{
get
{
return _nsManager;
}
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return (_drawings.GetEnumerator());
}
#region IEnumerable<ExcelDrawing> Members
IEnumerator<ExcelDrawing> IEnumerable<ExcelDrawing>.GetEnumerator()
{
return (_drawings.GetEnumerator());
}
#endregion
/// <summary>
/// Returns the drawing at the specified position.
/// </summary>
/// <param name="PositionID">The position of the drawing. 0-base</param>
/// <returns></returns>
public ExcelDrawing this[int PositionID]
{
get
{
return (_drawings[PositionID]);
}
}
/// <summary>
/// Returns the drawing matching the specified name
/// </summary>
/// <param name="Name">The name of the worksheet</param>
/// <returns></returns>
public ExcelDrawing this[string Name]
{
get
{
if (_drawingNames.ContainsKey(Name))
{
return _drawings[_drawingNames[Name]];
}
else
{
return null;
}
}
}
public int Count
{
get
{
if (_drawings == null)
{
return 0;
}
else
{
return _drawings.Count;
}
}
}
Packaging.ZipPackagePart _part=null;
internal Packaging.ZipPackagePart Part
{
get
{
return _part;
}
}
Uri _uriDrawing=null;
public Uri UriDrawing
{
get
{
return _uriDrawing;
}
}
#endregion
#region Add functions
/// <summary>
/// Add a new chart to the worksheet.
/// Do not support Bubble-, Radar-, Stock- or Surface charts.
/// </summary>
/// <param name="Name"></param>
/// <param name="ChartType">Type of chart</param>
/// <param name="PivotTableSource">The pivottable source for a pivotchart</param>
/// <returns>The chart</returns>
public ExcelChart AddChart(string Name, eChartType ChartType, ExcelPivotTable PivotTableSource)
{
if(_drawingNames.ContainsKey(Name))
{
throw new Exception("Name already exists in the drawings collection");
}
if (ChartType == eChartType.StockHLC ||
ChartType == eChartType.StockOHLC ||
ChartType == eChartType.StockVOHLC)
{
throw(new NotImplementedException("Chart type is not supported in the current version"));
}
if (Worksheet is ExcelChartsheet && _drawings.Count > 0)
{
throw new InvalidOperationException("Chart Worksheets can't have more than one chart");
}
XmlElement drawNode = CreateDrawingXml();
ExcelChart chart = ExcelChart.GetNewChart(this, drawNode, ChartType, null, PivotTableSource);
chart.Name = Name;
_drawings.Add(chart);
_drawingNames.Add(Name, _drawings.Count - 1);
return chart;
}
/// <summary>
/// Add a new chart to the worksheet.
/// Do not support Bubble-, Radar-, Stock- or Surface charts.
/// </summary>
/// <param name="Name"></param>
/// <param name="ChartType">Type of chart</param>
/// <returns>The chart</returns>
public ExcelChart AddChart(string Name, eChartType ChartType)
{
return AddChart(Name, ChartType, null);
}
/// <summary>
/// Add a picure to the worksheet
/// </summary>
/// <param name="Name"></param>
/// <param name="image">An image. Allways saved in then JPeg format</param>
/// <returns></returns>
public ExcelPicture AddPicture(string Name, Image image)
{
return AddPicture(Name, image, null);
}
/// <summary>
/// Add a picure to the worksheet
/// </summary>
/// <param name="Name"></param>
/// <param name="image">An image. Allways saved in then JPeg format</param>
/// <param name="Hyperlink">Picture Hyperlink</param>
/// <returns></returns>
public ExcelPicture AddPicture(string Name, Image image, Uri Hyperlink)
{
if (image != null)
{
if (_drawingNames.ContainsKey(Name))
{
throw new Exception("Name already exists in the drawings collection");
}
XmlElement drawNode = CreateDrawingXml();
drawNode.SetAttribute("editAs", "oneCell");
ExcelPicture pic = new ExcelPicture(this, drawNode, image, Hyperlink);
pic.Name = Name;
_drawings.Add(pic);
_drawingNames.Add(Name, _drawings.Count - 1);
return pic;
}
throw (new Exception("AddPicture: Image can't be null"));
}
/// <summary>
/// Add a picure to the worksheet
/// </summary>
/// <param name="Name"></param>
/// <param name="ImageFile">The image file</param>
/// <returns></returns>
public ExcelPicture AddPicture(string Name, FileInfo ImageFile)
{
return AddPicture(Name, ImageFile, null);
}
/// <summary>
/// Add a picure to the worksheet
/// </summary>
/// <param name="Name"></param>
/// <param name="ImageFile">The image file</param>
/// <param name="Hyperlink">Picture Hyperlink</param>
/// <returns></returns>
public ExcelPicture AddPicture(string Name, FileInfo ImageFile, Uri Hyperlink)
{
if (Worksheet is ExcelChartsheet && _drawings.Count > 0)
{
throw new InvalidOperationException("Chart worksheets can't have more than one drawing");
}
if (ImageFile != null)
{
if (_drawingNames.ContainsKey(Name))
{
throw new Exception("Name already exists in the drawings collection");
}
XmlElement drawNode = CreateDrawingXml();
drawNode.SetAttribute("editAs", "oneCell");
ExcelPicture pic = new ExcelPicture(this, drawNode, ImageFile, Hyperlink);
pic.Name = Name;
_drawings.Add(pic);
_drawingNames.Add(Name, _drawings.Count - 1);
return pic;
}
throw (new Exception("AddPicture: ImageFile can't be null"));
}
/// <summary>
/// Add a new shape to the worksheet
/// </summary>
/// <param name="Name">Name</param>
/// <param name="Style">Shape style</param>
/// <returns>The shape object</returns>
public ExcelShape AddShape(string Name, eShapeStyle Style)
{
if (Worksheet is ExcelChartsheet && _drawings.Count > 0)
{
throw new InvalidOperationException("Chart worksheets can't have more than one drawing");
}
if (_drawingNames.ContainsKey(Name))
{
throw new Exception("Name already exists in the drawings collection");
}
XmlElement drawNode = CreateDrawingXml();
ExcelShape shape = new ExcelShape(this, drawNode, Style);
shape.Name = Name;
shape.Style = Style;
_drawings.Add(shape);
_drawingNames.Add(Name, _drawings.Count - 1);
return shape;
}
/// <summary>
/// Add a new shape to the worksheet
/// </summary>
/// <param name="Name">Name</param>
/// <param name="Source">Source shape</param>
/// <returns>The shape object</returns>
public ExcelShape AddShape(string Name, ExcelShape Source)
{
if (Worksheet is ExcelChartsheet && _drawings.Count > 0)
{
throw new InvalidOperationException("Chart worksheets can't have more than one drawing");
}
if (_drawingNames.ContainsKey(Name))
{
throw new Exception("Name already exists in the drawings collection");
}
XmlElement drawNode = CreateDrawingXml();
drawNode.InnerXml = Source.TopNode.InnerXml;
ExcelShape shape = new ExcelShape(this, drawNode);
shape.Name = Name;
shape.Style = Source.Style;
_drawings.Add(shape);
_drawingNames.Add(Name, _drawings.Count - 1);
return shape;
}
private XmlElement CreateDrawingXml()
{
if (DrawingXml.DocumentElement == null)
{
DrawingXml.LoadXml(string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><xdr:wsDr xmlns:xdr=\"{0}\" xmlns:a=\"{1}\" />", ExcelPackage.schemaSheetDrawings, ExcelPackage.schemaDrawings));
Packaging.ZipPackage package = Worksheet._package.Package;
//Check for existing part, issue #100
var id = Worksheet.SheetID;
do
{
_uriDrawing = new Uri(string.Format("/xl/drawings/drawing{0}.xml", id++), UriKind.Relative);
}
while (package.PartExists(_uriDrawing));
_part = package.CreatePart(_uriDrawing, "application/vnd.openxmlformats-officedocument.drawing+xml", _package.Compression);
StreamWriter streamChart = new StreamWriter(_part.GetStream(FileMode.Create, FileAccess.Write));
DrawingXml.Save(streamChart);
streamChart.Close();
package.Flush();
_drawingRelation = Worksheet.Part.CreateRelationship(UriHelper.GetRelativeUri(Worksheet.WorksheetUri, _uriDrawing), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/drawing");
XmlElement e = (XmlElement)Worksheet.CreateNode("d:drawing");
//XmlElement e = Worksheet.WorksheetXml.SelectSingleNode("d:drawing", );
e.SetAttribute("id",ExcelPackage.schemaRelationships, _drawingRelation.Id);
//Worksheet.WorksheetXml.DocumentElement.AppendChild(e);
package.Flush();
}
XmlNode colNode = _drawingsXml.SelectSingleNode("//xdr:wsDr", NameSpaceManager);
XmlElement drawNode;
if (this.Worksheet is ExcelChartsheet)
{
drawNode = _drawingsXml.CreateElement("xdr", "absoluteAnchor", ExcelPackage.schemaSheetDrawings);
XmlElement posNode = _drawingsXml.CreateElement("xdr", "pos", ExcelPackage.schemaSheetDrawings);
posNode.SetAttribute("y", "0");
posNode.SetAttribute("x", "0");
drawNode.AppendChild(posNode);
XmlElement extNode = _drawingsXml.CreateElement("xdr", "ext", ExcelPackage.schemaSheetDrawings);
extNode.SetAttribute("cy", "6072876");
extNode.SetAttribute("cx", "9299263");
drawNode.AppendChild(extNode);
colNode.AppendChild(drawNode);
}
else
{
drawNode = _drawingsXml.CreateElement("xdr", "twoCellAnchor", ExcelPackage.schemaSheetDrawings);
colNode.AppendChild(drawNode);
//Add from position Element;
XmlElement fromNode = _drawingsXml.CreateElement("xdr", "from", ExcelPackage.schemaSheetDrawings);
drawNode.AppendChild(fromNode);
fromNode.InnerXml = "<xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff>";
//Add to position Element;
XmlElement toNode = _drawingsXml.CreateElement("xdr", "to", ExcelPackage.schemaSheetDrawings);
drawNode.AppendChild(toNode);
toNode.InnerXml = "<xdr:col>10</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>10</xdr:row><xdr:rowOff>0</xdr:rowOff>";
}
return drawNode;
}
#endregion
#region Remove methods
/// <summary>
/// Removes a drawing.
/// </summary>
/// <param name="Index">The index of the drawing</param>
public void Remove(int Index)
{
if (Worksheet is ExcelChartsheet && _drawings.Count > 0)
{
throw new InvalidOperationException("Can' remove charts from chart worksheets");
}
RemoveDrawing(Index);
}
internal void RemoveDrawing(int Index)
{
var draw = _drawings[Index];
draw.DeleteMe();
for (int i = Index + 1; i < _drawings.Count; i++)
{
_drawingNames[_drawings[i].Name]--;
}
_drawingNames.Remove(draw.Name);
_drawings.Remove(draw);
}
/// <summary>
/// Removes a drawing.
/// </summary>
/// <param name="Drawing">The drawing</param>
public void Remove(ExcelDrawing Drawing)
{
Remove(_drawingNames[Drawing.Name]);
}
/// <summary>
/// Removes a drawing.
/// </summary>
/// <param name="Name">The name of the drawing</param>
public void Remove(string Name)
{
Remove(_drawingNames[Name]);
}
/// <summary>
/// Removes all drawings from the collection
/// </summary>
public void Clear()
{
if (Worksheet is ExcelChartsheet && _drawings.Count > 0)
{
throw new InvalidOperationException("Can' remove charts from chart worksheets");
}
ClearDrawings();
}
internal void ClearDrawings()
{
while (Count > 0)
{
RemoveDrawing(0);
}
}
#endregion
internal void AdjustWidth(int[,] pos)
{
var ix = 0;
//Now set the size for all drawings depending on the editAs property.
foreach (OfficeOpenXml.Drawing.ExcelDrawing d in this)
{
if (d.EditAs != Drawing.eEditAs.TwoCell)
{
if (d.EditAs == Drawing.eEditAs.Absolute)
{
d.SetPixelLeft(pos[ix, 0]);
}
d.SetPixelWidth(pos[ix, 1]);
}
ix++;
}
}
internal void AdjustHeight(int[,] pos)
{
var ix = 0;
//Now set the size for all drawings depending on the editAs property.
foreach (OfficeOpenXml.Drawing.ExcelDrawing d in this)
{
if (d.EditAs != Drawing.eEditAs.TwoCell)
{
if (d.EditAs == Drawing.eEditAs.Absolute)
{
d.SetPixelTop(pos[ix, 0]);
}
d.SetPixelHeight(pos[ix, 1]);
}
ix++;
}
}
internal int[,] GetDrawingWidths()
{
int[,] pos = new int[Count, 2];
int ix = 0;
//Save the size for all drawings
foreach (ExcelDrawing d in this)
{
pos[ix, 0] = d.GetPixelLeft();
pos[ix++, 1] = d.GetPixelWidth();
}
return pos;
}
internal int[,] GetDrawingHeight()
{
int[,] pos = new int[Count, 2];
int ix = 0;
//Save the size for all drawings
foreach (ExcelDrawing d in this)
{
pos[ix, 0] = d.GetPixelTop();
pos[ix++, 1] = d.GetPixelHeight();
}
return pos;
}
public void Dispose()
{
_drawingsXml = null;
_hashes.Clear();
_hashes = null;
_part = null;
_drawingNames.Clear();
_drawingNames = null;
_drawingRelation = null;
foreach (var d in _drawings)
{
d.Dispose();
}
_drawings.Clear();
_drawings = null;
}
}
}