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