This is a migrated thread and some comments may be shown as answers.

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

4 Answers 196 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pavel
Top achievements
Rank 1
Pavel asked on 22 Nov 2016, 10:34 PM

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));
        }
    }
}

4 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 23 Nov 2016, 10:49 AM
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.
0
Pavel
Top achievements
Rank 1
answered on 23 Nov 2016, 04:40 PM
Dimitar, is there a way to have more control over the column width? I want to set different widths for different columns.
0
Pavel
Top achievements
Rank 1
answered on 23 Nov 2016, 04:44 PM
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.
0
Dimitar
Telerik team
answered on 24 Nov 2016, 07:36 AM
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.
Tags
GridView
Asked by
Pavel
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Pavel
Top achievements
Rank 1
Share this question
or