请教大家一个问题:Gridview里面的列是自动生成的,同时,我还在后台cs代码中手动增加了几个BoundField。而我gridview里面的内容有一些日期类型,还有一些编号(000232,000342),以及(3.00,3.100)等类似的格式,导出后数据格式出现如下:232,342,3,3。1.
我导出execl的方法如下:
  string style=@"<style> .text { mso-number-format:\@; } </script> ";             Response.ClearContent();            Response.AddHeader("content-disposition", "attachment; filename=InputFileSolineInfo.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/vnd.xls";
            StringWriter sw = new StringWriter();            HtmlTextWriter htw = new HtmlTextWriter(sw);           
            
            GridViewInputFile.RenderControl(htw);            // Style is added dynamically            Response.Write(style);            Response.Write(sw.ToString());            Response.End();
  protected void GridViewInputFile_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < GridViewInputFile.Columns.Count; i++)
                {
                    e.Row.Cells[i].Attributes.Add("class", "text");
                  //e.Row.Cells[i].Text =  e.Row.Cells[i].Text + " "; 
                }
            }
        }
主要原因是我 GridViewInputFile.Columns.Count=0,所以没有办法对此格式化为文本。
方法2:如果直接用execl模板写数据,这样导出也非常不方便,导出的格式与实际页面差距比较大(不考虑使用此方法)

解决方案 »

  1.   

    //在gridview的每一行绑定事件里把下面这句话加上,并且数出那个身份证是第几列。
    protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
             if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 //比如身份证是第7列,格式化为文本。
                 e.Row.Cells[7].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
            }
        }
      

  2.   


    如一楼
    e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
      

  3.   

    目前主要原因是:我的GridViewInputFile.Columns.Count=0;
    而且我现在需要把所有的gridview都转化为文本格式。
      e.Row.Cells[7]
    这个row.cells.count=0