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

Export GridView to Excel with column formatted as Percent

3 Answers 611 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Iron
Iron
Brian asked on 19 Dec 2017, 04:13 PM
I am trying to export a GridView in WinForms to Excel. One column I have formatted in the GridView as Percent, however when I am exporting to Excel, I don't see how I can format a specific column as Percent.

3 Answers, 1 is accepted

Sort by
0
Brian
Top achievements
Rank 1
Iron
Iron
answered on 20 Dec 2017, 03:06 PM

Here is the exact code I have tried. I also noticed that it will not export as an xml extension, it exports as xls even though I have the file name specified.

 

private void radBtnExcel_Click(object sender, EventArgs e)
{
    ExportData();
}
 
void ExportData()
{
    //ExportToExcelML exporter = new ExportToExcelML(radGv);
    var excelML = new ExportToExcelML(radGv);
    excelML.ExportVisualSettings = true;
    radGv.Columns["Success Ratio"].ExcelExportType = DisplayFormatType.Custom;
    radGv.Columns["Success Ratio"].ExcelExportFormatString = "0:#0.000%; (0000#);none";
    excelML.SheetMaxRows = ExcelMaxRows._1048576;
    excelML.RunExport("c:\\#support\\CTCStatistics.xml");
}
0
Accepted
Hristo
Telerik team
answered on 21 Dec 2017, 09:52 AM
Hello Brian,

Thank you for writing.

It appears that the number format you are using is not valid for Excel. You can try this way: 
this.radGridView1.Columns[0].ExcelExportType = Telerik.WinControls.UI.Export.DisplayFormatType.Custom;
this.radGridView1.Columns[0].ExcelExportFormatString = "#0.000%; (0000#);";

Additionally, you can also check the following article: https://support.office.com/en-us/article/Create-or-delete-a-custom-number-format-78f2a361-936b-4c03-8772-09fab54be7f4

Regarding the extension of the exported file, you can you can set the FileExtension property:
ExportToExcelML exporter = new ExportToExcelML(this.radGridView1);
exporter.FileExtension = "xml";

I also recommend switching the GridViewSpreadExport class as it is using the spread processing libraries and it can directly export in the .xlsx format: https://docs.telerik.com/devtools/winforms/gridview/exporting-data/spread-export.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Brian
Top achievements
Rank 1
Iron
Iron
answered on 02 Jan 2018, 02:21 PM

That did the trick. Appreciate the help.

 

 

Tags
GridView
Asked by
Brian
Top achievements
Rank 1
Iron
Iron
Answers by
Brian
Top achievements
Rank 1
Iron
Iron
Hristo
Telerik team
Share this question
or