日期:2014-05-18  浏览次数:20913 次

ASP.NET导出EXECL字符串01被自动转成1如何解决?
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。 
不用这个控件有解决方法吗?

------解决方案--------------------
设置execl单元格的格式为文本型
------解决方案--------------------
string mes = "=\"" + dr[i].ToString() + "\"";

------解决方案--------------------
探讨

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