Export ASPxGridView to xlsx using ASPxGridViewExporter with formatted text
Solution 1
The ASPxGridViewExporter exports the grid content by values, not display text. Create an instance of the XlsxExportOptions class, set its TextExportMode property to Text, and use this instance during exporting. Here is a corresponding ticket on DevExpress Support Center: ASPxGridView - The CustomColumnDisplayText event doesn't not work when exporting to Excel after upgrading from v.9.2 to v.13.2.
You may also face a problem described in the ASPxGridView / MVC GridView Extension - Excel Data Aware Export FAQ ticket. This ticket provides a solution.
Solution 2
Try this.
<asp:UpdatePanel runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="lnkXlsxExport" />
</Triggers>
</asp:UpdatePanel>
Szabolcs Antal
Software development engineer. "If you are the smartest person in the room then you are in the wrong room." And I think that rock is the best music.
Updated on June 15, 2022Comments
-
Szabolcs Antal almost 2 years
I have an
ASPxGridView
having a number of columns (of typeGridViewDataComboBoxColumn
orGridViewDataHyperLinkColumn
). In the code behind in the grid'sCustomColumnDisplayText
event I format the text of the cells in the following way:protected void MyGrid_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) { if (e.Column is DevExpress.Web.ASPxGridView.GridViewDataColumn) { switch (e.Column.FieldName) { case "Energy": if (e.Value is float) { // the FormatEnergy method here returns for example "323.32 kWh" e.DisplayText = DataDisplayTools.Instance.FormatEnergy((float)e.Value); } break; // other cases } } }
For exporting the grid view in .xlsx format I use an
ASPxGridViewExporter
:protected void lnkXlsxExport_Click(object sender, EventArgs e) { try { gridViewExporter.DataBind(); gridViewExporter.WriteXlsxToResponse(); } catch (Exception e1) { // } }
My problem is that when I export the grid view to .xlsx, the values of the columns in the exported file are not formatted as they are in the grid view. For example the values in the grid view (they are formatted in the
CustomColumnDisplayText
event) are- 32,332 kWh
- 311 kWh
- 2,254 kWh
but these values in the xlsx file after exporting appear like
- 32332
- 311
- 2254
My question is: Can export an
ASPxGridView
to an excel file having exactly the same texts in the excel file's cells as the grid view has, using anASPxGridViewExporter
? -
xKobalt over 3 yearsPlease, provide an explanation about what your solution is supposed to do and why