2017-07-31 7 views
0

J'utilise 2 ASPxGridView différents dans un onglet différent. Je veux exporter ces ASPxGridViews avec différentes feuilles dans le même fichier Excel.Exporter plusieurs ASPxGridView vers un fichier Excel avec différentes feuilles

Je peux exporter plusieurs ASPxGridViews dans 1 Excel mais aussi 1 feuille.

protected void ExportButton_Click(object sender, EventArgs e) 
{ 
    PrintingSystem ps = new PrintingSystem(); 

    PrintableComponentLink link1 = new PrintableComponentLink(ps); 
    link1.Component = GridExporter1; 

    PrintableComponentLink link2 = new PrintableComponentLink(ps); 
    link2.Component = GridExporter2; 

    CompositeLink compositeLink = new CompositeLink(ps); 
    compositeLink.Links.AddRange(new object[] { link1, link2 }); 

    compositeLink.CreateDocument(); 
    using (MemoryStream stream = new MemoryStream()) 
    { 
     compositeLink.PrintingSystem.ExportToXls(stream); 
     WriteToResponse("filename", true, "xls", stream); 
    } 
    ps.Dispose(); 

} 
void WriteToResponse(string fileName, bool saveAsFile, string fileFormat, MemoryStream stream) 
{ 
    if (Page == null || Page.Response == null) 
     return; 

    string disposition = saveAsFile ? "attachment" : "inline"; 
    Page.Response.Clear(); 
    Page.Response.Buffer = false; 
    Page.Response.AppendHeader("Content-Type", string.Format("application/{0}", fileFormat)); 
    Page.Response.AppendHeader("Content-Transfer-Encoding", "binary"); 
    Page.Response.AppendHeader("Content-Disposition",string.Format("{0}; filename={1}.{2}", disposition, fileName, fileFormat)); 
    Page.Response.BinaryWrite(stream.GetBuffer()); 
    Page.Response.End(); 
} 

ceci est mon article:

<dx:ASPxGridView ID="ASPxGridView1" runat="server" Theme="DevEx"> 
    <Settings ShowGroupPanel="True" /> 
    <SettingsPager PageSize="15" /> 
</dx:ASPxGridView> 

<dx:ASPxGridView ID="ASPxGridView2" runat="server" Theme="Office2010Silver"> 
</dx:ASPxGridView> 

<dx:ASPxButton ID="ExportButton" runat="server" Text="Export both grids" Width="205px" OnClick="ExportButton_Click" /> 

<dx:ASPxGridViewExporter ID="GridExporter1" runat="server" GridViewID="ASPxGridView1" /> 

<dx:ASPxGridViewExporter ID="GridExporter2" runat="server" GridViewID="ASPxGridView2" /><br /> 

Répondre

0

Vous devez spécifier le mode d'exportation pour exporter

Syntaxe

public XlsExportMode ExportMode { get; set; } 

XlsExportMode Un XlsExportMode enumerat ion , représentant le mode d'exportation XLS. La valeur par défaut est XlsExportMode.SingleFile.

et différents modes sont:

Name      Description 
DifferentFiles   A document is exported to multiple files, page-by-page. In this mode every document page is exported to a single XLSX file. 
SingleFile    A document is exported to a single file. Note that in this mode, page headers and footers are added to the resulting XLSX file only once, at the beginning and at the end of the document. 
SingleFilePageByPage  A document is exported to a single file, page-by-page. In this mode, each page is exported to an individual sheet of the same XLSX file. 

afin que vous puissiez faire comme ceci:

using (MemoryStream stream = new MemoryStream()) 
    { 
     XlsExportOptions options = new XlsExportOptions(); 
     options.ExportMode = XlsExportOptions.SingleFilePageByPage; 
     compositeLink.PrintingSystemBase.ExportToXls(stream, options); 
     WriteToResponse("filename", true, "xls", stream); 
    } 

Vérifiez pour plus de détails How to export the ASPxGridView and WebChartControl to the same print document