Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ExportSymbolicDataAnalysisSolutions/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/3.1.3/EPPlus-3.1.3/Drawing/ExcelDrawings.cs @ 9931

Last change on this file since 9931 was 9580, checked in by sforsten, 11 years ago

#1730:

  • added SymbolicDataAnalysisExpressionExcelFormatter
  • changed modifiers in SymbolicExpressionTreeChart of methods SaveImageAsBitmap and SaveImageAsEmf to public
  • added menu item ExportSymbolicSolutionToExcelMenuItem to export a symbolic solution to an excel file
  • added EPPlus-3.1.3 to ExtLibs
File size: 20.4 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Jan Källman                    Initial Release           2009-12-22
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.IO.Packaging;
36using System.Xml;
37using System.Collections;
38using System.IO;
39using System.Drawing;
40using System.Linq;
41using OfficeOpenXml.Drawing.Chart;
42using OfficeOpenXml.Table.PivotTable;
43namespace OfficeOpenXml.Drawing
44{
45    /// <summary>
46    /// Collection for Drawing objects.
47    /// </summary>
48    public class ExcelDrawings : IEnumerable<ExcelDrawing>
49    {
50        private XmlDocument _drawingsXml=new XmlDocument();
51        private Dictionary<string, int> _drawingNames;
52        private List<ExcelDrawing> _drawings;
53        internal class ImageCompare
54        {
55            internal byte[] image { get; set; }
56            internal string relID { get; set; }
57
58            internal bool Comparer(byte[] compareImg)
59            {
60                if (compareImg.Length != image.Length)
61                {
62                    return false;
63                }
64
65                for (int i = 0; i < image.Length; i++)
66                {
67                    if (image[i] != compareImg[i])
68                    {
69                        return false;
70                    }
71                }
72                return true; //Equal
73            }
74        }
75        //internal List<ImageCompare> _pics = new List<ImageCompare>();
76        internal Dictionary<string, string> _hashes = new Dictionary<string, string>();
77        internal ExcelPackage _package;
78        internal PackageRelationship _drawingRelation=null;
79        internal ExcelDrawings(ExcelPackage xlPackage, ExcelWorksheet sheet)
80        {
81                _drawingsXml = new XmlDocument();               
82                _drawingsXml.PreserveWhitespace = false;
83                _drawings = new List<ExcelDrawing>();
84                _drawingNames = new Dictionary<string,int>();
85                _package = xlPackage;
86                Worksheet = sheet;
87                XmlNode node = sheet.WorksheetXml.SelectSingleNode("//d:drawing", sheet.NameSpaceManager);
88                CreateNSM();
89                if (node != null)
90                {
91                    _drawingRelation = sheet.Part.GetRelationship(node.Attributes["r:id"].Value);
92                    _uriDrawing = PackUriHelper.ResolvePartUri(sheet.WorksheetUri, _drawingRelation.TargetUri);
93
94                    _part = xlPackage.Package.GetPart(_uriDrawing);
95                    XmlHelper.LoadXmlSafe(_drawingsXml, _part.GetStream());
96
97                    AddDrawings();
98                }
99         }
100        internal ExcelWorksheet Worksheet { get; set; }
101        /// <summary>
102        /// A reference to the drawing xml document
103        /// </summary>
104        public XmlDocument DrawingXml
105        {
106            get
107            {
108                return _drawingsXml;
109            }
110        }
111        private void AddDrawings()
112        {
113            XmlNodeList list = _drawingsXml.SelectNodes("//xdr:twoCellAnchor", NameSpaceManager);
114
115            foreach (XmlNode node in list)
116            {
117                ExcelDrawing dr = ExcelDrawing.GetDrawing(this, node);
118                _drawings.Add(dr);
119                if (!_drawingNames.ContainsKey(dr.Name.ToLower()))
120                {
121                    _drawingNames.Add(dr.Name.ToLower(), _drawings.Count - 1);
122                }
123            }
124        }
125
126
127        #region NamespaceManager
128        /// <summary>
129        /// Creates the NamespaceManager.
130        /// </summary>
131        private void CreateNSM()
132        {
133            NameTable nt = new NameTable();
134            _nsManager = new XmlNamespaceManager(nt);
135            _nsManager.AddNamespace("a", ExcelPackage.schemaDrawings);
136            _nsManager.AddNamespace("xdr", ExcelPackage.schemaSheetDrawings);
137            _nsManager.AddNamespace("c", ExcelPackage.schemaChart);
138            _nsManager.AddNamespace("r", ExcelPackage.schemaRelationships);
139        }
140        /// <summary>
141        /// Provides access to a namespace manager instance to allow XPath searching
142        /// </summary>
143        XmlNamespaceManager _nsManager=null;
144        public XmlNamespaceManager NameSpaceManager
145        {
146            get
147            {
148                return _nsManager;
149            }
150        }
151        #endregion
152        #region IEnumerable Members
153
154        public IEnumerator GetEnumerator()
155        {
156            return (_drawings.GetEnumerator());
157        }
158        #region IEnumerable<ExcelDrawing> Members
159
160        IEnumerator<ExcelDrawing> IEnumerable<ExcelDrawing>.GetEnumerator()
161        {
162            return (_drawings.GetEnumerator());
163        }
164
165        #endregion
166
167        /// <summary>
168        /// Returns the drawing at the specified position. 
169        /// </summary>
170        /// <param name="PositionID">The position of the drawing. 0-base</param>
171        /// <returns></returns>
172        public ExcelDrawing this[int PositionID]
173        {
174            get
175            {
176                return (_drawings[PositionID]);
177            }
178        }
179
180        /// <summary>
181        /// Returns the drawing matching the specified name
182        /// </summary>
183        /// <param name="Name">The name of the worksheet</param>
184        /// <returns></returns>
185        public ExcelDrawing this[string Name]
186        {
187            get
188            {
189                if (_drawingNames.ContainsKey(Name.ToLower()))
190                {
191                    return _drawings[_drawingNames[Name.ToLower()]];
192                }
193                else
194                {
195                    return null;
196                }
197            }
198        }
199        public int Count
200        {
201            get
202            {
203                if (_drawings == null)
204                {
205                    return 0;
206                }
207                else
208                {
209                    return _drawings.Count;
210                }
211            }
212        }
213        PackagePart _part=null;
214        internal PackagePart Part
215        {
216            get
217            {
218                return _part;
219            }       
220        }
221        Uri _uriDrawing=null;
222        public Uri UriDrawing
223        {
224            get
225            {
226                return _uriDrawing;
227            }
228        }
229        #endregion
230        #region Add functions
231            /// <summary>
232            /// Add a new chart to the worksheet.
233            /// Do not support Bubble-, Radar-, Stock- or Surface charts.
234            /// </summary>
235            /// <param name="Name"></param>
236            /// <param name="ChartType">Type of chart</param>
237            /// <param name="PivotTableSource">The pivottable source for a pivotchart</param>   
238            /// <returns>The chart</returns>
239            public ExcelChart AddChart(string Name, eChartType ChartType, ExcelPivotTable PivotTableSource)
240            {
241                if(_drawingNames.ContainsKey(Name.ToLower()))
242                {
243                    throw new Exception("Name already exists in the drawings collection");
244                }
245
246                if (ChartType == eChartType.Bubble ||
247                    ChartType == eChartType.Bubble3DEffect ||
248                    ChartType == eChartType.Radar ||
249                    ChartType == eChartType.RadarFilled ||
250                    ChartType == eChartType.RadarMarkers ||
251                    ChartType == eChartType.StockHLC ||
252                    ChartType == eChartType.StockOHLC ||
253                    ChartType == eChartType.StockVOHLC ||
254                    ChartType == eChartType.Surface ||
255                    ChartType == eChartType.SurfaceTopView ||
256                    ChartType == eChartType.SurfaceTopViewWireframe ||
257                    ChartType == eChartType.SurfaceWireframe)
258                {
259                    throw(new NotImplementedException("Chart type is not supported in the current version"));
260                }
261
262                XmlElement drawNode = CreateDrawingXml();
263
264                ExcelChart chart = ExcelChart.GetNewChart(this, drawNode, ChartType, null, PivotTableSource);
265                chart.Name = Name;
266                _drawings.Add(chart);
267                _drawingNames.Add(Name.ToLower(), _drawings.Count - 1);
268                return chart;
269            }
270            /// <summary>
271            /// Add a new chart to the worksheet.
272            /// Do not support Bubble-, Radar-, Stock- or Surface charts.
273            /// </summary>
274            /// <param name="Name"></param>
275            /// <param name="ChartType">Type of chart</param>
276            /// <returns>The chart</returns>
277            public ExcelChart AddChart(string Name, eChartType ChartType)
278            {
279                return AddChart(Name, ChartType, null);
280            }
281            /// <summary>
282            /// Add a picure to the worksheet
283            /// </summary>
284            /// <param name="Name"></param>
285            /// <param name="image">An image. Allways saved in then JPeg format</param>
286            /// <returns></returns>
287            public ExcelPicture AddPicture(string Name, Image image)
288            {
289               return AddPicture(Name, image, null);
290            }
291            /// <summary>
292            /// Add a picure to the worksheet
293            /// </summary>
294            /// <param name="Name"></param>
295            /// <param name="image">An image. Allways saved in then JPeg format</param>
296            /// <param name="Hyperlink">Picture Hyperlink</param>
297            /// <returns></returns>
298            public ExcelPicture AddPicture(string Name, Image image, Uri Hyperlink)
299            {
300                if (image != null)
301                {
302                    if (_drawingNames.ContainsKey(Name.ToLower()))
303                    {
304                        throw new Exception("Name already exists in the drawings collection");
305                    }
306                    XmlElement drawNode = CreateDrawingXml();
307                    drawNode.SetAttribute("editAs", "oneCell");
308                    ExcelPicture pic = new ExcelPicture(this, drawNode, image, Hyperlink);
309                    pic.Name = Name;
310                    _drawings.Add(pic);
311                    _drawingNames.Add(Name.ToLower(), _drawings.Count - 1);
312                    return pic;
313                }
314                throw (new Exception("AddPicture: Image can't be null"));
315            }
316            /// <summary>
317            /// Add a picure to the worksheet
318            /// </summary>
319            /// <param name="Name"></param>
320            /// <param name="ImageFile">The image file</param>
321            /// <returns></returns>
322            public ExcelPicture AddPicture(string Name, FileInfo ImageFile)
323            {
324               return AddPicture(Name, ImageFile, null);
325            }
326            /// <summary>
327            /// Add a picure to the worksheet
328            /// </summary>
329            /// <param name="Name"></param>
330            /// <param name="ImageFile">The image file</param>
331            /// <param name="Hyperlink">Picture Hyperlink</param>
332            /// <returns></returns>
333            public ExcelPicture AddPicture(string Name, FileInfo ImageFile, Uri Hyperlink)
334            {
335               if (ImageFile != null)
336               {
337                  if (_drawingNames.ContainsKey(Name.ToLower()))
338                  {
339                     throw new Exception("Name already exists in the drawings collection");
340                  }
341                  XmlElement drawNode = CreateDrawingXml();
342                  drawNode.SetAttribute("editAs", "oneCell");
343                  ExcelPicture pic = new ExcelPicture(this, drawNode, ImageFile, Hyperlink);
344                  pic.Name = Name;
345                  _drawings.Add(pic);
346                  _drawingNames.Add(Name.ToLower(), _drawings.Count - 1);
347                  return pic;
348               }
349               throw (new Exception("AddPicture: ImageFile can't be null"));
350            }
351
352        /// <summary>
353        /// Add a new shape to the worksheet
354        /// </summary>
355        /// <param name="Name">Name</param>
356        /// <param name="Style">Shape style</param>
357        /// <returns>The shape object</returns>
358   
359        public ExcelShape AddShape(string Name, eShapeStyle Style)
360            {
361                if (_drawingNames.ContainsKey(Name.ToLower()))
362                {
363                    throw new Exception("Name already exists in the drawings collection");
364                }
365                XmlElement drawNode = CreateDrawingXml();
366                ExcelShape shape = new ExcelShape(this, drawNode, Style);
367                shape.Name = Name;
368                shape.Style = Style;
369                _drawings.Add(shape);
370                _drawingNames.Add(Name.ToLower(), _drawings.Count - 1);
371                return shape;
372            }
373            private XmlElement CreateDrawingXml()
374            {
375                if (DrawingXml.OuterXml == "")
376                {
377                    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));
378                    _uriDrawing = new Uri(string.Format("/xl/drawings/drawing{0}.xml", Worksheet.SheetID),UriKind.Relative);
379
380                    Package package = Worksheet._package.Package;
381                    _part = package.CreatePart(_uriDrawing, "application/vnd.openxmlformats-officedocument.drawing+xml", _package.Compression);
382
383                    StreamWriter streamChart = new StreamWriter(_part.GetStream(FileMode.Create, FileAccess.Write));
384                    DrawingXml.Save(streamChart);
385                    streamChart.Close();
386                    package.Flush();
387
388                    _drawingRelation = Worksheet.Part.CreateRelationship(PackUriHelper.GetRelativeUri(Worksheet.WorksheetUri, _uriDrawing), TargetMode.Internal, ExcelPackage.schemaRelationships + "/drawing");
389                    XmlElement e = Worksheet.WorksheetXml.CreateElement("drawing", ExcelPackage.schemaMain);
390                    e.SetAttribute("id",ExcelPackage.schemaRelationships, _drawingRelation.Id);
391
392                    Worksheet.WorksheetXml.DocumentElement.AppendChild(e);
393                    package.Flush();                   
394                }
395                XmlNode colNode = _drawingsXml.SelectSingleNode("//xdr:wsDr", NameSpaceManager);
396                XmlElement drawNode = _drawingsXml.CreateElement("xdr", "twoCellAnchor", ExcelPackage.schemaSheetDrawings);
397                colNode.AppendChild(drawNode);
398
399                //Add from position Element;
400                XmlElement fromNode = _drawingsXml.CreateElement("xdr","from", ExcelPackage.schemaSheetDrawings);
401                drawNode.AppendChild(fromNode);
402                fromNode.InnerXml = "<xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff>";
403
404                //Add to position Element;
405                XmlElement toNode = _drawingsXml.CreateElement("xdr", "to", ExcelPackage.schemaSheetDrawings);
406                drawNode.AppendChild(toNode);
407                toNode.InnerXml = "<xdr:col>10</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>10</xdr:row><xdr:rowOff>0</xdr:rowOff>";
408                return drawNode;
409            }
410        #endregion
411        #region Remove methods
412            /// <summary>
413            /// Removes a drawing.
414            /// </summary>
415            /// <param name="Index">The index of the drawing</param>
416            public void Remove(int Index)
417        {
418            var draw=_drawings[Index];
419            draw.DeleteMe();
420            for (int i = Index + 1; i < _drawings.Count; i++)
421            {
422                _drawingNames[_drawings[i].Name.ToLower()]--;
423            }
424            _drawingNames.Remove(draw.Name.ToLower());
425            _drawings.Remove(draw);
426        }
427        /// <summary>
428        /// Removes a drawing.
429        /// </summary>
430        /// <param name="Drawing">The drawing</param>
431        public void Remove(ExcelDrawing Drawing)
432        {
433            Remove(_drawingNames[Drawing.Name.ToLower()]);
434        }
435        /// <summary>
436        /// Removes a drawing.
437        /// </summary>
438        /// <param name="Name">The name of the drawing</param>
439        public void Remove(string Name)
440        {
441            Remove(_drawingNames[Name.ToLower()]);
442        }
443        /// <summary>
444        /// Removes all drawings from the collection
445        /// </summary>
446        public void Clear()
447        {
448            while (Count > 0)
449            {
450                Remove(0);
451            }
452        }
453        #endregion
454            internal void AdjustWidth(int[,] pos)
455            {
456                var ix = 0;
457                //Now set the size for all drawings depending on the editAs property.
458                foreach (OfficeOpenXml.Drawing.ExcelDrawing d in this)
459                {
460                    if (d.EditAs != Drawing.eEditAs.TwoCell)
461                    {
462                        if (d.EditAs == Drawing.eEditAs.Absolute)
463                        {
464                            d.SetPixelLeft(pos[ix, 0]);
465                        }
466                        d.SetPixelWidth(pos[ix, 1]);
467                       
468                    }
469                    ix++;
470                }
471            }
472            internal void AdjustHeight(int[,] pos)
473            {
474                var ix = 0;
475                //Now set the size for all drawings depending on the editAs property.
476                foreach (OfficeOpenXml.Drawing.ExcelDrawing d in this)
477                {
478                    if (d.EditAs != Drawing.eEditAs.TwoCell)
479                    {
480                        if (d.EditAs == Drawing.eEditAs.Absolute)
481                        {
482                            d.SetPixelTop(pos[ix, 0]);
483                        }
484                        d.SetPixelHeight(pos[ix, 1]);
485
486                    }
487                    ix++;
488                }
489            }
490            internal int[,] GetDrawingWidths()
491            {
492                int[,] pos = new int[Count, 2];
493                int ix = 0;
494                //Save the size for all drawings
495                foreach (ExcelDrawing d in this)
496                {
497                    pos[ix, 0] = d.GetPixelLeft();
498                    pos[ix++, 1] = d.GetPixelWidth();
499                }
500                return pos;
501            }
502            internal int[,] GetDrawingHeight()
503            {
504                int[,] pos = new int[Count, 2];
505                int ix = 0;
506                //Save the size for all drawings
507                foreach (ExcelDrawing d in this)
508                {
509                    pos[ix, 0] = d.GetPixelTop();
510                    pos[ix++, 1] = d.GetPixelHeight();
511                }
512                return pos;
513            }
514    }
515}
Note: See TracBrowser for help on using the repository browser.