Search code examples
asp.netdevexpressexport-to-excelaspxgridview

Export ASPxGridView with column with multiline text to excel


Is it possible to export ASPxGridView with column which contains multiline text to excel using ASPxGridViewExporter?

I have an ASPxGridView with column that contains a multiline text. When I export a grid's data using ASPxGridViewExporter that multiline text is rendered as one line (without line breaks).

I tried both <br/> and "\n" (newline) as line separators.

btw value of property PropertiesTextEdit-EncodeHtml is false on that column.

Thanks


Solution

  • There are 2 ways to achieve what you're looking for:

    1. Specify WYSIWYG export type in XlsxExportOptionsEx:

      XlsxExportOptionsEx options = new XlsxExportOptionsEx()
      {
          ExportType = DevExpress.Export.ExportType.WYSIWYG
      };
      ASPxGridView1.ExportToXlsx("Test.xlsx", options);
      
    2. Tell the exporter you want to have data aware export and handle CustomizeCell event to set cell wrapping to true:

      XlsxExportOptionsEx options = new XlsxExportOptionsEx()
      {
          ExportType = DevExpress.Export.ExportType.DataAware
      };
      
      options.CustomizeCell += options_CustomizeCell;
      
      void options_CustomizeCell(DevExpress.Export.CustomizeCellEventArgs e)
      {
         e.Formatting.Alignment = new XlCellAlignment() { WrapText = true };
         e.Handled = true;    
      }
      

      Then use the customized options object for export.

    See: https://www.devexpress.com/Support/Center/Question/Details/T381176

    There is also the RenderBrick event which sometimes may be helpful. You may handle it like:

    gveExporter.RenderBrick += gveExporter_RenderBrick;
    
    void gveExporter_RenderBrick(object sender, DevExpress.Web.ASPxGridViewExportRenderingEventArgs e)
    {
        ...
        StringFormat sFormat = new StringFormat(StringFormatFlags.NoWrap);
        BrickStringFormat brickSFormat = new BrickStringFormat(sFormat);
        e.BrickStyle.StringFormat = brickSFormat;
        ...
    }
    

    But I have not found how to actually force cell wrap there, because StringFormatFlags has only NoWrap among suitable items. In my experience I did have cell wrapping long text in the exported Excel doc, so I've used RenderBrick to switch that wrapping off.

    Hope that helps.