2010-03-05 33 views
-1

我需要將gridview導出爲ex​​cel,我將從gridview返回的html代碼放到HtmlTextWriter中,並將其放入響應中。Excel 2008無法解析HTML

結果文件在excel中可以正常工作,excel可以解析html,結果是可讀的,在excel 2003和2007中工作完美,但在某些帶有Excel 2008(MACOS)的計算機上,excel只顯示原始html代碼,處理這個html代碼。

任何想法配置excel?

這是轉換代碼:

public static void ToExcel(GridView gridView, string fileName) 
{ 
    HttpResponse response = HttpContext.Current.Response; 
    response.Clear(); 
    response.Buffer = true; 

    fileName = fileName.Replace(".xls", string.Empty) + ".xls"; 

    response.AddHeader("content-disposition", 
         "attachment;filename=" + fileName); 
    response.Charset = ""; 
    response.ContentEncoding = Encoding.Unicode; 
    response.BinaryWrite(Encoding.Unicode.GetPreamble()); 
    response.ContentType = MimeTypes.GetContentType(fileName); 

    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 
    gridView.AllowPaging = false; 
    //gridView.DataBind(); 

    //Change the Header Row back to white color 
    gridView.HeaderRow.Style.Add("background-color", "#FFFFFF"); 

    //Apply style to Individual Cells 
    for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++) 
    { 
     gridView.HeaderRow.Cells[i].Style.Add("background-color", "yellow");  
    } 

    for (int i = 0; i < gridView.Rows.Count; i++) 
    { 
     GridViewRow row = gridView.Rows[i]; 

     //Change Color back to white 
     row.BackColor = System.Drawing.Color.White; 

     //Apply text style to each Row 
     row.Attributes.Add("class", "textmode"); 

     //Apply style to Individual Cells of Alternating Row 
     if (i % 2 != 0) 
     { 
      for (int j = 0; j < row.Cells.Count; j++) 
      { 
       row.Cells[j].Style.Add("background-color", "#C2D69B"); 
      } 
     } 
    } 

    gridView.RenderControl(hw); 

    //style to format numbers to string 
    string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
    response.Write(style); 
    response.Output.Write(sw.ToString()); 
    response.Flush(); 
    response.End(); 
} 
+0

嘗試攝入這個HTML樣本程序的一個較大的量。 OpenOffice.org將成爲我的第一個考生。 – Broam 2010-03-05 14:12:34

+0

@Broam這是如何幫助Excel Mac OS的問題? – btlog 2010-03-05 14:19:42

+0

@btlog直到最新版本之前,Excel/Mac仍然與Excel/Windows相當不同。我不知道Mac版本中的HTML導入代碼是否窒息。看看另一個程序是否喜歡HTML,會發現問題是Excel/Windows是否過於自由,或者Excel/Mac太嚴格。 – Broam 2010-03-05 18:37:38

回答

0

一般來說,依靠Excel解析你的Html是非常脆弱的。正如您發現的那樣,Mac版Excel可能會遇到麻煩。相反,您應該考慮使用Spreadsheet XML或NPOI等引擎來構建實際的Excel文件。

無論是Html文檔還是Xml文檔,Excel都會向用戶發出警告,告知用戶從網絡上下載的文件與預期的文件類型不同(例如,您有一個Html文件聲稱是Excel基於內容類型的文件)。如果您構建實際的Excel文件,則不會出現兼容性問題,用戶也不會收到此警告。

Spreadsheet XML

NPOI

0

嗯,你可以這樣做......

 Response.Clear(); 
     Response.AddHeader("content-disposition", "attachment;filename=report.xls"); 
     Response.Charset = ""; 
     Response.ContentType = "application/vnd.xls"; 
     var stringWrite = new System.IO.StringWriter(); 
     var htmlWrite = new HtmlTextWriter(stringWrite); 
     this.gridView.RenderControl(htmlWrite); 
     Response.Write(stringWrite.ToString()); 
     Response.End(); 
+0

簡而言之,我完全是這樣做的,我使用這些代碼進行測試,並且html在Excel 2008上呈現,僅在某些計算機上的Excel 2008中呈現。 – VictorV 2010-03-05 14:49:40

0

的Excel 2008 for Mac不具有VBA。如果沒有VBA環境,我不知道C#加載項(我認爲這是什麼)運行。我知道我的VBA文件和加載項沒有什麼可以完成的。

+0

我有些mac的excel文件顯示ok。 – VictorV 2010-03-06 00:43:29

+0

他們是否也有Excel 2008?因爲Excel 2004(用於Mac)具有VBA支持。 – guitarthrower 2010-03-06 01:12:49