我正在尝试使用此代码将我的面板导出到Excel中。
设计:
<asp:Panel ID="ExcelPanel" runat="server" Visible="false">
<asp:GridView ID="ExcelGridView" runat="server" AutoGenerateColumns="False"
Width="100%" CellPadding="3" ForeColor="#333333" GridLines="Both" ShowHeader="True" Visible="true" >
<Columns>
<asp:TemplateField HeaderText="S. No." HeaderStyle-CssClass="gridViewHeader" ItemStyle-HorizontalAlign="left" SortExpression="wcName" ItemStyle-Width="12%">
<ItemTemplate>
<asp:Label ID="performanceReportTBMWCNameLabel" runat="server" Text='<%# Eval("SNo") %>' CssClass="gridViewItems"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
C#代码:
ExcelGridView.DataSource = gridviewdt;
ExcelGridView.DataBind();
ExcelPanel.Visible = true;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=file.xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
ExcelPanel.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
ExcelPanel.Visible = false;
这是我想要在excel文件中的区域。但是我在excel文件中也获得了包含母版页的整个页面。如何避免整个页面,只获取excel文件中的面板?
答案 0 :(得分:0)
您正在从ExcelPanel
呈现HTML,因此它会将有关该面板更改面板的所有HTML显示为GridView
,因此它将从GridView
变化
ExcelPanel.RenderControl(htmlWrite);
到
ExcelGridView.RenderControl(htmlWrite);