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();
    }这个是我导出excel的代码,但是有个小问题,就是当gridview里某列为纯数字串的时候,比如:0235564525这样的,导出到excel里后前面的0都会丢失,而0235655A552这样的就不丢失,我想让纯数字的也不丢失前面的0,都是完整的导出到excel,请高手给出正确代码,谢谢!急...解决后送50分!谢谢!

解决方案 »

  1.   

    需要在哪里强制指定类型呢?
    在gridview里显示的是string型的,前面的0也都能正常显示的,只是导出后就丢失了,我上面贴的代码里改吗?不是很明白,请详细点好吗?谢谢!解决了马上送分,感谢!
      

  2.   

    以前早期的做法是在数字前加一个'号就可以了
    如:0235564525= '0235564525 这样很久没玩excel了,新解法没砸关心他,只记得老解法了
      

  3.   

    gridView.Rows[i].Cells[i]你的代码不够全面,不太清楚你的逻辑.
      

  4.   

        protected void btnToExcel_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.AllowSorting = false;
            GridView1.DataBind();
            ToExcel(GridView1, "DCMX_Data.xls");
            GridView1.AllowPaging = true;
            GridView1.AllowSorting = true;
        }    #region 导出为Excel
        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
        }    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();
        }
        #endregion这是整个代码,在这个基础上可以改吗?或者提供一个可行的其他代码也可以
      

  5.   

    已回答了!
    http://topic.csdn.net/u/20071127/13/2df09efc-9883-4adf-b4f7-5147c59eba93.html
      

  6.   

    qing2005我按照你的方法做了,数字字符串可以完整导出了,可是汉字都变成乱码了啊 
      

  7.   

        protected void btnToExcel_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.AllowSorting = false;
            GridView1.DataBind();
            ToExcel(GridView1, "DCMX_Data.xls");
            GridView1.AllowPaging = true;
            GridView1.AllowSorting = true;
        }    #region 导出为Excel
        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for
        }    private void ToExcel(Control ctl, string FileName)
        {
            string style = @"<style> .text { mso-number-format:\@; } </script> "; //设置格式
             Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment;filename=" + "" + FileName);
            Response.ContentType = "application/excel";
            ctl.Page.EnableViewState = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            ctl.RenderControl(htw);
            Response.Write(style);
            Response.Write(sw.ToString());
            Response.End();
        }
        #endregion我修改后的代码是这样的,可是导出来的excel里文字都是乱码了
      

  8.   

    也可以在 DataGrid1中ItemDataBound事件加 e.Item.Cells["某一列"].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); 加这句话
      

  9.   

     if (dt.Rows[row][col].GetType() == typeof(string))
                            {
                                newSheet.Cells[row + 2, col + 1] = "'" + dt.Rows[row][col].ToString(); ;//将长数值转换成文本
                            }
                            else
                            {
                                newSheet.Cells[row + 2, col + 1] = dt.Rows[row][col].ToString();
                            }
      

  10.   

    人家说的是winform不是web哪些不看病就瞎叽歪的大哥,别逼着眼睛回答好不好