By default, when exporting data from a grid to the XLSX format, the data is formatted to one of available formats in XLSX. If in Excel two Decimal Places are used for numeric data by default, numeric values with a floating point will be shown with two decimal places regardless of a format set for a corresponding grid column or repository item.
There are two solutions to overcome this shortcoming:
1. You can export values as text. In this case, your formatting will be preserved. For this, assign RepositoryItemTextEdit to a column by setting the GridColumn.ColumnEdit property and set the RepositoryItemTextEdit.ExportMode property to DisplayText. In this case, you can export data as text for specific columns. In addition, you can determine the Export Mode for all data in the grid. For this, pass an XlsxExportOptions object with the XlsxExportOptions.TextExportMode property set to Text to the GridView.ExportToXlsx method. This approach has one disadvantage, though. Since values are exported as text, you will not be able to execute any calculations with them in Excel.
2. You can export data as values and change XLSX data format settings. To do it, create a custom RepositoryItemTextEdit as shown in the Custom Editors help article, and override the GetBrick method. In this method, you can set the TextBrick.XlsxFormatString property to a required value. In this case, you will be able to execute any calculations with exported data in Excel.
This example illustrates how to implement the second approach. In this project we have added the XlsxFormatString property to a custom RepositoryItemTextEdit.
Question Comments
Added By: CaryO at: 2/10/2014 2:31:05 PM
I'm trying to use solution 2. In my case I want to export custom currency format (c2) displayed int he xtragrid for the currency column (say 'InvoiceAmount'). For instance, if the currency column is displayed in british pounds, I want the same to be exported to .xlsx as well. Currently, irrespective of the currency symbol displayed in the grid column it always get exported to default excel currency format. how do I achieve this.
Added By: Svetlana (DevExpress Support) at: 2/11/2014 12:01:47 AMHi,
In order not to mix multiple questions within one thread, I have extracted your inquiry to a separate https://www.devexpress.com/Support/Center/Question/Details/Q466561 ticket. Please refer to it.
Added By: Raval at: 7/17/2014 6:25:55 AMThanks. Solution 1 worked for me.
Added By: OLIVIER ANSQUER at: 11/20/2014 2:41:36 AMHi,
How to make it work with the new DataAware Excel export ?