Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ExportSymbolicDataAnalysisSolutions/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/3.1.3/EPPlus-3.1.3/Table/ExcelTable.cs @ 9580

Last change on this file since 9580 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: 19.5 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    Added   30-AUG-2010
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Xml;
36using System.IO.Packaging;
37using System.Text.RegularExpressions;
38
39namespace OfficeOpenXml.Table
40{
41    /// <summary>
42    /// Table style Enum
43    /// </summary>
44    public enum TableStyles
45    {
46        None,
47        Custom,
48        Light1,
49        Light2,
50        Light3,
51        Light4,
52        Light5,
53        Light6,
54        Light7,
55        Light8,
56        Light9,
57        Light10,
58        Light11,
59        Light12,
60        Light13,
61        Light14,
62        Light15,
63        Light16,
64        Light17,
65        Light18,
66        Light19,
67        Light20,
68        Light21,
69        Medium1,
70        Medium2,
71        Medium3,
72        Medium4,
73        Medium5,
74        Medium6,
75        Medium7,
76        Medium8,
77        Medium9,
78        Medium10,
79        Medium11,
80        Medium12,
81        Medium13,
82        Medium14,
83        Medium15,
84        Medium16,
85        Medium17,
86        Medium18,
87        Medium19,
88        Medium20,
89        Medium21,
90        Medium22,
91        Medium23,
92        Medium24,
93        Medium25,
94        Medium26,
95        Medium27,
96        Medium28,
97        Dark1,
98        Dark2,
99        Dark3,
100        Dark4,
101        Dark5,
102        Dark6,
103        Dark7,
104        Dark8,
105        Dark9,
106        Dark10,
107        Dark11,
108    }
109    /// <summary>
110    /// An Excel Table
111    /// </summary>
112    public class ExcelTable : XmlHelper
113    {
114        internal ExcelTable(PackageRelationship rel, ExcelWorksheet sheet) :
115            base(sheet.NameSpaceManager)
116        {
117            WorkSheet = sheet;
118            TableUri = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
119            RelationshipID = rel.Id;
120            var pck = sheet._package.Package;
121            Part=pck.GetPart(TableUri);
122
123            TableXml = new XmlDocument();
124            LoadXmlSafe(TableXml, Part.GetStream());
125            init();
126            Address = new ExcelAddressBase(GetXmlNodeString("@ref"));
127        }
128        internal ExcelTable(ExcelWorksheet sheet, ExcelAddressBase address, string name, int tblId) :
129            base(sheet.NameSpaceManager)
130      {
131            WorkSheet = sheet;
132            Address = address;
133            TableXml = new XmlDocument();
134            LoadXmlSafe(TableXml, GetStartXml(name, tblId), Encoding.UTF8);
135            TopNode = TableXml.DocumentElement;
136
137            init();
138
139            //If the table is just one row we can not have a header.
140            if (address._fromRow == address._toRow)
141            {
142                ShowHeader = false;
143            }
144        }
145
146        private void init()
147        {
148            TopNode = TableXml.DocumentElement;
149            SchemaNodeOrder = new string[] { "autoFilter", "tableColumns", "tableStyleInfo" };
150        }
151        private string GetStartXml(string name, int tblId)
152        {
153            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>";
154            xml += string.Format("<table xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" id=\"{0}\" name=\"{1}\" displayName=\"{2}\" ref=\"{3}\" headerRowCount=\"1\">",
155            tblId,
156            name,
157            cleanDisplayName(name),
158            Address.Address);
159            xml += string.Format("<autoFilter ref=\"{0}\" />", Address.Address);
160
161            int cols=Address._toCol-Address._fromCol+1;
162            xml += string.Format("<tableColumns count=\"{0}\">",cols);
163            var names = new Dictionary<string, string>();           
164            for(int i=1;i<=cols;i++)
165            {
166                var cell = WorkSheet.Cells[Address._fromRow, Address._fromCol+i-1];
167                string colName;
168                if (cell.Value == null || names.ContainsKey(cell.Value.ToString()))
169                {
170                    //Get an unique name
171                    int a=i;
172                    do
173                    {
174                        colName = string.Format("Column{0}", a++);
175                    }
176                    while (names.ContainsKey(colName));
177                }
178                else
179                {
180                    colName = System.Security.SecurityElement.Escape(cell.Value.ToString());
181                }
182                names.Add(colName, colName);
183                xml += string.Format("<tableColumn id=\"{0}\" name=\"{1}\" />", i,colName);
184            }
185            xml += "</tableColumns>";
186            xml += "<tableStyleInfo name=\"TableStyleMedium9\" showFirstColumn=\"0\" showLastColumn=\"0\" showRowStripes=\"1\" showColumnStripes=\"0\" /> ";
187            xml += "</table>";
188
189            return xml;
190        }
191        private string cleanDisplayName(string name)
192        {
193            return Regex.Replace(name, @"[^\w\.-_]", "_");
194        }
195        internal PackagePart Part
196        {
197            get;
198            set;
199        }
200        /// <summary>
201        /// Provides access to the XML data representing the table in the package.
202        /// </summary>
203        public XmlDocument TableXml
204        {
205            get;
206            set;
207        }
208        /// <summary>
209        /// The package internal URI to the Table Xml Document.
210        /// </summary>
211        public Uri TableUri
212        {
213            get;
214            internal set;
215        }
216        internal string RelationshipID
217        {
218            get;
219            set;
220        }
221        const string ID_PATH = "@id";
222        internal int Id
223        {
224            get
225            {
226                return GetXmlNodeInt(ID_PATH);
227            }
228            set
229            {
230                SetXmlNodeString(ID_PATH, value.ToString());
231            }
232        }
233        const string NAME_PATH = "@name";
234        const string DISPLAY_NAME_PATH = "@displayName";
235        /// <summary>
236        /// The name of the table object in Excel
237        /// </summary>
238        public string Name
239        {
240            get
241            {
242                return GetXmlNodeString(NAME_PATH);
243            }
244            set
245            {
246                if(WorkSheet.Workbook.ExistsTableName(value))
247                {
248                    throw (new ArgumentException("Tablename is not unique"));
249                }
250                string prevName = Name;
251                if (WorkSheet.Tables._tableNames.ContainsKey(prevName))
252                {
253                    int ix=WorkSheet.Tables._tableNames[prevName];
254                    WorkSheet.Tables._tableNames.Remove(prevName);
255                    WorkSheet.Tables._tableNames.Add(value,ix);
256                }
257                SetXmlNodeString(NAME_PATH, value);
258                SetXmlNodeString(DISPLAY_NAME_PATH, cleanDisplayName(value));
259            }
260        }
261        /// <summary>
262        /// The worksheet of the table
263        /// </summary>
264        public ExcelWorksheet WorkSheet
265        {
266            get;
267            set;
268        }
269        /// <summary>
270        /// The address of the table
271        /// </summary>
272        public ExcelAddressBase Address
273        {
274            get;
275            internal set;
276        }
277        ExcelTableColumnCollection _cols = null;
278        /// <summary>
279        /// Collection of the columns in the table
280        /// </summary>
281        public ExcelTableColumnCollection Columns
282        {
283            get
284            {
285                if(_cols==null)
286                {
287                    _cols = new ExcelTableColumnCollection(this);
288                }
289                return _cols;
290            }
291        }
292        TableStyles _tableStyle = TableStyles.Medium6;
293        /// <summary>
294        /// The table style. If this property is cusom, the style from the StyleName propery is used.
295        /// </summary>
296        public TableStyles TableStyle
297        {
298            get
299            {
300                return _tableStyle;
301            }
302            set
303            {
304                _tableStyle=value;
305                if (value != TableStyles.Custom)
306                {
307                    SetXmlNodeString(STYLENAME_PATH, "TableStyle" + value.ToString());
308                }
309            }
310        }
311        const string HEADERROWCOUNT_PATH = "@headerRowCount";
312        const string AUTOFILTER_PATH = "d:autoFilter/@ref";
313        /// <summary>
314        /// If the header row is visible or not
315        /// </summary>
316        public bool ShowHeader
317        {
318            get
319            {
320                return GetXmlNodeInt(HEADERROWCOUNT_PATH)!=0;
321            }
322            set
323            {
324                if (Address._toRow - Address._fromRow < 1 && value ||
325                    Address._toRow - Address._fromRow == 1 && value && ShowTotal)
326                {
327                    throw (new Exception("Cant set ShowHeader-property. Table has too few rows"));
328                }
329
330                if(value)
331                {
332                    DeleteNode(HEADERROWCOUNT_PATH);
333                    WriteAutoFilter(ShowTotal);
334                }
335                else
336                {
337                    SetXmlNodeString(HEADERROWCOUNT_PATH, "0");
338                    DeleteAllNode(AUTOFILTER_PATH);
339                }
340            }
341        }
342        internal ExcelAddressBase AutoFilterAddress
343        {
344            get
345            {
346                string a=GetXmlNodeString(AUTOFILTER_PATH);
347                if (a == "")
348                {
349                    return null;
350                }
351                else
352                {
353                    return new ExcelAddressBase(a);
354                }
355            }
356        }
357        private void WriteAutoFilter(bool showTotal)
358        {
359            string autofilterAddress;
360            if (ShowHeader)
361            {
362                if (showTotal)
363                {
364                    autofilterAddress = ExcelCellBase.GetAddress(Address._fromRow, Address._fromCol, Address._toRow - 1, Address._toCol);
365                }
366                else
367                {
368                    autofilterAddress = Address.Address;
369                }
370                SetXmlNodeString(AUTOFILTER_PATH, autofilterAddress);
371            }
372        }
373        /// <summary>
374        /// If the header row has an autofilter
375        /// </summary>
376        public bool ShowFilter
377        {
378            get
379            {
380                return ShowHeader && AutoFilterAddress != null;
381            }
382            set
383            {
384                if (ShowHeader)
385                {
386                    if (value)
387                    {
388                        WriteAutoFilter(ShowTotal);
389                    }
390                    else
391                    {
392                        DeleteAllNode(AUTOFILTER_PATH);
393                    }
394                }
395                else if(value)
396                {
397                    throw(new InvalidOperationException("Filter can only be applied when ShowHeader is set to true"));
398                }
399            }
400        }
401        const string TOTALSROWCOUNT_PATH = "@totalsRowCount";
402        const string TOTALSROWSHOWN_PATH = "@totalsRowShown";
403        /// <summary>
404        /// If the total row is visible or not
405        /// </summary>
406        public bool ShowTotal
407        {
408            get
409            {
410                return GetXmlNodeInt(TOTALSROWCOUNT_PATH) == 1;
411            }
412            set
413            {
414                if (value != ShowTotal)
415                {
416                    if (value)
417                    {
418                        Address=new ExcelAddress(WorkSheet.Name, ExcelAddressBase.GetAddress(Address.Start.Row, Address.Start.Column, Address.End.Row+1, Address.End.Column));
419                    }
420                    else
421                    {
422                        Address = new ExcelAddress(WorkSheet.Name, ExcelAddressBase.GetAddress(Address.Start.Row, Address.Start.Column, Address.End.Row - 1, Address.End.Column));
423                    }
424                    SetXmlNodeString("@ref", Address.Address);
425                    if (value)
426                    {
427                        SetXmlNodeString(TOTALSROWCOUNT_PATH, "1");
428                    }
429                    else
430                    {
431                        DeleteNode(TOTALSROWCOUNT_PATH);
432                    }
433                    WriteAutoFilter(value);
434                }
435            }
436        }
437        const string STYLENAME_PATH = "d:tableStyleInfo/@name";
438        /// <summary>
439        /// The style name for custum styles
440        /// </summary>
441        public string StyleName
442        {
443            get
444            {
445                return GetXmlNodeString(STYLENAME_PATH);
446            }
447            set
448            {
449                if (value.StartsWith("TableStyle"))
450                {
451                    try
452                    {
453                        _tableStyle = (TableStyles)Enum.Parse(typeof(TableStyles), value.Substring(10,value.Length-10), true);
454                    }
455                    catch
456                    {
457                        _tableStyle = TableStyles.Custom;
458                    }
459                }
460                else if (value == "None")
461                {
462                    _tableStyle = TableStyles.None;
463                    value = "";
464                }
465                else
466                {
467                    _tableStyle = TableStyles.Custom;
468                }
469                SetXmlNodeString(STYLENAME_PATH,value,true);
470            }
471        }
472        const string SHOWFIRSTCOLUMN_PATH = "d:tableStyleInfo/@showFirstColumn";
473        /// <summary>
474        /// Display special formatting for the first row
475        /// </summary>
476        public bool ShowFirstColumn
477        {
478            get
479            {
480                return GetXmlNodeBool(SHOWFIRSTCOLUMN_PATH);
481            }
482            set
483            {
484                SetXmlNodeBool(SHOWFIRSTCOLUMN_PATH, value, false);
485            }   
486        }
487        const string SHOWLASTCOLUMN_PATH = "d:tableStyleInfo/@showLastColumn";
488        /// <summary>
489        /// Display special formatting for the last row
490        /// </summary>
491        public bool ShowLastColumn
492        {
493            get
494            {
495                return GetXmlNodeBool(SHOWLASTCOLUMN_PATH);
496            }
497            set
498            {
499                SetXmlNodeBool(SHOWLASTCOLUMN_PATH, value, false);
500            }
501        }
502        const string SHOWROWSTRIPES_PATH = "d:tableStyleInfo/@showRowStripes";
503        /// <summary>
504        /// Display banded rows
505        /// </summary>
506        public bool ShowRowStripes
507        {
508            get
509            {
510                return GetXmlNodeBool(SHOWROWSTRIPES_PATH);
511            }
512            set
513            {
514                SetXmlNodeBool(SHOWROWSTRIPES_PATH, value, false);
515            }
516        }
517        const string SHOWCOLUMNSTRIPES_PATH = "d:tableStyleInfo/@showColumnStripes";
518        /// <summary>
519        /// Display banded columns
520        /// </summary>
521        public bool ShowColumnStripes
522        {
523            get
524            {
525                return GetXmlNodeBool(SHOWCOLUMNSTRIPES_PATH);
526            }
527            set
528            {
529                SetXmlNodeBool(SHOWCOLUMNSTRIPES_PATH, value, false);
530            }
531        }
532
533        const string TOTALSROWCELLSTYLE_PATH = "@totalsRowCellStyle";
534        /// <summary>
535        /// Named style used for the total row
536        /// </summary>
537        public string TotalsRowCellStyle
538        {
539            get
540            {
541                return GetXmlNodeString(TOTALSROWCELLSTYLE_PATH);
542            }
543            set
544            {
545                if (WorkSheet.Workbook.Styles.NamedStyles.FindIndexByID(value) < 0)
546                {
547                    throw (new Exception(string.Format("Named style {0} does not exist.", value)));
548                }
549                SetXmlNodeString(TopNode, TOTALSROWCELLSTYLE_PATH, value, true);
550
551                if (ShowTotal)
552                {
553                    WorkSheet.Cells[Address._toRow, Address._fromCol, Address._toRow, Address._toCol].StyleName = value;
554                }
555            }
556        }
557        const string DATACELLSTYLE_PATH = "@dataCellStyle";
558        /// <summary>
559        /// Named style used for the data cells
560        /// </summary>
561        public string DataCellStyleName
562        {
563            get
564            {
565                return GetXmlNodeString(DATACELLSTYLE_PATH);
566            }
567            set
568            {
569                if (WorkSheet.Workbook.Styles.NamedStyles.FindIndexByID(value) < 0)
570                {
571                    throw (new Exception(string.Format("Named style {0} does not exist.", value)));
572                }
573                SetXmlNodeString(TopNode, DATACELLSTYLE_PATH, value, true);
574
575                int fromRow = Address._fromRow + (ShowHeader ? 1 : 0),
576                    toRow = Address._toRow - (ShowTotal ? 1 : 0);
577
578                if (fromRow < toRow)
579                {
580                    WorkSheet.Cells[fromRow, Address._fromCol, toRow, Address._toCol].StyleName = value;
581                }
582            }
583        }
584        const string HEADERROWCELLSTYLE_PATH = "@headerRowCellStyle";
585        /// <summary>
586        /// Named style used for the header row
587        /// </summary>
588        public string HeaderRowCellStyle
589        {
590            get
591            {
592                return GetXmlNodeString(HEADERROWCELLSTYLE_PATH);
593            }
594            set
595            {
596                if (WorkSheet.Workbook.Styles.NamedStyles.FindIndexByID(value) < 0)
597                {
598                    throw (new Exception(string.Format("Named style {0} does not exist.", value)));
599                }
600                SetXmlNodeString(TopNode, HEADERROWCELLSTYLE_PATH, value, true);
601
602                if (ShowHeader)
603                {
604                    WorkSheet.Cells[Address._fromRow, Address._fromCol, Address._fromRow, Address._toCol].StyleName = value;
605                }
606
607            }
608        }       
609    }
610}
Note: See TracBrowser for help on using the repository browser.