Export To Excel
Export To Excel
Export To Excel
including styles
ASP.NET
2
Default.aspx
<table class="tblStyle">
<tr>
<td align="right">
<asp:LinkButton ID="LinkButton1" runat="server"
Text="Export to Excel" OnClick="LinkButton1_Click"></asp:LinkButton>
</td>
</tr>
</table>
<asp:Panel ID="pnlUsers" runat="server">
<table class="tblStyle">
<tr>
<td align="center">
<asp:Label ID="lblheader" runat="server"
Text="User Details" CssClass="pageHeader"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="grvTest" runat="server"
AutoGenerateColumns="false">
<HeaderStyle CssClass="gridHeader" />
<RowStyle CssClass="gridItem" />
<Columns>
<asp:BoundField HeaderText="ID"
DataField="id" HeaderStyle-CssClass="idStyle" />
<asp:BoundField HeaderText="Name"
DataField="name" HeaderStyle-CssClass="nameStyle" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</asp:Panel>
Now if you are using update panel in your page and click on
export button you’ll get an error similar to the following
3
To avoid this error include the post back trigger for the link
button.
<Triggers>
<asp:PostBackTrigger ControlID="LinkButton1" /> </Triggers>
Now to preserve CSS Styles in your Ecel also use the folloiwng Export
functions
strHtml += "<html><head>";
strHtml += "<style type=\"text/css\">";
strHtml += cssSheet;
strHtml += "</style></head><body>";
Results
5