2015-12-23 64 views
1

這裏是我的代碼:asp.net導出到Excel UTF-8

var model = _db.FacebookInfo.OrderByDescending(f => f.Followers).ToList(); 
    var grid = new System.Web.UI.WebControls.GridView(); 



    grid.DataSource = (from u in model 
         select new 
         { 
          fullname = System.Text.RegularExpressions.Regex.Replace(u.FullName, "<span .*?>(.*?)", ""), 
          followers = u.Followers, 
          friends = u.Friends, 
          url = u.FbLink 
         }).ToList(); 

    grid.DataBind(); 

    Response.ClearContent(); 
    Response.AddHeader("content-disposition", "attachment; filename=Nana bregvadze Friends.xls"); 
    Response.ContentType = "application/excel"; 

    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 

    grid.RenderControl(htw); 

    Response.Write(sw.ToString()); 

    Response.End(); 

它只是創建一個Excel文件,但我有一個統一的問題,無法讀取格魯吉亞charachters。看圖片:

enter image description here

也許這是一個重複的,但沒有辦法爲我工作。

+0

Excel和UTF8沒有問題。您的代碼完全不會生成Excel - 它會創建一個HTML表格,其中包含一個虛假標題和Excel *作爲HTML導入的文件擴展名。使用像EPPlus這樣的庫來創建* real * XLSX文件。它實際上更容易,像'ws.Cells [「A1」]這樣的調用。LoadFromCollection(model)將從任何IEnumerable集合生成一張表 –

+0

只是沒有辦法從html讀取utf-8 charachtares? –

+0

當然,只要您設置正確的HTTP標頭即可。但是,當正確的解決方案更簡單時,爲什麼堅持使用醜陋的黑客?在Excel中使用默認設置導入HTML文件時發現* next *問題會發生什麼?如果您和客戶端之間的任何防火牆或代理檢測到發送HTML文件而不是二進制'xls'文件並阻止它,會發生什麼?如果客戶想要格式化會怎麼樣?孩子玩真正的Excel文件,真的很難用HTML –

回答

0

您的代碼不會創建真正的XLSX文件,它會創建一個僞造標題和擴展名的HTML表格。 Excel將導入這個文件使用最終用戶的默認設置和區域設置。

而不是僞造Excel文件,您可以使用庫如EPPlus輕鬆地創建一個真正的XLSX文件與最少的代碼。 Codeplex站點中甚至有一個Web Application示例。你可以在你的情況下使用LoadFromCollection代替LoadFromDataTable

using (ExcelPackage pck = new ExcelPackage()) 
{ 
    //Create the worksheet 
    var ws = pck.Workbook.Worksheets.Add("Demo"); 
    //Load the datatable into the sheet, starting from cell A1. 
    // Print the column names on row 1 
    var table=ws.Cells["A1"].LoadFromCollection(model, true); 


    //Write it back to the client 
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx"); 
    Response.BinaryWrite(pck.GetAsByteArray()); 
} 

注意table是一個實際的ExcelRange對象,可以給一個名字,風格等

-1

奧基我解決這個問題: 只是改變Response.ClearContent()來Response.Clear(),並添加

Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />") 

全碼:

var model = _db.FacebookInfo.OrderByDescending(f => f.Followers).ToList(); 
     var grid = new System.Web.UI.WebControls.GridView(); 
    grid.DataSource = (from u in model 
         select new 
         { 
          fullname = System.Text.RegularExpressions.Regex.Replace(u.FullName, "<span .*?>(.*?)", ""), 
          followers = u.Followers, 
          friends = u.Friends, 
          url = u.FbLink 
         }).ToList(); 

    grid.DataBind(); 

    Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment; filename=Nana bregvadze Friends.xls"); 
    Response.ContentType = "application/excel"; 
    Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 

    grid.RenderControl(htw); 

    Response.Write(sw.ToString()); 

    Response.End();