2011-11-14 78 views
4

我的asp.net應用程序應該將數據集導出爲CSV文件以在Excel中打開。數據集除了英文字符外,還有日文雙倍寬度的漢字字符。我需要什麼字符集才能在Excel中正確編碼CSV格式的日語?

我試過了一些頭,字符集和內容編碼的不同組合,但是我一直沒能找到正確的組合,允許Excel打開文件並正確顯示日文字符。

但是,如果我在記事本中打開該文件,然後使用UTF-8編碼保存該文件,則可以在Excel中打開該文件並按照預期查看日文字符。因此它一定是可能的,但是我無法找到正確的標題組合,使它不需要在記事本中打開和保存導出的文件。

Private Sub TestCSV() 

    Dim context As HttpContext = HttpContext.Current 

    context.Response.Clear() 
    context.Response.ClearHeaders() 
    context.Response.ClearContent() 
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache) 
    context.Response.AddHeader("Content-Disposition", "attachment; filename=test.csv") 
    context.Response.ContentType = "text/csv"   
    context.Response.Charset = Encoding.UTF8.WebName 

    context.Response.Write("English,Japanese") 
    context.Response.Write(Environment.NewLine) 
    context.Response.Write("Test,日本語") 

    context.Response.End() 

End Sub 

任何幫助獲取此代碼的工作將不勝感激。

我也嘗試在各種組合和順序中使用以下代碼行,但沒有任何工作。

context.Response.BinaryWrite(Encoding.UTF8.GetPreamble()) 
    context.Response.BinaryWrite(Encoding.GetEncoding("utf-16le").GetPreamble()) 
    context.Response.Charset = Encoding.GetEncoding("utf-16le").WebName 
    context.Response.AddHeader("Content-Type", "text/csv; charset=utf-16") 
    context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250") 

謝謝!

回答

0

Got it!我添加了一些其他語言和符號,以便加倍確定。

關鍵是將標題中的字符集,內容編碼前導碼全部轉換爲UTF-8。

Private Sub TestCSV() 

    Dim context As HttpContext = HttpContext.Current 

    context.Response.Clear() 
    context.Response.ClearHeaders() 
    context.Response.ClearContent() 
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache) 
    context.Response.ContentType = "text/csv" 
    context.Response.AddHeader("Content-Disposition", "attachment; filename=test.csv") 
    context.Response.Charset = Encoding.UTF8.WebName 
    context.Response.ContentEncoding = Encoding.UTF8 
    context.Response.BinaryWrite(Encoding.UTF8.GetPreamble) 

    context.Response.Write("Language,Sample") 
    context.Response.Write(Environment.NewLine) 
    context.Response.Write("Symbol,™£©€®") 
    context.Response.Write(Environment.NewLine) 
    context.Response.Write("Japanese,日本語") 
    context.Response.Write(Environment.NewLine) 
    context.Response.Write("Chinese Simplified,中文(簡體)") 
    context.Response.Write(Environment.NewLine) 
    context.Response.Write("Spanish,Español") 
    context.Response.Write(Environment.NewLine) 
    context.Response.Write("Chinese Traditional,中文(繁體)") 
    context.Response.Write(Environment.NewLine) 
    context.Response.Write("Korean,한국어") 

    context.Response.End() 

End Sub 

謝謝!

0
/// <summary> 
/// method to export report into excel 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
protected void linkProcess_Click(object sender, EventArgs e) 
{ 
    Response.ClearContent(); 
    Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "YTDReviewsData")); 
    Response.ContentType = "application/ms-excel"; 
    Response.ContentEncoding = System.Text.Encoding.Unicode; 
    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());  
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
    gvDealDetails.RenderControl(htmlWrite); 
    Response.Write(stringWrite.ToString()); 
    Response.End(); 
} 
相關問題