我需要將gridview導出爲excel,我將從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();
}
嘗試攝入這個HTML樣本程序的一個較大的量。 OpenOffice.org將成爲我的第一個考生。 – Broam 2010-03-05 14:12:34
@Broam這是如何幫助Excel Mac OS的問題? – btlog 2010-03-05 14:19:42
@btlog直到最新版本之前,Excel/Mac仍然與Excel/Windows相當不同。我不知道Mac版本中的HTML導入代碼是否窒息。看看另一個程序是否喜歡HTML,會發現問題是Excel/Windows是否過於自由,或者Excel/Mac太嚴格。 – Broam 2010-03-05 18:37:38