diff --git a/EPPlus/Table/ExcelTable.cs b/EPPlus/Table/ExcelTable.cs index 1cedfb677..7be59607c 100644 --- a/EPPlus/Table/ExcelTable.cs +++ b/EPPlus/Table/ExcelTable.cs @@ -29,15 +29,14 @@ * Jan Källman Added 30-AUG-2010 * Jan Källman License changed GPL-->LGPL 2011-12-16 *******************************************************************************/ +using OfficeOpenXml.FormulaParsing.ExcelUtilities; +using OfficeOpenXml.Utils; using System; using System.Collections.Generic; +using System.Security; using System.Text; -using System.Xml; using System.Text.RegularExpressions; -using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; -using OfficeOpenXml.Utils; -using System.Security; -using OfficeOpenXml.FormulaParsing.ExcelUtilities; +using System.Xml; namespace OfficeOpenXml.Table { @@ -114,27 +113,27 @@ public enum TableStyles /// public class ExcelTable : XmlHelper, IEqualityComparer { - internal ExcelTable(Packaging.ZipPackageRelationship rel, ExcelWorksheet sheet) : + internal ExcelTable(Packaging.ZipPackageRelationship rel, ExcelWorksheet sheet) : base(sheet.NameSpaceManager) { WorkSheet = sheet; TableUri = UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri); RelationshipID = rel.Id; var pck = sheet._package.Package; - Part=pck.GetPart(TableUri); + Part = pck.GetPart(TableUri); TableXml = new XmlDocument(); LoadXmlSafe(TableXml, Part.GetStream()); init(); Address = new ExcelAddressBase(GetXmlNodeString("@ref")); } - internal ExcelTable(ExcelWorksheet sheet, ExcelAddressBase address, string name, int tblId) : + internal ExcelTable(ExcelWorksheet sheet, ExcelAddressBase address, string name, int tblId) : base(sheet.NameSpaceManager) - { + { WorkSheet = sheet; Address = address; TableXml = new XmlDocument(); - LoadXmlSafe(TableXml, GetStartXml(name, tblId), Encoding.UTF8); + LoadXmlSafe(TableXml, GetStartXml(name, tblId), Encoding.UTF8); TopNode = TableXml.DocumentElement; init(); @@ -162,17 +161,17 @@ private string GetStartXml(string name, int tblId) Address.Address); xml += string.Format("", Address.Address); - int cols=Address._toCol-Address._fromCol+1; - xml += string.Format("",cols); - var names = new Dictionary(); - for(int i=1;i<=cols;i++) + int cols = Address._toCol - Address._fromCol + 1; + xml += string.Format("", cols); + var names = new Dictionary(StringComparer.OrdinalIgnoreCase); + for (int i = 1; i <= cols; i++) { - var cell = WorkSheet.Cells[Address._fromRow, Address._fromCol+i-1]; + var cell = WorkSheet.Cells[Address._fromRow, Address._fromCol + i - 1]; string colName; if (cell.Value == null || names.ContainsKey(cell.Value.ToString())) { //Get an unique name - int a=i; + int a = i; do { colName = string.Format("Column{0}", a++); @@ -184,7 +183,7 @@ private string GetStartXml(string name, int tblId) colName = SecurityElement.Escape(cell.Value.ToString()); } names.Add(colName, colName); - xml += string.Format("", i,colName); + xml += string.Format("", i, colName); } xml += ""; xml += " "; @@ -192,7 +191,7 @@ private string GetStartXml(string name, int tblId) return xml; } - internal static string CleanDisplayName(string name) + internal static string CleanDisplayName(string name) { return Regex.Replace(name, @"[^\w\.-_]", "_"); } @@ -223,7 +222,7 @@ internal string RelationshipID set; } const string ID_PATH = "@id"; - internal int Id + internal int Id { get { @@ -245,18 +244,18 @@ public string Name { return GetXmlNodeString(NAME_PATH); } - set + set { - if(Name.Equals(value, StringComparison.CurrentCultureIgnoreCase)==false && WorkSheet.Workbook.ExistsTableName(value)) + if (Name.Equals(value, StringComparison.CurrentCultureIgnoreCase) == false && WorkSheet.Workbook.ExistsTableName(value)) { throw (new ArgumentException("Tablename is not unique")); } string prevName = Name; if (WorkSheet.Tables._tableNames.ContainsKey(prevName)) { - int ix=WorkSheet.Tables._tableNames[prevName]; + int ix = WorkSheet.Tables._tableNames[prevName]; WorkSheet.Tables._tableNames.Remove(prevName); - WorkSheet.Tables._tableNames.Add(value,ix); + WorkSheet.Tables._tableNames.Add(value, ix); } SetXmlNodeString(NAME_PATH, value); SetXmlNodeString(DISPLAY_NAME_PATH, ExcelAddressUtil.GetValidName(value)); @@ -284,7 +283,7 @@ public ExcelAddressBase Address internal set { _address = value; - SetXmlNodeString("@ref",value.Address); + SetXmlNodeString("@ref", value.Address); WriteAutoFilter(ShowTotal); } } @@ -296,7 +295,7 @@ public ExcelTableColumnCollection Columns { get { - if(_cols==null) + if (_cols == null) { _cols = new ExcelTableColumnCollection(this); } @@ -315,7 +314,7 @@ public TableStyles TableStyle } set { - _tableStyle=value; + _tableStyle = value; if (value != TableStyles.Custom) { SetXmlNodeString(STYLENAME_PATH, "TableStyle" + value.ToString()); @@ -331,7 +330,7 @@ public bool ShowHeader { get { - return GetXmlNodeInt(HEADERROWCOUNT_PATH)!=0; + return GetXmlNodeInt(HEADERROWCOUNT_PATH) != 0; } set { @@ -341,14 +340,14 @@ public bool ShowHeader throw (new Exception("Cant set ShowHeader-property. Table has too few rows")); } - if(value) + if (value) { DeleteNode(HEADERROWCOUNT_PATH); WriteAutoFilter(ShowTotal); for (int i = 0; i < Columns.Count; i++) { var v = WorkSheet.GetValue(Address._fromRow, Address._fromCol + i); - if(string.IsNullOrEmpty(v)) + if (string.IsNullOrEmpty(v)) { WorkSheet.SetValue(Address._fromRow, Address._fromCol + i, _cols[i].Name); } @@ -369,7 +368,7 @@ internal ExcelAddressBase AutoFilterAddress { get { - string a=GetXmlNodeString(AUTOFILTER_PATH); + string a = GetXmlNodeString(AUTOFILTER_PATH); if (a == "") { return null; @@ -399,8 +398,8 @@ private void WriteAutoFilter(bool showTotal) /// /// If the header row has an autofilter /// - public bool ShowFilter - { + public bool ShowFilter + { get { return ShowHeader && AutoFilterAddress != null; @@ -413,14 +412,14 @@ public bool ShowFilter { WriteAutoFilter(ShowTotal); } - else + else { DeleteAllNode(AUTOFILTER_PATH); } } - else if(value) + else if (value) { - throw(new InvalidOperationException("Filter can only be applied when ShowHeader is set to true")); + throw (new InvalidOperationException("Filter can only be applied when ShowHeader is set to true")); } } } @@ -441,7 +440,7 @@ public bool ShowTotal { if (value) { - Address=new ExcelAddress(WorkSheet.Name, ExcelAddressBase.GetAddress(Address.Start.Row, Address.Start.Column, Address.End.Row+1, Address.End.Column)); + Address = new ExcelAddress(WorkSheet.Name, ExcelAddressBase.GetAddress(Address.Start.Row, Address.Start.Column, Address.End.Row + 1, Address.End.Column)); } else { @@ -476,7 +475,7 @@ public string StyleName { try { - _tableStyle = (TableStyles)Enum.Parse(typeof(TableStyles), value.Substring(10,value.Length-10), true); + _tableStyle = (TableStyles)Enum.Parse(typeof(TableStyles), value.Substring(10, value.Length - 10), true); } catch { @@ -492,7 +491,7 @@ public string StyleName { _tableStyle = TableStyles.Custom; } - SetXmlNodeString(STYLENAME_PATH,value,true); + SetXmlNodeString(STYLENAME_PATH, value, true); } } const string SHOWFIRSTCOLUMN_PATH = "d:tableStyleInfo/@showFirstColumn"; @@ -508,7 +507,7 @@ public bool ShowFirstColumn set { SetXmlNodeBool(SHOWFIRSTCOLUMN_PATH, value, false); - } + } } const string SHOWLASTCOLUMN_PATH = "d:tableStyleInfo/@showLastColumn"; ///