protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (this.GridView1.Rows.Count > 0)
            {
                ToExcel(GridView1, "aaa.xls");
            }
            else
            {
                MessageBox.Show(this, "暂无需要导出的数据!");
            }
        }
        public override void VerifyRenderingInServerForm(Control control)
        {        }        private void ToExcel(Control ctl, string FileName)
        {
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            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);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }这是我从GridView中导出数据到Excel表格中,
然后我又在另一个页面把这个Excel表格导入系统,则报错:外部表不是预期的格式。。请各位看清楚我的问题在发言,谢谢! 

解决方案 »

  1.   

    问题补充。。导入这个Excel,在VS里面打开它,显示的是纯html格式的代码,我想问题应该出现在这里。是到处的时候格式没设置正常,求解。多谢!
      

  2.   

     protected void Button1_Click(object sender, EventArgs e)
        {
            Export("application/ms-excel","学生成绩报表.xls");    }    //GridView控件与Excel的转换
        private void Export(string FileType,string FileName) 
        {
            Response.Charset = "GB2312";//输出是采用的gb2312(简体中文)。 
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(FileName,System.Text.Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();    }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
    }
      

  3.   

    给你一个数据集导出为EXCEL的类吧。。 public static class CreateExcel
        {
            /// <summary>
            ///  导出Exel
            /// </summary>
            /// <param name="ctl">导出数据的控件载体</param>
            /// <param name="FileName">导出Exel的默认名字</param>
            public static void ToExcel(System.Web.UI.Control ctl, string FileName)
            {            HttpContext.Current.Response.Charset = "UTF-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
                ctl.Page.EnableViewState = false;
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                ctl.RenderControl(hw);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();
            }        public static void ToExcel1(DataTable Dt, string FileName)
            {
                System.Web.UI.WebControls.GridView dgExport = null;
                //当前对话 
                System.Web.HttpContext curContext = System.Web.HttpContext.Current;
                //IO用于导出并返回excel文件 
                System.IO.StringWriter strWriter = null;
                System.Web.UI.HtmlTextWriter htmlWriter = null;            if (Dt != null)
                {
                    //设置编码和附件格式 
                    //System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码
                    curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
                    curContext.Response.ContentType = "application nd.ms-excel";
                    curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    curContext.Response.Charset = "GB2312";                //导出Excel文件 
                    strWriter = new System.IO.StringWriter();
                    htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);                //为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView 
                    dgExport = new System.Web.UI.WebControls.GridView();
                    dgExport.DataSource = Dt.DefaultView;
                    dgExport.AllowPaging = false;
                    dgExport.DataBind();                //下载到客户端 
                    dgExport.RenderControl(htmlWriter);
                    curContext.Response.Write(strWriter.ToString());
                    curContext.Response.End();
                }
            }
        }
      

  4.   

    你导出到excel的格式并不是真正的excel格式
      

  5.   

    那请问如何能导出真正的excel格式?