Export GridView to Excel with column formatted as Percent

4 posts, 1 answers
  1. Brian
    Brian avatar
    37 posts
    Member since:
    Jul 2010

    Posted 19 Dec 2017 Link to this post

    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.
  2. Brian
    Brian avatar
    37 posts
    Member since:
    Jul 2010

    Posted 20 Dec 2017 in reply to Brian Link to this post

    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");
    }
  3. Answer
    Hristo
    Admin
    Hristo avatar
    1507 posts

    Posted 21 Dec 2017 Link to this post

    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.
  4. Brian
    Brian avatar
    37 posts
    Member since:
    Jul 2010

    Posted 02 Jan 2018 in reply to Hristo Link to this post

    That did the trick. Appreciate the help.

     

     

Back to Top