在网页时,我的GridView为了美观,设置了样式的。同时,我导出的excel也具有原来的样式。我就想,我的excel只有黑白色,怎么实现?我的代码如下
public void ExportToExcel(string FileType, string FileName,GridView GV)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); ;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType; ////解决出现乱码关键问题
////GridView中导出Excel的方法,大部分都是导出一个文本的html文件,
////利用了office能够编辑网页的功能,来实现。只是有时候导出的中文显示是乱码
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GV.RenderControl(hw);
//SS1LogGridView.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
}<div style="position:absolute;width:100%;left: 12px;overflow:auto; height:250px;">
<asp:GridView ID="SS1LogGridView" runat="server" Height="250px"
HorizontalAlign="Center" Width="95%" CellPadding="4" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle BackColor="White" Font-Bold="True" ForeColor="Black" />
</asp:GridView>
</div>
public void ExportToExcel(string FileType, string FileName,GridView GV)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); ;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType; ////解决出现乱码关键问题
////GridView中导出Excel的方法,大部分都是导出一个文本的html文件,
////利用了office能够编辑网页的功能,来实现。只是有时候导出的中文显示是乱码
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GV.RenderControl(hw);
//SS1LogGridView.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
}<div style="position:absolute;width:100%;left: 12px;overflow:auto; height:250px;">
<asp:GridView ID="SS1LogGridView" runat="server" Height="250px"
HorizontalAlign="Center" Width="95%" CellPadding="4" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle BackColor="White" Font-Bold="True" ForeColor="Black" />
</asp:GridView>
</div>
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
} 这里只是简单的举例子,更多的样式设置你需要查看msdn 的Office 开发 SDK
你在后台把样式替换下就行了
可能我题目说错了,我是想excel上面没有样式。
我导出的excel不要样式