How do I set width to "auto" in excel export? And how do I change header style?

5 posts, 1 answers
  1. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 22 Nov 2016 Link to this post

    I can't figure out how to do the following 2 things.

    1. Set the width of the exported columns. They look ok in my grid in the WinForm, but when exported take up too much space.

    2. How do I change the style of the column headers? I would like to bold it.

    Here's my code.

    private void btnExportVendorInvoices_Click(object sender, EventArgs e)
    {
        using (SaveFileDialog dialog = new SaveFileDialog())
        {
            dialog.Filter = "Microsoft Excel (*.xlsx)|*.xlsx";
            dialog.FilterIndex = 2;
            dialog.RestoreDirectory = true;
     
            if (dialog.ShowDialog() == DialogResult.OK)
            {
     
                rgvInvoices.EnableAlternatingRowColor = true;
                var exporter = new ExportToExcelML(rgvInvoices);
                exporter.SheetMaxRows = ExcelMaxRows._1048576;
                exporter.HiddenColumnOption = HiddenOption.DoNotExport;
                exporter.ExportVisualSettings = true;
                exporter.ExcelCellFormatting += explorter_ExcelCellFormatting;
                exporter.RunExport(dialog.FileName);
                exporter.ExcelRowFormatting += exporter_ExcelRowFormatting;
     
                var dr = RadMessageBox.Show(this, "File has been saved, would you like to open it?",
                                            "File saved", MessageBoxButtons.YesNo, RadMessageIcon.Info);
                if (dr.ToString() == "Yes")
                {
                    System.Diagnostics.Process.Start(dialog.FileName);
                }
            }
        }
    }
     
     
    private void explorter_ExcelCellFormatting(object sender, ExcelCellFormattingEventArgs e)
    {
        if (e.GridCellInfo.RowInfo is GridViewDataRowInfo)
        {
            if (e.GridCellInfo.ColumnInfo.Name == "vendorFedId")
            {
                e.ExcelCellElement.Data.DataItem = string.Format("{0:00-0000000}", Convert.ToInt32(e.GridCellInfo.Value));
            }
        }
    }
  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    2951 posts

    Posted 23 Nov 2016 Link to this post

    Hi Pavel,

    Thank you for writing.

    1. The following article shows how you can set the width of the columns: Set Column Width and Row Height with ExcelML format | RadGridView.

    2. You can set the font style in the ExcelCellFormatting event:
    private void explorter_ExcelCellFormatting(object sender, ExcelCellFormattingEventArgs e)
    {
         
        if(e.GridRowInfoType == typeof(GridViewTableHeaderRowInfo))
        {
     
            e.ExcelStyleElement.FontStyle.Bold = true;
        }
        else
        {
            e.ExcelStyleElement.FontStyle.Bold = false;
        }
    }

    I hope this will be useful. Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik by Progress
    Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
  3. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 23 Nov 2016 Link to this post

    Dimitar, is there a way to have more control over the column width? I want to set different widths for different columns.
  4. Pavel
    Pavel avatar
    32 posts
    Member since:
    Sep 2016

    Posted 23 Nov 2016 in reply to Pavel Link to this post

    I hit "post" too early. I know I can loop though using a for loop, and just do "if (columnIndex == blah)..." but, I would much rather not hardcode indexes.
  5. Dimitar
    Admin
    Dimitar avatar
    2951 posts

    Posted 24 Nov 2016 Link to this post

    Hi Pavel,

    There is no another way to change the widths in this case and you will have to access them using the index. 

    Please do not hesitate to contact us with any other questions or concerns. 

    Regards,
    Dimitar
    Telerik by Progress
    Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
Back to Top