2010-08-31 144 views
1

我想將數據從DaraGrid導出到Excel文件中。在法語 這裏有了這些數據,你是我的方法:編碼問題

public static void ExportGridView(DataGrid dataGrid, string fileName) 
    { 
     HttpResponse m_Response = HttpContext.Current.Response; 
     m_Response.Clear(); 
     m_Response.AddHeader("content-disposition", 
      string.Format("attachment;filename={0}", fileName)); 
     m_Response.Charset = "";   
     m_Response.ContentType = "application/octet-stream"; //"application/vnd.xls"; 
     m_Response.ContentEncoding = Encoding.UTF32; 
     StringWriter stringWrite = new StringWriter(); 
     HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
     dataGrid.RenderControl(htmlWrite); 
     m_Response.Write(stringWrite.ToString()); 
     m_Response.End(); 

} 

否則輸出不是一個好的法國格式

例)

從事=>engagé

Société=>Société©

回答

6

嘗試使用ISO-8859-1

public static void ExportGridView(DataGrid dataGrid, string fileName) 
{ 
    var response = HttpContext.Current.Response; 
    response.Clear(); 
    response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); 
    var isoEncoding = Encoding.GetEncoding("iso-8859-1"); 
    response.Charset = isoEncoding.WebName; 
    response.ContentType = "application/vnd.ms-excel"; 
    response.ContentEncoding = isoEncoding; 
    using (var writer = new StringWriter()) 
    using (var htmlWriter = new HtmlTextWriter(writer)) 
    { 
     dataGrid.RenderControl(htmlWriter); 
     response.Write(writer.GetStringBuilder().ToString()); 
    } 
    response.End(); 
} 

另一種方法可能會更好是生成一個完整的HTML頁面,並指定UTF8編碼,這恕我直言,是更好:

public static void ExportGridView(DataGrid dataGrid, string fileName) 
{ 
    var response = HttpContext.Current.Response; 
    response.Clear(); 
    response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); 
    response.Charset = Encoding.UTF8.WebName; 
    response.ContentType = "application/vnd.ms-excel"; 
    response.ContentEncoding = Encoding.UTF8; 
    using (var writer = new StringWriter()) 
    using (var htmlWriter = new HtmlTextWriter(writer)) 
    { 
     dataGrid.RenderControl(htmlWriter); 
     string html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Test</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>{0}</body></html>"; 
     response.Write(string.Format(html, writer.GetStringBuilder())); 
    } 
    response.End(); 
} 
+0

非常感謝Dick,Darin和所有回答的人。 你的第二個解決方案Darin是完美的,但是ISO-8859-1沒有解決問題。這是UTF8 – 2010-08-31 16:20:00

+0

我試過第二個..但它不適用於我.. y:S? – Grace 2011-06-03 07:17:17

+0

第二種方法適用於我,第一種方法(沒有完整的HTML語法)沒有。使用MS Office Professional Plus 2013打開保存的文件。 – 2013-12-03 15:13:54

0

將內容編碼設置爲UTF8應確保Excel文件中的特殊符號將會保留我被解釋得很好。當然,您需要確保在客戶端計算機上安裝了法語字體。

0

我會懷疑他/她使用utf32是有原因的。我會試圖完全打破這條線,看看會發生什麼。如果從Excel到DataGrid,utf8會起作用我認爲你就在那裏,但它是相反的。這條線甚至可能不是問題。

我更懷疑那些「StringWriter」對象。我對這些並不熟悉。如果你使用html來移動東西,瀏覽器不應該能夠識別utf32,而是把它看作是utf16,這可能是你的問題。