+ -
当前位置:首页 → 问答吧 → ASP.NET导出EXECL字符串01被自动转成1如何解决?

ASP.NET导出EXECL字符串01被自动转成1如何解决?

时间:2011-12-28

来源:互联网

private void ExportExcel(System.Data.DataTable dt)
  {
  System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  System.Web.HttpContext.Current.Response.Buffer = false;
  System.Web.HttpContext.Current.Response.Clear();
  System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  System.Web.HttpContext.Current.Response.ClearHeaders();
  System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");

  string sep = "";
  foreach (DataColumn dc in dt.Columns)
  {
  System.Web.HttpContext.Current.Response.Write(sep + dc.ColumnName);
  sep = "\t";
  }
  System.Web.HttpContext.Current.Response.Write("\n");

  int i;
  foreach (DataRow dr in dt.Rows)
  {
  sep = "";
  for (i = 0; i < dt.Columns.Count; i++)
  {
  string mes = dr[i].ToString();
  if (i == 8)
  {
  mes = " " + dr[i].ToString();
  }

  System.Web.HttpContext.Current.Response.Write(sep + mes);
  sep = "\t";
  }
  System.Web.HttpContext.Current.Response.Write("\n");
  }
  System.Web.HttpContext.Current.Response.End();
  }

我用此方法进行EXECL的导出。但是DataTable中有一列会出现字符串01。我想导出的execl中就显示01。调试进去
System.Web.HttpContext.Current.Response.Write(sep + mes);
中的mes也是01。
但是导出的EXECL中会被自动转换成1。请问各位大大如何解决这个问题啊。
系统使用
Microsoft.Office.Interop.Excel._Application excel = new ApplicationClass();
时会报
检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。 
不用这个控件有解决方法吗?

作者: iceqin34   发布时间: 2011-12-28

设置单元格格式为字符型

作者: liyangfd   发布时间: 2011-12-28

C# code

01./// <summary>   
02./// GridView_CheckStat加入行变化样式   
03. /// </summary>   
04./// <param name="sender"></param>   
05./// <param name="e"></param>   
06.  
07.protected void GridView_CheckStat_RowDataBound(object sender, GridViewRowEventArgs e)  
08.{  
09.    if (e.Row.RowType == DataControlRowType.DataRow)  
10.    {  
11.        //当鼠标在某一行上方时激发   
12.        e.Row.Attributes.Add("onmouseover", "curColor=this.style.backgroundColor;this.style.backgroundColor='#DDCCAA'");  
13.        //当鼠标从某一行上方移开时激发   
14.        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=curColor");  
15.        //导出Excel时文本化   这就是中点!!!!!!!!!!!!!!!!!!!!
16.        //e.Row.Cells[1].Attributes.Add("class", "text");    
17.        //这里的[0]代表第一列   
18.        e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");  
19.    }  
20.}  

作者: ajaxtop   发布时间: 2011-12-28

设置execl单元格的格式为文本型

作者: Net_Java_dram   发布时间: 2011-12-28