Skip to main content
Tab

ASPxGridExporterBase.WriteDocx(Stream, DocxExportOptions) Method

Exports the grid’s data to a stream in DOCX format with the specified export options.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v24.2.dll

Declaration

public void WriteDocx(
    Stream stream,
    DocxExportOptions exportOptions
)

Parameters

Name Type Description
stream Stream

A Stream object to which the data is exported.

exportOptions DocxExportOptions

A DocxExportOptions object that provides export options.

Remarks

Note

Before exporting a grid, the ASPxGridExporterBase component creates an export document on a server. Therefore, when you export a large amount of data to a server with limited memory, the OutOfMemoryException exception may appear.

Example

<form id="frmMain" runat="server">
<dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ID" OnDataBinding="Grid_DataBinding">
    <Columns>
        <dx:GridViewDataTextColumn FieldName="C1" />
        <dx:GridViewDataSpinEditColumn FieldName="C2" />
        <dx:GridViewDataTextColumn FieldName="C3" />
        <dx:GridViewDataCheckColumn FieldName="C4" />
        <dx:GridViewDataDateColumn FieldName="C5" />
    </Columns>
</dx:ASPxGridView>
<dx:ASPxGridViewExporter ID="Exporter" runat="server" GridViewID="Grid">
</dx:ASPxGridViewExporter>
<dx:ASPxButton ID="BtnExportToCSV" runat="server" Text="Export To CSV" OnClick="BtnExportToCSV_Click">
</dx:ASPxButton>
<dx:ASPxButton ID="BtnExportToPDF" runat="server" Text="Export To PDF" OnClick="BtnExportToPDF_Click">
</dx:ASPxButton>
<dx:ASPxButton ID="BtnExportToRTF" runat="server" Text="Export To RTF" OnClick="BtnExportToRTF_Click">
</dx:ASPxButton>
<dx:ASPxButton ID="BtnExportToXLS" runat="server" Text="Export To XLS" OnClick="BtnExportToXLS_Click">
</dx:ASPxButton>
<dx:ASPxButton ID="BtnExportToXLSX" runat="server" Text="Export To XLSX" OnClick="BtnExportToXLSX_Click">
</dx:ASPxButton>
</form>
using System;
using System.Linq;
using System.IO;

public partial class _Default: System.Web.UI.Page {
    protected string DocumentsFolder = "~/App_Data/";
    protected string DocumentFileName = "Document";
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsCallback && !IsPostBack) {
            Directory.EnumerateFiles(MapPath(DocumentsFolder)).ToList().ForEach(File.Delete);
            Grid.DataBind();
        }
    }
    protected void Grid_DataBinding(object sender, EventArgs e) {
        Grid.DataSource = Enumerable.Range(0, 100).Select(i => new {
            ID = i,
            C1 = i % 2,
            C2 = i * 0.5 % 3,
            C3 = "C3 " + i,
            C4 = i % 2 == 0,
            C5 = new DateTime(2015 + i, 12, 16)
        }).ToList();
    }
    protected void BtnExportToCSV_Click(object sender, EventArgs e) {
        SaveFile(actionToCall => Exporter.WriteCsv(actionToCall), "csv");
    }
    protected void BtnExportToPDF_Click(object sender, EventArgs e) {
        SaveFile(actionToCall => Exporter.WritePdf(actionToCall), "pdf");
    }
    protected void BtnExportToRTF_Click(object sender, EventArgs e) {
        SaveFile(actionToCall => Exporter.WriteRtf(actionToCall), "rtf");
    }
    protected void BtnExportToXLS_Click(object sender, EventArgs e) {
        SaveFile(actionToCall => Exporter.WriteXls(actionToCall), "xls");
    }
    protected void BtnExportToXLSX_Click(object sender, EventArgs e) {
        SaveFile(actionToCall => Exporter.WriteXlsx(actionToCall), "xlsx");
    }
    protected void SaveFile(Action<Stream> action, string format) {
        using (MemoryStream memoryStream = new MemoryStream()) {
            action(memoryStream);
            memoryStream.Seek(0, SeekOrigin.Begin);
            using (FileStream fileStream = new FileStream(MapPath(
                    string.Format("{0}/{1}.{2}",
                    DocumentsFolder,
                    DocumentFileName,
                    format)
                ),
                FileMode.Create,
                FileAccess.Write)) {
                memoryStream.WriteTo(fileStream);
            }
        }
    }
}
See Also