我需要将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