将多个ASPxGridView导出到具有不同工作表的Excel

时间:2017-07-31 07:07:48

标签: .net devexpress export-to-excel aspxgridview

我在不同的标签中使用了2个不同的ASPxGridView。我想在同一个excel文件中使用不同的工作表导出这些ASPxGridViews。

我可以在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 />

1 个答案:

答案 0 :(得分:0)

您必须指定要导出的导出模式

<强>语法

public XlsExportMode ExportMode { get; set; }
  

XlsExportMode XlsExportMode枚举   value,表示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