首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

导出Excel的有关问题

2012-04-07 
导出Excel的问题!C# codehtml xmlnshttp://www.w3.org/1999/xhtml head runatservertitle无标

导出Excel的问题!

C# code
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>无标题页</title>    <link href="../css.css" rel="stylesheet" type="text/css" />                    <script runat="server">        public static void GridViewToExcel(Control ctrl, string FileType, string FileName)        {            HttpContext.Current.Response.Charset = "GB2312";            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;//注意编码            HttpContext.Current.Response.AppendHeader("Content-Disposition",                "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());            HttpContext.Current.Response.ContentType = FileType;            ctrl.Page.EnableViewState = false;            System.IO.StringWriter tw = new System.IO.StringWriter();            HtmlTextWriter hw = new HtmlTextWriter(tw);            ctrl.RenderControl(hw);            HttpContext.Current.Response.Write(tw.ToString());            HttpContext.Current.Response.End();        }  protected void Button1_Click( object sender, System.EventArgs e )  {      GridViewToExcel(GridView1, "Excel:application/ms-excel","ttt.xls");     }  protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )  {    if (e.Row.RowType == DataControlRowType.DataRow)    {        e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");      e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");      e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");      e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");    }  }</script>                </head><body>    <form id="form1" runat="server">    <div>        <table align="center" width="100%">        <tr height="30">                    <td width="3%" background="../Images/topbg.jpg" align=left><IMG height="16" src="../Images/icon/right.GIF"><b>外出记录</b>                    </td>                        </tr>            <tr>                <td>                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>                                        <asp:Button ID="Button1" runat="server" Text="导出" OnClick="Button1_Click" />                    </td>            </tr>            <tr>                <td>                    <asp:GridView ID="GridView1" runat="server" Height="71px" Width="798px"                         BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"                         CellPadding="3" AutoGenerateColumns="False"                         onrowdatabound="GridView1_RowDataBound" >                        <FooterStyle BackColor="White" ForeColor="#000066" />                        <RowStyle ForeColor="#000066" />                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />                        <Columns>                            <asp:BoundField DataField="Datetime" HeaderText="登记时间" />                            <asp:BoundField DataField="OutTime" HeaderText="开始时间" />                            <asp:BoundField DataField="ReturnTime" HeaderText="结束时间" />                            <asp:BoundField DataField="Why" HeaderText="外出事由" />                            <asp:BoundField DataField="OutRegisterid" HeaderText="OutRegisterid" Visible="False" />                        </Columns>                    </asp:GridView>                  <asp:Literal ID="HiddenOut" runat="server" />                </td>            </tr>        </table>    </div>    </form></body></html> 



导出的时候:您尝试打开的文件ttt[1].xls的格式与文件扩展名的格式不一致。。。

[解决办法]
不要直接打开,先保存到本地再说.
[解决办法]
Excel:application/ms-excel
错了吧
application/ms-excel

热点排行