2017-07-31 140 views
0

我在不同的選項卡中使用2個不同的ASPxGridView。我想用同一個excel文件中的不同工作表導出這些ASPxGridViews。將多個ASPxGridView導出到不同工作表的Excel中

我可以在1 excel中導出多個ASPxGridViews,但也可以導出1張表格。

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(); 
} 

這是我的項目:

<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 /> 

回答

0

您必須指定導出模式導出

語法

public XlsExportMode ExportMode { get; set; } 

XlsExportMode的XlsExportMode列舉,離子 值,表示XLS導出模式。默認爲 XlsExportMode.SingleFile。

而且各種各樣的方式是:

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. 

所以你可以這樣做:

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

詳細信息請參考How to export the ASPxGridView and WebChartControl to the same print document

相關問題