2010-01-31 45 views
0

我轉換數據表中使用下面的代碼,以擅長: 公共靜態無效ExportDataTabletoExcel(字符串文件名,數據表thetable) { 如果(thetable!= NULL ) {出口數據表到Excel,價值3E2成爲300,應該是文字3E2

 //clear anything in io buffer 
     HttpContext.Current.Response.Clear(); 
     //set to excel for to save file. 
     HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; 
     HttpContext.Current.Response.AddHeader(
     "content-disposition", string.Format("attachment; filename={0}", fileName)); 
     HttpContext.Current.Response.ContentType = "application/ms-excel"; 

     HttpContext.Current.Response.Charset = ""; 
     HttpContext.Current.Response.BufferOutput = true; 
     //write columns 
     string tab = ""; 
     foreach (DataColumn dc in thetable.Columns) 
     { 
      HttpContext.Current.Response.Write(tab + dc.ColumnName); 
      tab = "\t"; 

     } 
     HttpContext.Current.Response.Write("\n"); 

     //write rows 
     int i; 
     foreach (DataRow dr in thetable.Rows) 
     { 
      tab = ""; 
      for (i = 0; i < thetable.Columns.Count; i++) 
      { 
       //if (i != 1) 
       HttpContext.Current.Response.Write(tab + dr[i].ToString()); 
       //else 
       //HttpContext.Current.Response.Write(tab + "'" + dr[i].ToString()); 
       tab = "\t"; 
      } 
      HttpContext.Current.Response.Write("\n"); 
     } 
     HttpContext.Current.Response.End(); 
    } 

}

然而,對於值3E2或0E2或4E3,它被轉換爲數字..像1D2其它值被正確地轉換成字符串。

例如3E2成爲300等圍繞價值

+1

您似乎正在使用'application/ms-excel' MIME類型提供製表符分隔的值文件。我懷疑這是不錯的做法。 – Thomas 2010-01-31 16:18:48

+0

你能建議我應該用什麼來代替 – 2010-02-12 02:23:13

回答

1

把雙引號,這樣很明顯到Excel,它是一個字符串,而不是在科學記數法的數字。