diff --git a/ClosedXML/Excel/Columns/XLColumn.cs b/ClosedXML/Excel/Columns/XLColumn.cs index c713895..a5f2e92 100644 --- a/ClosedXML/Excel/Columns/XLColumn.cs +++ b/ClosedXML/Excel/Columns/XLColumn.cs @@ -127,7 +127,7 @@ { asRange.Delete(XLShiftDeletedCells.ShiftCellsLeft); } - + Worksheet.Internals.ColumnsCollection.Remove(columnNumber); var columnsToMove = new List(); columnsToMove.AddRange( @@ -433,6 +433,13 @@ } else thisWidthMax = c.Style.Font.GetWidth(c.GetFormattedString(), fontCache); + + if (c.Worksheet.AutoFilter != null + && c.Worksheet.AutoFilter.Range != null + && c.Worksheet.AutoFilter.Range.Contains(c)) + thisWidthMax += 2.7148; // Allow room for arrow icon in autofilter + + if (thisWidthMax >= maxWidth) { colMaxWidth = maxWidth; @@ -763,4 +770,4 @@ return base.IsEmpty(includeFormats); } } -} \ No newline at end of file +} diff --git a/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML_Examples/ClosedXML_Examples.csproj index 6603cc8..9bdf8de 100644 --- a/ClosedXML_Examples/ClosedXML_Examples.csproj +++ b/ClosedXML_Examples/ClosedXML_Examples.csproj @@ -71,6 +71,7 @@ + diff --git a/ClosedXML_Examples/Creating/CreateFiles.cs b/ClosedXML_Examples/Creating/CreateFiles.cs index 4a17302..4513293 100644 --- a/ClosedXML_Examples/Creating/CreateFiles.cs +++ b/ClosedXML_Examples/Creating/CreateFiles.cs @@ -52,6 +52,7 @@ new MergeMoves().Create(Path.Combine(path, "MergedMoves.xlsx")); new WorkbookProperties().Create(Path.Combine(path, "WorkbookProperties.xlsx")); new AdjustToContents().Create(Path.Combine(path, "AdjustToContents.xlsx")); + new AdjustToContentsWithAutoFilter().Create(Path.Combine(path, "AdjustToContentsWithAutoFilter.xlsx")); new HideUnhide().Create(Path.Combine(path, "HideUnhide.xlsx")); new Outline().Create(Path.Combine(path, "Outline.xlsx")); new Formulas().Create(Path.Combine(path, "Formulas.xlsx")); diff --git a/ClosedXML_Examples/Misc/AdjustToContentsWithAutoFilter.cs b/ClosedXML_Examples/Misc/AdjustToContentsWithAutoFilter.cs new file mode 100644 index 0000000..84c39ad --- /dev/null +++ b/ClosedXML_Examples/Misc/AdjustToContentsWithAutoFilter.cs @@ -0,0 +1,25 @@ +using ClosedXML.Excel; +using System; + +namespace ClosedXML_Examples.Misc +{ + public class AdjustToContentsWithAutoFilter : IXLExample + { + // Public + public void Create(String filePath) + { + var wb = new XLWorkbook(); + var ws = wb.Worksheets.Add("AutoFilter"); + ws.Cell("A1").Value = "AVeryLongColumnHeader"; + ws.Cell("A2").Value = "John"; + ws.Cell("A3").Value = "Hank"; + ws.Cell("A4").Value = "Dagny"; + + ws.RangeUsed().SetAutoFilter(); + + ws.Columns().AdjustToContents(); + + wb.SaveAs(filePath); + } + } +} diff --git a/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML_Tests/ClosedXML_Tests.csproj index cd9b3c0..db56799 100644 --- a/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -139,6 +139,7 @@ + @@ -262,4 +263,4 @@ --> - \ No newline at end of file + diff --git a/ClosedXML_Tests/Examples/MiscTests.cs b/ClosedXML_Tests/Examples/MiscTests.cs index 04a1ede..eebb483 100644 --- a/ClosedXML_Tests/Examples/MiscTests.cs +++ b/ClosedXML_Tests/Examples/MiscTests.cs @@ -26,6 +26,12 @@ } [Test] + public void AdjustToContentsWithAutoFilter() + { + TestHelper.RunTestExample(@"Misc\AdjustToContentsWithAutoFilter.xlsx"); + } + + [Test] public void AutoFilter() { TestHelper.RunTestExample(@"Misc\AutoFilter.xlsx"); diff --git a/ClosedXML_Tests/Resource/Examples/Misc/AdjustToContentsWithAutoFilter.xlsx b/ClosedXML_Tests/Resource/Examples/Misc/AdjustToContentsWithAutoFilter.xlsx new file mode 100644 index 0000000..739fe19 --- /dev/null +++ b/ClosedXML_Tests/Resource/Examples/Misc/AdjustToContentsWithAutoFilter.xlsx Binary files differ