2013-03-19 73 views
0

我正在將數據表導出爲ex​​cel,並且一列在數據表中具有電話號碼,但在Excel中導出後,電話號碼coloumn將顯示爲指數。如何將指數格式化爲從導出到Excel的編號?

我需要數字,如何解決這個問題?

string fileName = "File" + DateTime.Now.ToString("MMddyyyy_HHmmss") + ".xls"; 
     Response.AddHeader("content-disposition", "attachment;filename=" + fileName); 
     //Response.AddHeader("content-disposition", "attachment;filename=File.xls"); 
     Response.ContentType = "application/vnd.ms-excel"; 

     StringWriter stringWriter = new StringWriter(); 
     HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter); 
     DataGrid dataExportExcel = new DataGrid(); 
     dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataExportExcel_ItemDataBound); 
     dataExportExcel.DataSource = dt; 
     dataExportExcel.DataBind(); 
     dataExportExcel.RenderControl(htmlWrite); 
     System.Text.StringBuilder sbResponseString = new System.Text.StringBuilder(); 
     sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head></head> <body>"); 
     sbResponseString.Append(stringWriter + "</body></html>"); 
     Response.Write(sbResponseString.ToString()); 
     Response.End(); 
+0

與[this]相同(http://stackoverflow.com/questions/15024367/exporting-contacts-into-csv-file-automatically-format-mobile-numbers-into-scient#comment21113659_15024367)? – Sinatr 2013-03-19 11:00:16

+0

你想以編程方式將一個單撇號的電話號碼 – JMK 2013-03-19 11:12:01

+0

這是否有助於我的問題? 上面的代碼我寫作爲出口到Excel,有沒有其他最好的選擇呢? 我正在將數據錶轉換爲html,實際上我需要將數據導出爲ex​​cel? – Anand 2013-03-19 11:16:19

回答

2

使用本 TD {MSO的數字格式: 「@」; }

2

您可以使用NumberValue and NumberFormat properties

這裏的樣本,設定你的範圍

yourSheet.Range[".."].NumberValue = 1234.5678; 
yourSheet.Range[".."].NumberFormat = "0.00"; 
+0

我需要在上面的代碼中添加? – Anand 2013-03-19 11:06:13

+0

當你寫你的文件,你的代碼匯訪問 – 2013-03-19 11:07:44

1

你可以在前面加上一個單引號的電話號碼將其寫入spreadsheat的細胞之前。

像「'1234567890" ,而不是 「1234567890」

感謝

3

以下風格添加到您的HTML標記,它會幫助你..

<style> table { mso-number-format:'0'; } </style> 

這樣:

sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> td { mso-number-format:'0'; } </style></head> <body>"); 

全碼:

protected void btnExcel_Click(object sender, EventArgs e) 
    { 
     DataTable dt = new DataTable(); 
     dt.Clear(); 
     dt.Columns.Add("Phone"); 
     dt.Columns.Add("Name"); 
     DataRow Sample = dt.NewRow(); 
     Sample["Phone"] = 125316245612456124; 
     Sample["Name"] = "Pandian"; 
     dt.Rows.Add(Sample); 
     GetExcel(dt);    
    } 
    public void GetExcel(DataTable dt) 
    { 
     string fileName = "File" + DateTime.Now.ToString("MMddyyyy_HHmmss") + ".xls"; 
     Response.AddHeader("content-disposition", "attachment;filename=" + fileName); 
     Response.ContentType = "application/vnd.ms-excel"; 
     StringWriter stringWriter = new StringWriter(); 
     HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter); 
     DataGrid dataExportExcel = new DataGrid(); 
     dataExportExcel.DataSource = dt; 
     dataExportExcel.DataBind(); 
     dataExportExcel.RenderControl(htmlWrite); 
     System.Text.StringBuilder sbResponseString = new System.Text.StringBuilder(); 
     sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> table { mso-number-format:'0'; } </style></head> <body>"); 
     sbResponseString.Append(stringWriter + "</body></html>"); 
     Response.Write(sbResponseString.ToString()); 
     Response.End(); 
    } 

輸出到Excel:

Excel Output

+0

其工作..非常感謝 – Anand 2013-03-19 13:11:13

+0

@Anand:歡迎:) – Pandian 2013-03-19 13:20:51

+0

pandian喜 它不使用字符超過500以及日期細胞 請幫我最好什麼格式的工作? – Anand 2013-03-21 10:31:07

相關問題