diff --git a/ClosedXML/ClosedXML/ClosedXML.sln b/ClosedXML/ClosedXML/ClosedXML.sln index 9a36446..e56d111 100644 --- a/ClosedXML/ClosedXML/ClosedXML.sln +++ b/ClosedXML/ClosedXML/ClosedXML.sln @@ -1,8 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClosedXML", "ClosedXML\ClosedXML.csproj", "{BD5E6BFE-E837-4A35-BCA9-39667D873A20}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClosedXML_Sandbox", "ClosedXML_Sandbox\ClosedXML_Sandbox.csproj", "{38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5C94E22C-85AA-48FD-B082-CF929FFC6C31}" @@ -13,27 +11,29 @@ EndProject Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ClosedXML_Sandbox_VB", "ClosedXML_Sandbox_VB\ClosedXML_Sandbox_VB.vbproj", "{E005997D-B192-461F-AA3E-44007A33A3DF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClosedXML", "ClosedXML\ClosedXML.csproj", "{BD5E6BFE-E837-4A35-BCA9-39667D873A20}" +EndProject Global GlobalSection(TeamFoundationVersionControl) = preSolution SccNumberOfProjects = 6 SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C} SccTeamFoundationServer = https://tfs.codeplex.com/tfs/tfs09 SccLocalPath0 = . - SccProjectUniqueName1 = ClosedXML\\ClosedXML.csproj - SccProjectName1 = ClosedXML - SccLocalPath1 = ClosedXML - SccProjectUniqueName2 = ClosedXML_Examples\\ClosedXML_Examples.csproj - SccProjectName2 = ClosedXML_Examples - SccLocalPath2 = ClosedXML_Examples - SccProjectUniqueName3 = ClosedXML_Sandbox\\ClosedXML_Sandbox.csproj - SccProjectName3 = ClosedXML_Sandbox - SccLocalPath3 = ClosedXML_Sandbox - SccProjectUniqueName4 = ClosedXML_Net3.5\\ClosedXML_Net3.5.csproj - SccProjectName4 = ClosedXML_Net3.5 - SccLocalPath4 = ClosedXML_Net3.5 - SccProjectUniqueName5 = ClosedXML_Sandbox_VB\\ClosedXML_Sandbox_VB.vbproj - SccProjectName5 = ClosedXML_Sandbox_VB - SccLocalPath5 = ClosedXML_Sandbox_VB + SccProjectUniqueName1 = ClosedXML_Examples\\ClosedXML_Examples.csproj + SccProjectName1 = ClosedXML_Examples + SccLocalPath1 = ClosedXML_Examples + SccProjectUniqueName2 = ClosedXML_Sandbox\\ClosedXML_Sandbox.csproj + SccProjectName2 = ClosedXML_Sandbox + SccLocalPath2 = ClosedXML_Sandbox + SccProjectUniqueName3 = ClosedXML_Net3.5\\ClosedXML_Net3.5.csproj + SccProjectName3 = ClosedXML_Net3.5 + SccLocalPath3 = ClosedXML_Net3.5 + SccProjectUniqueName4 = ClosedXML_Sandbox_VB\\ClosedXML_Sandbox_VB.vbproj + SccProjectName4 = ClosedXML_Sandbox_VB + SccLocalPath4 = ClosedXML_Sandbox_VB + SccProjectUniqueName5 = ClosedXML\\ClosedXML.csproj + SccProjectName5 = ClosedXML + SccLocalPath5 = ClosedXML EndGlobalSection GlobalSection(TestCaseManagementSettings) = postSolution CategoryFile = ClosedXML.vsmdi @@ -41,66 +41,88 @@ GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|Mixed Platforms = Debug|Mixed Platforms + Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU Release|Mixed Platforms = Release|Mixed Platforms + Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Any CPU.Build.0 = Debug|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|x86.ActiveCfg = Debug|x86 - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|x86.Build.0 = Debug|x86 - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Any CPU.ActiveCfg = Release|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Any CPU.Build.0 = Release|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|x86.ActiveCfg = Release|Any CPU - {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|x86.Build.0 = Release|Any CPU {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Debug|Any CPU.ActiveCfg = Debug|x86 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Debug|Mixed Platforms.Build.0 = Debug|x86 + {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Debug|x64.ActiveCfg = Debug|x64 + {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Debug|x64.Build.0 = Debug|x64 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Debug|x86.ActiveCfg = Debug|x86 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Debug|x86.Build.0 = Debug|x86 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Release|Any CPU.ActiveCfg = Release|x86 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Release|Mixed Platforms.ActiveCfg = Release|x86 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Release|Mixed Platforms.Build.0 = Release|x86 + {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Release|x64.ActiveCfg = Release|x64 + {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Release|x64.Build.0 = Release|x64 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Release|x86.ActiveCfg = Release|x64 {38B882F0-E6F2-45C5-9BE9-CDC27FBEB4AB}.Release|x86.Build.0 = Release|x64 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Debug|Any CPU.ActiveCfg = Debug|x86 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Debug|Mixed Platforms.Build.0 = Debug|x86 + {03A518D0-1CB7-488E-861C-C4E782B27A46}.Debug|x64.ActiveCfg = Debug|x64 + {03A518D0-1CB7-488E-861C-C4E782B27A46}.Debug|x64.Build.0 = Debug|x64 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Debug|x86.ActiveCfg = Debug|x86 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Debug|x86.Build.0 = Debug|x86 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Release|Any CPU.ActiveCfg = Release|x86 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Release|Mixed Platforms.ActiveCfg = Release|x86 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Release|Mixed Platforms.Build.0 = Release|x86 + {03A518D0-1CB7-488E-861C-C4E782B27A46}.Release|x64.ActiveCfg = Release|x64 + {03A518D0-1CB7-488E-861C-C4E782B27A46}.Release|x64.Build.0 = Release|x64 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Release|x86.ActiveCfg = Release|x64 {03A518D0-1CB7-488E-861C-C4E782B27A46}.Release|x86.Build.0 = Release|x64 {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|Any CPU.Build.0 = Debug|Any CPU {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|x64.ActiveCfg = Debug|x64 + {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|x64.Build.0 = Debug|x64 {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|x86.ActiveCfg = Debug|x86 {5F43B12B-A900-40C6-9924-A0C0B032F791}.Debug|x86.Build.0 = Debug|x86 {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|Any CPU.ActiveCfg = Release|Any CPU {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|Any CPU.Build.0 = Release|Any CPU {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|x64.ActiveCfg = Release|x64 + {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|x64.Build.0 = Release|x64 {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|x86.ActiveCfg = Release|Any CPU {5F43B12B-A900-40C6-9924-A0C0B032F791}.Release|x86.Build.0 = Release|Any CPU {E005997D-B192-461F-AA3E-44007A33A3DF}.Debug|Any CPU.ActiveCfg = Debug|x86 {E005997D-B192-461F-AA3E-44007A33A3DF}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 {E005997D-B192-461F-AA3E-44007A33A3DF}.Debug|Mixed Platforms.Build.0 = Debug|x86 + {E005997D-B192-461F-AA3E-44007A33A3DF}.Debug|x64.ActiveCfg = Debug|x64 + {E005997D-B192-461F-AA3E-44007A33A3DF}.Debug|x64.Build.0 = Debug|x64 {E005997D-B192-461F-AA3E-44007A33A3DF}.Debug|x86.ActiveCfg = Debug|x86 {E005997D-B192-461F-AA3E-44007A33A3DF}.Debug|x86.Build.0 = Debug|x86 {E005997D-B192-461F-AA3E-44007A33A3DF}.Release|Any CPU.ActiveCfg = Release|x86 {E005997D-B192-461F-AA3E-44007A33A3DF}.Release|Mixed Platforms.ActiveCfg = Release|x86 {E005997D-B192-461F-AA3E-44007A33A3DF}.Release|Mixed Platforms.Build.0 = Release|x86 + {E005997D-B192-461F-AA3E-44007A33A3DF}.Release|x64.ActiveCfg = Release|x64 + {E005997D-B192-461F-AA3E-44007A33A3DF}.Release|x64.Build.0 = Release|x64 {E005997D-B192-461F-AA3E-44007A33A3DF}.Release|x86.ActiveCfg = Release|x64 {E005997D-B192-461F-AA3E-44007A33A3DF}.Release|x86.Build.0 = Release|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Mixed Platforms.ActiveCfg = Debug|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|Mixed Platforms.Build.0 = Debug|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|x64.ActiveCfg = Debug|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|x64.Build.0 = Debug|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|x86.ActiveCfg = Debug|x86 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Debug|x86.Build.0 = Debug|x86 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Any CPU.Build.0 = Release|Any CPU + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Mixed Platforms.ActiveCfg = Release|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|Mixed Platforms.Build.0 = Release|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|x64.ActiveCfg = Release|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|x64.Build.0 = Release|x64 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|x86.ActiveCfg = Release|x86 + {BD5E6BFE-E837-4A35-BCA9-39667D873A20}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs index 466d06b..4df22cc 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs @@ -310,7 +310,32 @@ } if (!isMerged) { - var thisWidth = ((XLFont)c.Style.Font).GetWidth(c.GetFormattedString()); + Int32 textRotation = c.Style.Alignment.TextRotation; + var f = (XLFont)c.Style.Font; + Double thisWidth; + if (textRotation == 0) + { + thisWidth = f.GetWidth(c.GetFormattedString()); + } + else + { + if (textRotation == 255) + { + thisWidth = f.GetWidth("X"); + } + else + { + Int32 rotation; + if (textRotation == 90 || textRotation == 180 || textRotation == 255) + rotation = 90; + else + rotation = textRotation % 90; + + Double r = DegreeToRadian(rotation); + thisWidth = f.GetWidth(c.GetFormattedString()) * Math.Cos(r) + Math.Sin(r) * f.GetWidth("X"); + } + } + if (thisWidth > colMaxWidth) colMaxWidth = thisWidth; } @@ -324,6 +349,11 @@ return this; } + private double DegreeToRadian(double angle) + { + return Math.PI * angle / 180.0; + } + public IXLColumn AdjustToContents(Double minWidth, Double maxWidth) { return AdjustToContents(1, XLWorksheet.MaxNumberOfRows, minWidth, maxWidth); @@ -349,7 +379,32 @@ } if (!isMerged) { - var thisWidth = ((XLFont)c.Style.Font).GetWidth(c.GetFormattedString()); + Int32 textRotation = c.Style.Alignment.TextRotation; + var f = (XLFont)c.Style.Font; + Double thisWidth; + if (textRotation == 0) + { + thisWidth = f.GetWidth(c.GetFormattedString()); + } + else + { + if (textRotation == 255) + { + thisWidth = f.GetWidth("X"); + } + else + { + Int32 rotation; + if (textRotation == 90 || textRotation == 180 || textRotation == 255) + rotation = 90; + else + rotation = textRotation % 90; + + Double r = DegreeToRadian(rotation); + thisWidth = f.GetWidth(c.GetFormattedString()) * Math.Cos(r) + Math.Sin(r) * f.GetWidth("X"); + } + } + if (thisWidth >= maxWidth) { colMaxWidth = maxWidth; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumns.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumns.cs index c1e35e1..d05f34b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumns.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumns.cs @@ -199,6 +199,10 @@ { columns.ForEach(c => c.Collapse()); } + public void CollapseOnly() + { + columns.ForEach(c => c.Collapsed = true ); + } public void Expand() { columns.ForEach(c => c.Expand()); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs index 3ffde8c..b0b45c4 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs @@ -211,7 +211,38 @@ } if (!isMerged) { - var thisHeight = ((XLFont)c.Style.Font).GetHeight(); + //var thisHeight = ((XLFont)c.Style.Font).GetHeight(); + + Int32 textRotation = c.Style.Alignment.TextRotation; + var f = (XLFont)c.Style.Font; + Double thisHeight; + if (textRotation == 0) + { + thisHeight = f.GetHeight(); + } + else + { + if (textRotation == 255) + { + thisHeight = f.GetHeight() * c.GetFormattedString().Length; + } + else + { + Int32 rotation; + if (textRotation == 90 || textRotation == 180 || textRotation == 255) + rotation = 90; + else + rotation = textRotation % 90; + + Double r = DegreeToRadian(rotation); + Double b = f.GetHeight(); + Double m = f.GetHeight() * c.GetFormattedString().Length; + Double t = m - b; + thisHeight = (rotation / 90) * t; + + } + } + if (thisHeight > maxHeight) maxHeight = thisHeight; } @@ -224,6 +255,11 @@ return this; } + private double DegreeToRadian(double angle) + { + return Math.PI * angle / 180.0; + } + public IXLRow AdjustToContents(Double minHeight, Double maxHeight) { return AdjustToContents(1, XLWorksheet.MaxNumberOfColumns, minHeight, maxHeight); @@ -249,7 +285,36 @@ } if (!isMerged) { - var thisHeight = ((XLFont)c.Style.Font).GetHeight(); + Int32 textRotation = c.Style.Alignment.TextRotation; + var f = (XLFont)c.Style.Font; + Double thisHeight; + if (textRotation == 0) + { + thisHeight = f.GetHeight(); + } + else + { + if (textRotation == 255) + { + thisHeight = f.GetHeight() * c.GetFormattedString().Length; + } + else + { + Int32 rotation; + if (textRotation == 90 || textRotation == 180 || textRotation == 255) + rotation = 90; + else + rotation = textRotation % 90; + + Double r = DegreeToRadian(rotation); + Double b = f.GetHeight(); + Double m = f.GetHeight() * c.GetFormattedString().Length; + Double t = m - b; + thisHeight = (rotation / 90) * t; + + } + } + if (thisHeight >= maxHeight) { rowMaxHeight = maxHeight; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Internal.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Internal.cs index 2906e32..171f26d 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Internal.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Internal.cs @@ -8,26 +8,28 @@ { public partial class XLColor { - + internal XLColor(IXLColor defaultColor) { - if (defaultColor.ColorType == XLColorType.Theme) + XLColor dColor = (XLColor)defaultColor; + if (dColor.colorType == XLColorType.Theme) { - this.ThemeColor = defaultColor.ThemeColor; - this.ThemeTint = defaultColor.ThemeTint; + this.themeColor = dColor.themeColor; + this.themeTint = dColor.themeTint; hashCode = 7 ^ themeColor.GetHashCode() ^ themeTint.GetHashCode(); } - else if (defaultColor.ColorType == XLColorType.Indexed) + else if (dColor.ColorType == XLColorType.Indexed) { - this.Indexed = defaultColor.Indexed; + this.indexed = dColor.indexed; hashCode = 11 ^ indexed; } else { - this.Color = defaultColor.Color; + this.color = dColor.color; hashCode = 13 ^ color.GetHashCode(); } HasValue = true; + colorType = dColor.colorType; } internal XLColor() { @@ -36,29 +38,33 @@ } internal XLColor(Color color) { - Color = color; + this.color = color; hashCode = 13 ^ this.color.GetHashCode(); HasValue = true; + colorType = XLColorType.Color; } internal XLColor(Int32 index) { - Indexed = index; + this.indexed = index; hashCode = 11 ^ indexed; HasValue = true; + colorType = XLColorType.Indexed; } internal XLColor(XLThemeColor themeColor) { - ThemeColor = themeColor; - ThemeTint = 1; + this.themeColor = themeColor; + this.themeTint = 1; hashCode = 7 ^ this.themeColor.GetHashCode() ^ themeTint.GetHashCode(); HasValue = true; + colorType = XLColorType.Theme; } internal XLColor(XLThemeColor themeColor, Double themeTint) { - ThemeColor = themeColor; - ThemeTint = themeTint; + this.themeColor = themeColor; + this.themeTint = themeTint; hashCode = 7 ^ this.themeColor.GetHashCode() ^ this.themeTint.GetHashCode(); HasValue = true; + colorType = XLColorType.Theme; } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs index 468ca03..03b1835 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs @@ -177,13 +177,18 @@ } set { - if ( value != 255 && (value < 0 || value > 180) ) - throw new ArgumentException("TextRotation must be between 0 and 180 degrees, or 255."); + Int32 rotation = value; + + if (rotation != 255 && (rotation < -90 || rotation > 180)) + throw new ArgumentException("TextRotation must be between -90 and 180 degrees, or 255."); + + if (rotation < 0) + rotation = 90 + (rotation * -1); if (container != null && !container.UpdatingStyle) - container.Styles.ForEach(s => s.Alignment.TextRotation = value); + container.Styles.ForEach(s => s.Alignment.TextRotation = rotation); else - textRotation = value; + textRotation = rotation; } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index d1cf344..c7e57d7 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -46,6 +46,7 @@ private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet) { SetProperties(dSpreadsheet); + //var sharedStrings = dSpreadsheet.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements(); SharedStringItem[] sharedStrings = null; if (dSpreadsheet.WorkbookPart.GetPartsOfType().Count() > 0) { @@ -91,8 +92,10 @@ var workbookStylesPart = (WorkbookStylesPart)dSpreadsheet.WorkbookPart.WorkbookStylesPart; var s = (Stylesheet)workbookStylesPart.Stylesheet; - var numberingFormats = (NumberingFormats)s.NumberingFormats; - Fills fills = (Fills)s.Fills; + + NumberingFormats numberingFormats = s.NumberingFormats; + //Int32 fillCount = (Int32)s.Fills.Count.Value; + var fills = s.Fills; Borders borders = (Borders)s.Borders; Fonts fonts = (Fonts)s.Fonts; @@ -104,7 +107,7 @@ Sheet dSheet = ((Sheet)sheet); WorksheetPart worksheetPart = (WorksheetPart)dSpreadsheet.WorkbookPart.GetPartById(dSheet.Id); - + var sheetName = dSheet.Name; var ws = (XLWorksheet)Worksheets.Add(sheetName); @@ -114,72 +117,79 @@ if (dSheet.State != null) ws.Visibility = sheetStateValues.Single(p => p.Value == dSheet.State).Key; - var sheetFormatProperties = (SheetFormatProperties)worksheetPart.Worksheet.Descendants().First(); - if (sheetFormatProperties.DefaultRowHeight != null) - ws.RowHeight = sheetFormatProperties.DefaultRowHeight; - - ws.RowHeightChanged = (sheetFormatProperties.CustomHeight != null && sheetFormatProperties.CustomHeight.Value); - - if (sheetFormatProperties.DefaultColumnWidth != null) - ws.ColumnWidth = sheetFormatProperties.DefaultColumnWidth; - - LoadSheetViews(worksheetPart, ws); - - foreach (var mCell in worksheetPart.Worksheet.Descendants()) + var sheetFormatProperties = worksheetPart.Worksheet.SheetFormatProperties; + if (sheetFormatProperties != null) { - var mergeCell = (MergeCell)mCell; - ws.Range(mergeCell.Reference).Merge(); + if (sheetFormatProperties.DefaultRowHeight != null) + ws.RowHeight = sheetFormatProperties.DefaultRowHeight; + + ws.RowHeightChanged = (sheetFormatProperties.CustomHeight != null && sheetFormatProperties.CustomHeight.Value); + + if (sheetFormatProperties.DefaultColumnWidth != null) + ws.ColumnWidth = sheetFormatProperties.DefaultColumnWidth; + } + LoadSheetViews(worksheetPart, ws); + var mergedCells = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (mergedCells != null) + { + foreach (MergeCell mergeCell in mergedCells.Elements()) + { + ws.Range(mergeCell.Reference).Merge(); + } } #region LoadColumns - Column wsDefaultColumn = null; - var defaultColumns = worksheetPart.Worksheet.Descendants().Where(c => c.Max == XLWorksheet.MaxNumberOfColumns); - if (defaultColumns.Count() > 0) - wsDefaultColumn = defaultColumns.Single(); - - if (wsDefaultColumn != null && wsDefaultColumn.Width != null) ws.ColumnWidth = wsDefaultColumn.Width - COLUMN_WIDTH_OFFSET; - - Int32 styleIndexDefault = wsDefaultColumn != null && wsDefaultColumn.Style != null ? Int32.Parse(wsDefaultColumn.Style.InnerText) : -1; - if (styleIndexDefault >= 0) + var columns = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (columns != null) { - ApplyStyle(ws, styleIndexDefault, s, fills, borders, fonts, numberingFormats); - } + + var wsDefaultColumn = columns.Elements().Where(c => c.Max == XLWorksheet.MaxNumberOfColumns).FirstOrDefault(); + + if (wsDefaultColumn != null && wsDefaultColumn.Width != null) ws.ColumnWidth = wsDefaultColumn.Width - COLUMN_WIDTH_OFFSET; - foreach (var col in worksheetPart.Worksheet.Descendants()) - { - //IXLStylized toApply; - if (col.Max != XLWorksheet.MaxNumberOfColumns) + Int32 styleIndexDefault = wsDefaultColumn != null && wsDefaultColumn.Style != null ? Int32.Parse(wsDefaultColumn.Style.InnerText) : -1; + if (styleIndexDefault >= 0) { - var xlColumns = (XLColumns)ws.Columns(col.Min, col.Max); - if (col.Width != null) - xlColumns.Width = col.Width - COLUMN_WIDTH_OFFSET; - else - xlColumns.Width = ws.ColumnWidth; + ApplyStyle(ws, styleIndexDefault, s, fills, borders, fonts, numberingFormats); + } - if (col.Hidden != null && col.Hidden) - xlColumns.Hide(); - - if (col.Collapsed != null && col.Collapsed) - xlColumns.Collapse(); - - if (col.OutlineLevel != null) - xlColumns.ForEach(c => c.OutlineLevel = col.OutlineLevel); - - Int32 styleIndex = col.Style != null ? Int32.Parse(col.Style.InnerText) : -1; - if (styleIndex > 0) + foreach (var col in columns.Elements()) + { + //IXLStylized toApply; + if (col.Max != XLWorksheet.MaxNumberOfColumns) { - ApplyStyle(xlColumns, styleIndex, s, fills, borders, fonts, numberingFormats); - } - else - { - xlColumns.Style = DefaultStyle; + var xlColumns = (XLColumns)ws.Columns(col.Min, col.Max); + if (col.Width != null) + xlColumns.Width = col.Width - COLUMN_WIDTH_OFFSET; + else + xlColumns.Width = ws.ColumnWidth; + + if (col.Hidden != null && col.Hidden) + xlColumns.Hide(); + + if (col.Collapsed != null && col.Collapsed) + xlColumns.CollapseOnly(); + + if (col.OutlineLevel != null) + xlColumns.ForEach(c => c.OutlineLevel = col.OutlineLevel); + + Int32 styleIndex = col.Style != null ? Int32.Parse(col.Style.InnerText) : -1; + if (styleIndex > 0) + { + ApplyStyle(xlColumns, styleIndex, s, fills, borders, fonts, numberingFormats); + } + else + { + xlColumns.Style = DefaultStyle; + } } } } #endregion #region LoadRows - foreach (var row in worksheetPart.Worksheet.Descendants()) //.Where(r => r.CustomFormat != null && r.CustomFormat).Select(r => r)) + var sheetData = worksheetPart.Worksheet.GetFirstChild(); + foreach (var row in sheetData.Elements()) //.Where(r => r.CustomFormat != null && r.CustomFormat).Select(r => r)) { var xlRow = (XLRow)ws.Row((Int32)row.RowIndex.Value, false); if (row.Height != null) @@ -191,7 +201,7 @@ xlRow.Hide(); if (row.Collapsed != null && row.Collapsed) - xlRow.Collapse(); + xlRow.Collapsed = true; if (row.OutlineLevel != null && row.OutlineLevel > 0) xlRow.OutlineLevel = row.OutlineLevel; @@ -211,46 +221,26 @@ //xlRow.Style = ws.Style; } } - } - #endregion - #region LoadCells - foreach (var cell in worksheetPart.Worksheet.Descendants()) - { - var dCell = (Cell)cell; - Int32 styleIndex = dCell.StyleIndex != null ? Int32.Parse(dCell.StyleIndex.InnerText) : 0; - var xlCell = (XLCell)ws.Cell(dCell.CellReference); + #region LoadCells + Dictionary styleList = new Dictionary(); + styleList.Add(0, DefaultStyle); + foreach (var cell in row.Elements()) + { + var dCell = (Cell)cell; + Int32 styleIndex = dCell.StyleIndex != null ? Int32.Parse(dCell.StyleIndex.InnerText) : 0; + var xlCell = (XLCell)ws.Cell(dCell.CellReference); - if (styleIndex > 0) - { - ApplyStyle(xlCell, styleIndex, s, fills, borders, fonts, numberingFormats); - } - else - { - xlCell.Style = DefaultStyle; - } - - if (cell.CellFormula != null && cell.CellFormula.SharedIndex != null && cell.CellFormula.Reference != null) - { - String formula; - if (cell.CellFormula.FormulaType != null && cell.CellFormula.FormulaType == CellFormulaValues.Array) - formula = "{" + cell.CellFormula.Text + "}"; + if (styleList.ContainsKey(styleIndex)) + xlCell.Style = styleList[styleIndex]; else - formula = cell.CellFormula.Text; - - xlCell.FormulaA1 = formula; - sharedFormulasR1C1.Add(cell.CellFormula.SharedIndex.Value, xlCell.FormulaR1C1); - - if (dCell.CellValue != null) - xlCell.ValueCached = dCell.CellValue.Text; - } - else if (dCell.CellFormula != null) - { - if (dCell.CellFormula.SharedIndex != null) { - xlCell.FormulaR1C1 = sharedFormulasR1C1[dCell.CellFormula.SharedIndex.Value]; + ApplyStyle(xlCell, styleIndex, s, fills, borders, fonts, numberingFormats); + styleList.Add(styleIndex, xlCell.Style); } - else + + + if (cell.CellFormula != null && cell.CellFormula.SharedIndex != null && cell.CellFormula.Reference != null) { String formula; if (cell.CellFormula.FormulaType != null && cell.CellFormula.FormulaType == CellFormulaValues.Array) @@ -259,77 +249,101 @@ formula = cell.CellFormula.Text; xlCell.FormulaA1 = formula; - } + sharedFormulasR1C1.Add(cell.CellFormula.SharedIndex.Value, xlCell.FormulaR1C1); - if (dCell.CellValue != null) - xlCell.ValueCached = dCell.CellValue.Text; - } - else if (dCell.DataType != null) - { - if (dCell.DataType == CellValues.InlineString) - { - xlCell.Value = dCell.InlineString.Text.Text; - xlCell.DataType = XLCellValues.Text; - xlCell.ShareString = false; - } - else if (dCell.DataType == CellValues.SharedString) - { if (dCell.CellValue != null) + xlCell.ValueCached = dCell.CellValue.Text; + } + else if (dCell.CellFormula != null) + { + if (dCell.CellFormula.SharedIndex != null) { - if (!StringExtensions.IsNullOrWhiteSpace(dCell.CellValue.Text)) - xlCell.cellValue = sharedStrings[Int32.Parse(dCell.CellValue.Text)].InnerText; + xlCell.FormulaR1C1 = sharedFormulasR1C1[dCell.CellFormula.SharedIndex.Value]; + } + else + { + String formula; + if (cell.CellFormula.FormulaType != null && cell.CellFormula.FormulaType == CellFormulaValues.Array) + formula = "{" + cell.CellFormula.Text + "}"; else - xlCell.cellValue = dCell.CellValue.Text; + formula = cell.CellFormula.Text; + + xlCell.FormulaA1 = formula; } - else + + if (dCell.CellValue != null) + xlCell.ValueCached = dCell.CellValue.Text; + } + else if (dCell.DataType != null) + { + if (dCell.DataType == CellValues.InlineString) { - xlCell.cellValue = String.Empty; - } - xlCell.DataType = XLCellValues.Text; - } - else if (dCell.DataType == CellValues.Date) - { - xlCell.Value = DateTime.FromOADate(Double.Parse(dCell.CellValue.Text, CultureInfo.InvariantCulture)); - } - else if (dCell.DataType == CellValues.Boolean) - { - xlCell.Value = (dCell.CellValue.Text == "1"); - } - else if (dCell.DataType == CellValues.Number) - { - xlCell.Value = Double.Parse(dCell.CellValue.Text, CultureInfo.InvariantCulture); - var numberFormatId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).NumberFormatId; - if (numberFormatId == 46U) - xlCell.DataType = XLCellValues.TimeSpan; - else - xlCell.DataType = XLCellValues.Number; - } - } - else if (dCell.CellValue != null) - { - var numberFormatId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).NumberFormatId; - Double val = Double.Parse(dCell.CellValue.Text, CultureInfo.InvariantCulture); - xlCell.Value = val; - if (s.NumberingFormats != null && s.NumberingFormats.Any(nf => ((NumberingFormat)nf).NumberFormatId.Value == numberFormatId)) - xlCell.Style.NumberFormat.Format = - ((NumberingFormat)s.NumberingFormats.Where(nf => ((NumberingFormat)nf).NumberFormatId.Value == numberFormatId).Single()).FormatCode.Value; - else - xlCell.Style.NumberFormat.NumberFormatId = Int32.Parse(numberFormatId); - - - if (!StringExtensions.IsNullOrWhiteSpace(xlCell.Style.NumberFormat.Format)) - xlCell.DataType = GetDataTypeFromFormat(xlCell.Style.NumberFormat.Format); - else - if ((numberFormatId >= 14 && numberFormatId <= 22) || (numberFormatId >= 45 && numberFormatId <= 47)) - xlCell.DataType = XLCellValues.DateTime; - else if (numberFormatId == 49) + xlCell.Value = dCell.InlineString.Text.Text; xlCell.DataType = XLCellValues.Text; + xlCell.ShareString = false; + } + else if (dCell.DataType == CellValues.SharedString) + { + if (dCell.CellValue != null) + { + if (!StringExtensions.IsNullOrWhiteSpace(dCell.CellValue.Text)) + xlCell.cellValue = sharedStrings[Int32.Parse(dCell.CellValue.Text)].InnerText; + else + xlCell.cellValue = dCell.CellValue.Text; + } + else + { + xlCell.cellValue = String.Empty; + } + xlCell.DataType = XLCellValues.Text; + } + else if (dCell.DataType == CellValues.Date) + { + xlCell.Value = DateTime.FromOADate(Double.Parse(dCell.CellValue.Text, CultureInfo.InvariantCulture)); + } + else if (dCell.DataType == CellValues.Boolean) + { + xlCell.Value = (dCell.CellValue.Text == "1"); + } + else if (dCell.DataType == CellValues.Number) + { + xlCell.Value = Double.Parse(dCell.CellValue.Text, CultureInfo.InvariantCulture); + var numberFormatId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).NumberFormatId; + if (numberFormatId == 46U) + xlCell.DataType = XLCellValues.TimeSpan; + else + xlCell.DataType = XLCellValues.Number; + } + } + else if (dCell.CellValue != null) + { + var numberFormatId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).NumberFormatId; + Double val = Double.Parse(dCell.CellValue.Text, CultureInfo.InvariantCulture); + xlCell.Value = val; + if (s.NumberingFormats != null && s.NumberingFormats.Any(nf => ((NumberingFormat)nf).NumberFormatId.Value == numberFormatId)) + xlCell.Style.NumberFormat.Format = + ((NumberingFormat)s.NumberingFormats.Where(nf => ((NumberingFormat)nf).NumberFormatId.Value == numberFormatId).Single()).FormatCode.Value; else - xlCell.DataType = XLCellValues.Number; + xlCell.Style.NumberFormat.NumberFormatId = Int32.Parse(numberFormatId); + + + if (!StringExtensions.IsNullOrWhiteSpace(xlCell.Style.NumberFormat.Format)) + xlCell.DataType = GetDataTypeFromFormat(xlCell.Style.NumberFormat.Format); + else + if ((numberFormatId >= 14 && numberFormatId <= 22) || (numberFormatId >= 45 && numberFormatId <= 47)) + xlCell.DataType = XLCellValues.DateTime; + else if (numberFormatId == 49) + xlCell.DataType = XLCellValues.Text; + else + xlCell.DataType = XLCellValues.Number; + } } + #endregion } #endregion + + #region LoadTables foreach (var tablePart in worksheetPart.TableDefinitionParts) { @@ -370,76 +384,79 @@ } #endregion - LoadAutoFilter(worksheetPart, ws); + LoadAutoFilter(worksheetPart, ws);; - LoadSheetProtection(worksheetPart, ws); + LoadSheetProtection(worksheetPart, ws);; - LoadDataValidations(worksheetPart, ws); + LoadDataValidations(worksheetPart, ws);; LoadHyperlinks(worksheetPart, ws); - LoadPrintOptions(worksheetPart, ws); + LoadPrintOptions(worksheetPart, ws);; - LoadPageMargins(worksheetPart, ws); + LoadPageMargins(worksheetPart, ws);; - LoadPageSetup(worksheetPart, ws); + LoadPageSetup(worksheetPart, ws);; - LoadHeaderFooter(worksheetPart, ws); + LoadHeaderFooter(worksheetPart, ws);; - LoadSheetProperties(worksheetPart, ws); + LoadSheetProperties(worksheetPart, ws);; - LoadRowBreaks(worksheetPart, ws); + LoadRowBreaks(worksheetPart, ws);; - LoadColumnBreaks(worksheetPart, ws); + LoadColumnBreaks(worksheetPart, ws);; } var workbook = (Workbook)dSpreadsheet.WorkbookPart.Workbook; - foreach (var definedName in workbook.Descendants()) + if (workbook.DefinedNames != null) { - var name = definedName.Name; - if (name == "_xlnm.Print_Area") + foreach (DefinedName definedName in workbook.DefinedNames) { - foreach (var area in definedName.Text.Split(',')) + var name = definedName.Name; + if (name == "_xlnm.Print_Area") { - var sections = area.Trim().Split('!'); - var sheetName = sections[0].Replace("\'", ""); - var sheetArea = sections[1]; - if (!sheetArea.Equals("#REF")) - Worksheets.Worksheet(sheetName).PageSetup.PrintAreas.Add(sheetArea); - } - } - else if (name == "_xlnm.Print_Titles") - { - var areas = definedName.Text.Split(','); - - var colSections = areas[0].Trim().Split('!'); - var sheetNameCol = colSections[0].Replace("\'", ""); - var sheetAreaCol = colSections[1]; - if (!sheetAreaCol.Equals("#REF")) - Worksheets.Worksheet(sheetNameCol).PageSetup.SetColumnsToRepeatAtLeft(sheetAreaCol); - - var rowSections = areas[1].Split('!'); - var sheetNameRow = rowSections[0].Replace("\'", ""); - var sheetAreaRow = rowSections[1]; - if (!sheetAreaRow.Equals("#REF")) - Worksheets.Worksheet(sheetNameRow).PageSetup.SetRowsToRepeatAtTop(sheetAreaRow); - } - else - { - - var text = definedName.Text; - - if (!text.Equals("#REF")) - { - var localSheetId = definedName.LocalSheetId; - var comment = definedName.Comment; - if (localSheetId == null) + foreach (var area in definedName.Text.Split(',')) { - NamedRanges.Add(name, text, comment); + var sections = area.Trim().Split('!'); + var sheetName = sections[0].Replace("\'", ""); + var sheetArea = sections[1]; + if (!sheetArea.Equals("#REF")) + Worksheets.Worksheet(sheetName).PageSetup.PrintAreas.Add(sheetArea); } - else + } + else if (name == "_xlnm.Print_Titles") + { + var areas = definedName.Text.Split(','); + + var colSections = areas[0].Trim().Split('!'); + var sheetNameCol = colSections[0].Replace("\'", ""); + var sheetAreaCol = colSections[1]; + if (!sheetAreaCol.Equals("#REF")) + Worksheets.Worksheet(sheetNameCol).PageSetup.SetColumnsToRepeatAtLeft(sheetAreaCol); + + var rowSections = areas[1].Split('!'); + var sheetNameRow = rowSections[0].Replace("\'", ""); + var sheetAreaRow = rowSections[1]; + if (!sheetAreaRow.Equals("#REF")) + Worksheets.Worksheet(sheetNameRow).PageSetup.SetRowsToRepeatAtTop(sheetAreaRow); + } + else + { + + var text = definedName.Text; + + if (!text.Equals("#REF")) { - Worksheet(Int32.Parse(localSheetId) + 1).NamedRanges.Add(name, text, comment); + var localSheetId = definedName.LocalSheetId; + var comment = definedName.Comment; + if (localSheetId == null) + { + NamedRanges.Add(name, text, comment); + } + else + { + Worksheet(Int32.Parse(localSheetId) + 1).NamedRanges.Add(name, text, comment); + } } } } @@ -465,45 +482,40 @@ private void LoadAutoFilter(WorksheetPart worksheetPart, XLWorksheet ws) { - var autoFilterQuery = worksheetPart.Worksheet.Descendants(); - if (autoFilterQuery.Count() > 0) - { - var af = (AutoFilter)autoFilterQuery.First(); + AutoFilter af = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (af != null) ws.Range(af.Reference.Value).SetAutoFilter(); - } } private void LoadSheetProtection(WorksheetPart worksheetPart, XLWorksheet ws) { - var sheetProtectionQuery = worksheetPart.Worksheet.Descendants(); - if (sheetProtectionQuery.Count() > 0) + var sp = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (sp != null) { - var sp = (SheetProtection)sheetProtectionQuery.First(); if (sp.Sheet != null) ws.Protection.Protected = sp.Sheet.Value; if (sp.Password != null) (ws.Protection as XLSheetProtection).PasswordHash = sp.Password.Value; - if (sp.FormatCells != null) ws.Protection.FormatCells = sp.FormatCells.Value; - if (sp.FormatColumns != null) ws.Protection.FormatColumns = sp.FormatColumns.Value; - if (sp.FormatRows != null) ws.Protection.FormatRows = sp.FormatRows.Value; - if (sp.InsertColumns != null) ws.Protection.InsertColumns = sp.InsertColumns.Value; - if (sp.InsertHyperlinks != null) ws.Protection.InsertHyperlinks = sp.InsertHyperlinks.Value; - if (sp.InsertRows != null) ws.Protection.InsertRows = sp.InsertRows.Value; - if (sp.DeleteColumns != null) ws.Protection.DeleteColumns = sp.DeleteColumns.Value; - if (sp.DeleteRows != null) ws.Protection.DeleteRows = sp.DeleteRows.Value; - if (sp.AutoFilter != null) ws.Protection.AutoFilter = sp.AutoFilter.Value; - if (sp.PivotTables != null) ws.Protection.PivotTables = sp.PivotTables.Value; - if (sp.Sort != null) ws.Protection.Sort = sp.Sort.Value; - if (sp.SelectLockedCells != null) ws.Protection.SelectLockedCells = !sp.SelectLockedCells.Value; - if (sp.SelectUnlockedCells != null) ws.Protection.SelectUnlockedCells = !sp.SelectUnlockedCells.Value; + if (sp.FormatCells != null) ws.Protection.FormatCells = sp.FormatCells.Value; + if (sp.FormatColumns != null) ws.Protection.FormatColumns = sp.FormatColumns.Value; + if (sp.FormatRows != null) ws.Protection.FormatRows = sp.FormatRows.Value; + if (sp.InsertColumns != null) ws.Protection.InsertColumns = sp.InsertColumns.Value; + if (sp.InsertHyperlinks != null) ws.Protection.InsertHyperlinks = sp.InsertHyperlinks.Value; + if (sp.InsertRows != null) ws.Protection.InsertRows = sp.InsertRows.Value; + if (sp.DeleteColumns != null) ws.Protection.DeleteColumns = sp.DeleteColumns.Value; + if (sp.DeleteRows != null) ws.Protection.DeleteRows = sp.DeleteRows.Value; + if (sp.AutoFilter != null) ws.Protection.AutoFilter = sp.AutoFilter.Value; + if (sp.PivotTables != null) ws.Protection.PivotTables = sp.PivotTables.Value; + if (sp.Sort != null) ws.Protection.Sort = sp.Sort.Value; + if (sp.SelectLockedCells != null) ws.Protection.SelectLockedCells = !sp.SelectLockedCells.Value; + if (sp.SelectUnlockedCells != null) ws.Protection.SelectUnlockedCells = !sp.SelectUnlockedCells.Value; } } private void LoadDataValidations(WorksheetPart worksheetPart, XLWorksheet ws) { - var dataValidationList = worksheetPart.Worksheet.Descendants(); - if (dataValidationList.Count() > 0) + var dataValidations = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (dataValidations != null) { - var dataValidations = (DataValidations)dataValidationList.First(); - foreach (var dvs in dataValidations.Descendants()) + foreach (var dvs in dataValidations.Elements()) { foreach (String rangeAddress in dvs.SequenceOfReferences.InnerText.Split(' ')) { @@ -531,12 +543,11 @@ var hyperlinkDictionary = new Dictionary(); if (worksheetPart.HyperlinkRelationships != null) hyperlinkDictionary = worksheetPart.HyperlinkRelationships.ToDictionary(hr => hr.Id, hr => hr.Uri); - - var hyperlinkList = worksheetPart.Worksheet.Descendants(); - if (hyperlinkList.Count() > 0) + + var hyperlinks = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (hyperlinks != null) { - var hyperlinks = (Hyperlinks)hyperlinkList.First(); - foreach (var hl in hyperlinks.Descendants()) + foreach (var hl in hyperlinks.Elements()) { if (!hl.Reference.Value.Equals("#REF")) { @@ -558,11 +569,18 @@ private void LoadColumnBreaks(WorksheetPart worksheetPart, XLWorksheet ws) { - var columnBreaksList = worksheetPart.Worksheet.Descendants(); - if (columnBreaksList.Count() > 0) + var rWS = worksheetPart.Worksheet; + var bs = rWS.Elements(); + ColumnBreaks columnBreaks = bs.FirstOrDefault(); + //try + //{ + // columnBreaks = bs[0]; + //} + //catch { } + + if (columnBreaks != null) { - var columnBreaks = (ColumnBreaks)columnBreaksList.First(); - foreach (var columnBreak in columnBreaks.Descendants()) + foreach (var columnBreak in columnBreaks.Elements()) { if (columnBreak.Id != null) ws.PageSetup.ColumnBreaks.Add(Int32.Parse(columnBreak.Id.InnerText)); @@ -572,11 +590,10 @@ private void LoadRowBreaks(WorksheetPart worksheetPart, XLWorksheet ws) { - var rowBreaksList = worksheetPart.Worksheet.Descendants(); - if (rowBreaksList.Count() > 0) + var rowBreaks = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (rowBreaks != null) { - var rowBreaks = (RowBreaks)rowBreaksList.First(); - foreach (var rowBreak in rowBreaks.Descendants()) + foreach (var rowBreak in rowBreaks.Elements()) { ws.PageSetup.RowBreaks.Add(Int32.Parse(rowBreak.Id.InnerText)); } @@ -585,10 +602,9 @@ private void LoadSheetProperties(WorksheetPart worksheetPart, XLWorksheet ws) { - var sheetProperties = worksheetPart.Worksheet.Descendants(); - if (sheetProperties.Count() > 0) + var sheetProperty = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (sheetProperty != null) { - var sheetProperty = (SheetProperties)sheetProperties.First(); if (sheetProperty.TabColor != null) ws.TabColor = GetColor(sheetProperty.TabColor); @@ -611,10 +627,9 @@ private void LoadHeaderFooter(WorksheetPart worksheetPart, XLWorksheet ws) { - var headerFooters = worksheetPart.Worksheet.Descendants(); - if (headerFooters.Count() > 0) + var headerFooter = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (headerFooter != null) { - var headerFooter = (HeaderFooter)headerFooters.First(); if (headerFooter.AlignWithMargins != null) ws.PageSetup.AlignHFWithMargins = headerFooter.AlignWithMargins; if (headerFooter.ScaleWithDoc != null) @@ -647,10 +662,9 @@ private void LoadPageSetup(WorksheetPart worksheetPart, XLWorksheet ws) { - var pageSetupQuery = worksheetPart.Worksheet.Descendants(); - if (pageSetupQuery.Count() > 0) + var pageSetup = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (pageSetup != null) { - var pageSetup = (PageSetup)pageSetupQuery.First(); if (pageSetup.PaperSize != null) ws.PageSetup.PaperSize = (XLPaperSize)Int32.Parse(pageSetup.PaperSize.InnerText); if (pageSetup.Scale != null) @@ -684,10 +698,9 @@ private void LoadPageMargins(WorksheetPart worksheetPart, XLWorksheet ws) { - var pageMarginsQuery = worksheetPart.Worksheet.Descendants(); - if (pageMarginsQuery.Count() > 0) + var pageMargins = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (pageMargins != null) { - var pageMargins = (PageMargins)pageMarginsQuery.First(); if (pageMargins.Bottom != null) ws.PageSetup.Margins.Bottom = pageMargins.Bottom; if (pageMargins.Footer != null) @@ -705,10 +718,9 @@ private void LoadPrintOptions(WorksheetPart worksheetPart, XLWorksheet ws) { - var printOptionsQuery = worksheetPart.Worksheet.Descendants(); - if (printOptionsQuery.Count() > 0) + var printOptions = worksheetPart.Worksheet.Elements().FirstOrDefault(); + if (printOptions != null) { - var printOptions = (PrintOptions)printOptionsQuery.First(); if (printOptions.GridLines != null) ws.PageSetup.ShowGridlines = printOptions.GridLines; if (printOptions.HorizontalCentered != null) @@ -722,26 +734,30 @@ private void LoadSheetViews(WorksheetPart worksheetPart, XLWorksheet ws) { - var sheetView = (SheetView)worksheetPart.Worksheet.Descendants().FirstOrDefault(); - if (sheetView != null) + SheetViews sheetViews = worksheetPart.Worksheet.SheetViews; + if (sheetViews != null) { - if (sheetView.ShowFormulas != null) ws.ShowFormulas = sheetView.ShowFormulas.Value; - if (sheetView.ShowGridLines != null) ws.ShowGridLines = sheetView.ShowGridLines.Value; - if (sheetView.ShowOutlineSymbols != null) ws.ShowOutlineSymbols = sheetView.ShowOutlineSymbols.Value; - if (sheetView.ShowRowColHeaders != null) ws.ShowRowColHeaders = sheetView.ShowRowColHeaders.Value; - if (sheetView.ShowRuler != null) ws.ShowRuler = sheetView.ShowRuler.Value; - if (sheetView.ShowWhiteSpace != null) ws.ShowWhiteSpace = sheetView.ShowWhiteSpace.Value; - if (sheetView.ShowZeros != null) ws.ShowZeros = sheetView.ShowZeros.Value; - - var pane = (Pane)sheetView.Descendants().FirstOrDefault(); - if (pane != null) + SheetView sheetView = sheetViews.Elements().FirstOrDefault(); + if (sheetView != null) { - if (pane.State != null && (pane.State == PaneStateValues.FrozenSplit || pane.State == PaneStateValues.Frozen)) + if (sheetView.ShowFormulas != null) ws.ShowFormulas = sheetView.ShowFormulas.Value; + if (sheetView.ShowGridLines != null) ws.ShowGridLines = sheetView.ShowGridLines.Value; + if (sheetView.ShowOutlineSymbols != null) ws.ShowOutlineSymbols = sheetView.ShowOutlineSymbols.Value; + if (sheetView.ShowRowColHeaders != null) ws.ShowRowColHeaders = sheetView.ShowRowColHeaders.Value; + if (sheetView.ShowRuler != null) ws.ShowRuler = sheetView.ShowRuler.Value; + if (sheetView.ShowWhiteSpace != null) ws.ShowWhiteSpace = sheetView.ShowWhiteSpace.Value; + if (sheetView.ShowZeros != null) ws.ShowZeros = sheetView.ShowZeros.Value; + + var pane = (Pane)sheetView.Elements().FirstOrDefault(); + if (pane != null) { - if (pane.HorizontalSplit != null) - ws.SheetView.SplitColumn = (Int32)pane.HorizontalSplit.Value; - if (pane.VerticalSplit != null) - ws.SheetView.SplitRow = (Int32)pane.VerticalSplit.Value; + if (pane.State != null && (pane.State == PaneStateValues.FrozenSplit || pane.State == PaneStateValues.Frozen)) + { + if (pane.HorizontalSplit != null) + ws.SheetView.SplitColumn = (Int32)pane.HorizontalSplit.Value; + if (pane.VerticalSplit != null) + ws.SheetView.SplitRow = (Int32)pane.VerticalSplit.Value; + } } } } @@ -803,10 +819,11 @@ private void ApplyStyle(IXLStylized xlStylized, Int32 styleIndex, Stylesheet s, Fills fills, Borders borders, Fonts fonts, NumberingFormats numberingFormats) { - var applyProtection = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).ApplyProtection; - if (applyProtection != null) + var cellFormat = (CellFormat)s.CellFormats.ElementAt(styleIndex); + + if (cellFormat.ApplyProtection != null) { - var protection = (Protection)((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).Protection; + Protection protection = cellFormat.Protection; if (protection == null) xlStylized.InnerStyle.Protection = new XLProtection(null, DefaultStyle.Protection); @@ -817,7 +834,7 @@ } } - var fillId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).FillId.Value; + var fillId = cellFormat.FillId.Value; if (fillId > 0) { var fill = (Fill)fills.ElementAt((Int32)fillId); @@ -840,7 +857,7 @@ //if (alignmentDictionary.ContainsKey(styleIndex)) //{ // var alignment = alignmentDictionary[styleIndex]; - var alignment = (Alignment)((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).Alignment; + var alignment = cellFormat.Alignment; if (alignment != null) { if (alignment.Horizontal != null) @@ -867,7 +884,7 @@ //if (borders.ContainsKey(styleIndex)) //{ // var border = borders[styleIndex]; - var borderId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).BorderId.Value; + var borderId = cellFormat.BorderId.Value; var border = (Border)borders.ElementAt((Int32)borderId); if (border != null) { @@ -926,7 +943,7 @@ //if (fonts.ContainsKey(styleIndex)) //{ // var font = fonts[styleIndex]; - var fontId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).FontId; + var fontId = cellFormat.FontId; var font = (Font)fonts.ElementAt((Int32)fontId.Value); if (font != null) { @@ -966,9 +983,8 @@ else xlStylized.InnerStyle.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Baseline; } - if (s.CellFormats != null) - { - var numberFormatId = ((CellFormat)((CellFormats)s.CellFormats).ElementAt(styleIndex)).NumberFormatId; + + var numberFormatId = cellFormat.NumberFormatId; if (numberFormatId != null) { var formatCode = String.Empty; @@ -988,7 +1004,7 @@ else xlStylized.InnerStyle.NumberFormat.NumberFormatId = (Int32)numberFormatId.Value; } - } + } private Boolean GetBoolean(BooleanPropertyType property) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index 29cbd75..c5836c7 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -1957,6 +1957,7 @@ row.Hidden = null; row.StyleIndex = null; row.CustomFormat = null; + row.Collapsed = null; if ((xlWorksheet as XLWorksheet).Internals.RowsCollection.ContainsKey(distinctRow)) { var thisRow = (xlWorksheet as XLWorksheet).Internals.RowsCollection[distinctRow]; @@ -1993,7 +1994,7 @@ if (cellsByRow.ContainsKey(distinctRow)) { - + var cellsByReference = row.Elements().ToDictionary(c => c.CellReference.Value, c => c); foreach (var opCell in cellsByRow[distinctRow] .OrderBy(c => c.Address.ColumnNumber) .Select(c => (XLCell)c)) @@ -2005,8 +2006,12 @@ //Boolean isNewCell = false; - Cell cell = row.Elements().FirstOrDefault(c => c.CellReference.Value == cellReference); - if (cell == null) + Cell cell; + if (cellsByReference.ContainsKey(cellReference)) + { + cell = cellsByReference[cellReference]; + } + else { //isNewCell = true; cell = new Cell() { CellReference = cellReference }; diff --git a/ClosedXML/ClosedXML/ClosedXML/Properties/AssemblyInfo.cs b/ClosedXML/ClosedXML/ClosedXML/Properties/AssemblyInfo.cs index 367a544..f33b8a7 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Properties/AssemblyInfo.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Properties/AssemblyInfo.cs @@ -8,9 +8,9 @@ [assembly: AssemblyTitle("ClosedXML")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyCompany("")] [assembly: AssemblyProduct("ClosedXML")] -[assembly: AssemblyCopyright("Copyright © Microsoft 2010")] +[assembly: AssemblyCopyright("Copyright © Manuel De Leon 2011")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("0.54.0.0")] +[assembly: AssemblyFileVersion("0.54.0.0")] diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj index 86f73af..07b46ed 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj @@ -160,7 +160,6 @@ ClosedXML -