worksheet.Columns.AutoFit() not taking into account filter dropdowns

Posted by: mth on 22 July 2020, 9:33 am EST

  • Posted 22 July 2020, 9:33 am EST

    I noticed that this code:

    worksheet.Columns.AutoFit();


    Will not take into account the presence of the dropdown to fit the content so the end result is that the column header is not showing all the text. Is there a workaround to resolve this?

    Using Excel autofit will correctly take into account the presence of the dropdown. See screenshots.
  • Marked as Answer

    Replied 22 July 2020, 8:48 pm EST

    Hello Mathieu,

    AutoFit is working perfectly at my end. When using the table in the excel, the last character is overlapped by the dropdown sign.
    You can use the code snippet given below as a workaround to make it look properly:

     Workbook wb = new Workbook();
    IWorksheet sheet = wb.Worksheets[0];
    sheet.Rows[0].Value = new object[] { "TotalColumnHeader", "TotalColumnHeader" };
    ITable table = sheet.Tables.Add(sheet.Range["A1:B2"], true);
    sheet.Range["A1:B2"].Columns.AutoFit();
    for(int i=0;i<table.Columns.Count;i++)
    sheet.Columns[i].ColumnWidth = sheet.Columns[1].ColumnWidth + 2;
    wb.Save("demo.xlsx");

    If you need any other help, please let me know.

    Regards,
    Prabhat Sharma.
  • Replied 23 July 2020, 1:13 am EST

    Thank you Sir. That worked perfectly.

    Great day to you.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels