asp.net 中到处功能,有的时候是将Gridview导出Excel,有的时候导出的是asp页面,这是为啥?我的Excel是2003的,跟版本问题有关吗? 代码是正确的,只有我遇到这样的问题,其他人没有,很奇怪,有没有人遇到这样的问题,求解!!

解决方案 »

  1.   

    我也遇到过!和Excel版本没有关系!public static void ToExcel(GridView ctl, string FileName)
            {
                for (int i = 0; i < ctl.Columns.Count; i++) //设置每个单元格
                {
                    ctl.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                    for (int j = 0; j < ctl.Rows.Count; j++)
                    {
                        ctl.Rows[j].Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
                    }
                }            HttpContext.Current.Response.Charset = "GBK";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
                ctl.Page.EnableViewState = false;
                System.IO.StringWriter tw = new System.IO.StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                ctl.RenderControl(hw);
                string style = @"<style> .text { mso-number-format:\@; } </style> ";
                HttpContext.Current.Response.Write(style);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();
            }
    页面直接调用就可以了