Auto-Fit column width when exporting to excel using Format="Xlsx"

2 posts, 0 answers
  1. jlj30
    jlj30 avatar
    101 posts
    Member since:
    Jan 2011

    Posted 16 May Link to this post

    Hi,

    I'm making some progress formatting the header row of my Excel export, but I'd really like to have the columns automatically adjust to fit the width of the content.

    Here's my OnInfrastructureExporting event handler:

    protected void grdControls_InfrastructureExporting(object sender, GridInfrastructureExportingEventArgs e)
    {
        var colCount = e.ExportStructure.Tables[0].Columns.Count;
        // Set the header style for all columns
        ExportStyle headerStyle = new ExportStyle();
        headerStyle.ForeColor = Color.White;
        headerStyle.BackColor = Color.Blue;
        headerStyle.Font.Bold = true;
     
        for (var i = 1; i <= colCount; i++)
        {
            e.ExportStructure.Tables[0].Cells[i, 1].Style = headerStyle;
            e.ExportStructure.Tables[0].Columns[i].Width = 200; // Would like auto-width !!
        }
    }

     

    I've seen similar threads but they are related to export formats other than Xlsx.

    Any idea how I can accomplish auto-fit using the above handler?  Or do I need to do that elsewhere?

    Thanks for any advice.

    Jim

  2. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 19 May Link to this post

    Hello Jim,

    Thank you for contacting us.

    I am afraid there isn't an auto fit function when using the binary export formats(Biff and Xlsx). This feature is supported if you are using the other export formats. Nevertheless, I would recommend you to create a new feature request in our Ideas&Feedback portal and vote for it. Our developers will make a research and will try to implement in some of our future releases.

    I hope this information helps.

    Regards,
    Kostadin
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top