AdjustToContents() on Columns not working
See original GitHub issueRead and complete the full issue template
Do you want to request a feature or report a bug?
- Bug
- Feature
Version of ClosedXML 0.91.1 and to latest Version of DocumentFormat.OpenXml 2.7.2 and to latest
What is the current behavior?
AdjustToContents() not working
What is the expected behavior or new feature?
Columns width in sheet is Auto Adjusted by Content.
Did this work in previous versions of our tool? Which versions?
Yes/ first versions 3-4 year ago Code to reproduce problem:
public void Main()
{
var workbook = new XLWorkbook();
var worksheet_pharmacies = workbook.Worksheets.Add("PHARMACIES");
DataTable REPORT_PHARMACIES_DATA_TABLE = new DataTable();
REPORT_PHARMACIES_DATA_TABLE = sql_procedures.RETURN_PRODUCERS_REPORTS_PROCEDURE(SQL_PROCEDURES.PRODUCER_REPORT_TYPE.PHARMACIES);
//use any other data with ADO.NET DataTable
worksheet_pharmacies.Cell(1, 1).Value = "REPORT PERIOD:";
worksheet_pharmacies.Cell(1, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
worksheet_pharmacies.Cell(1, 1).Style.Font.Bold = true;
worksheet_pharmacies.Cell(1, 2).Value = REPORT_PERIOD_STRING;
worksheet_pharmacies.Cell(1, 2).Style.Font.Bold = true;
worksheet_pharmacies.Cell(2, 1).Value = "CONTRACT NAME:";
worksheet_pharmacies.Cell(2, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
worksheet_pharmacies.Cell(2, 1).Style.Font.Bold = true;
worksheet_pharmacies.Cell(2, 2).Value = contract.report_name;
worksheet_pharmacies.Cell(2, 2).Style.Font.Bold = true;
worksheet_pharmacies.Cell(3, 1).Value = "REPORT NAME:";
worksheet_pharmacies.Cell(3, 1).Style.Font.Bold = true;
worksheet_pharmacies.Cell(3, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
worksheet_pharmacies.Cell(3, 2).Value = "PHARMACIES";
worksheet_pharmacies.Cell(3, 2).Style.Font.Bold = true;
worksheet_pharmacies.Cell(4, 1).Value = "REPORT CREATED TIME:";
worksheet_pharmacies.Cell(4, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
worksheet_pharmacies.Cell(4, 1).Style.Font.Bold = true;
worksheet_pharmacies.Cell(4, 2).Value = Create_date;
worksheet_pharmacies.Cell(4, 2).Style.Font.Bold = true;
worksheet_pharmacies.Cell(4, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
var table_pharmacies = worksheet_pharmacies.Cell(6, 1).InsertTable(REPORT_PHARMACIES_DATA_TABLE);
table_pharmacies.Theme = XLTableTheme.TableStyleLight18;
table_pharmacies.ShowTotalsRow = true;
IXLRange pharmacy_range = table_pharmacies.RangeUsed();
pharmacy_range.Column(3).Cells().Style.NumberFormat.Format = "# ##0.00€";
pharmacy_range.Column(5).Cells().Style.NumberFormat.Format = "# ##0.00€";
pharmacy_range.Column(6).Cells().Style.NumberFormat.Format = "# ##0.00€";
table_pharmacies.Field("PURCHASE_QTY").TotalsRowFunction = XLTotalsRowFunction.Sum;
table_pharmacies.Field("PURCHASE_SUM").TotalsRowFunction = XLTotalsRowFunction.Sum;
table_pharmacies.Field("BONUS_TOTAL_SUM").TotalsRowFunction = XLTotalsRowFunction.Sum;
table_pharmacies.TotalsRow().Cell(4).Style.NumberFormat.Format = "0";
table_pharmacies.TotalsRow().Cell(5).Style.NumberFormat.Format = "# ##0.00€";
table_pharmacies.TotalsRow().Cell(6).Style.NumberFormat.Format = "# ##0.00€";
table_pharmacies.Field(0).TotalsRowLabel = "TOTAL SUM:";
worksheet_pharmacies.PageSetup.PageOrientation = XLPageOrientation.Portrait;
worksheet_pharmacies.PageSetup.Margins.Top = 0.5;
worksheet_pharmacies.PageSetup.Margins.Bottom = 0.3;
worksheet_pharmacies.PageSetup.Margins.Left = 0.5;
worksheet_pharmacies.PageSetup.Margins.Right = 0.3;
worksheet_pharmacies.PageSetup.Margins.Footer = 0;
worksheet_pharmacies.PageSetup.Margins.Header = 0;
worksheet_pharmacies.PageSetup.PagesWide = 1;
worksheet_pharmacies.PageSetup.PaperSize = XLPaperSize.A4Paper;
worksheet_pharmacies.PageSetup.VerticalDpi = 600;
worksheet_pharmacies.PageSetup.HorizontalDpi = 600;
foreach (var item in worksheet_pharmacies.ColumnsUsed())
{
//item.Width = 20.00;
item.AdjustToContents();// this not working so instead of AdjustToContents() I use .Width
}
}
- I attached a sample spreadsheet. (You can drag files on to this issue)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Columns().AdjustToContents() Doesn't work · Issue #1702
Setting Columns().AdjustToContents() is not doing a AutoFit Column Width operation. What is the expected behavior or new feature?
Read more >Using ClosedXML how to adjust row height to content?
Column (1).Width = 10; worksheet.Rows().AdjustToContents();. The text has been moved by words, but row height is not changed ...
Read more >[Solved]-Autofit column in ClosedXML.Excel-C#
My fix was to use the worksheet.Columns().AdjustToContents(); but I had to add it after I wrote out all of the data. Initially I...
Read more >Read Excel file in .NET 6 deployed on Linux? : r/dotnet
AdjustToContents() . It has to measure the size of the font, and the font resource wasn't available on Linux which threw a runtime...
Read more >Auto Fit Row Height and Column Width
GcExcel provides support for automatic adjustment of row height and column width based on the data present in the rows and columns.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I experience the same behaviour with the latest release. Guess there will never be a fix?
Give me code that I can run, please. You’ll have to manually populate the data table.