独立的excel从C#

我出口我的HTML像这样的excel:

// our html datas // you can use foreach for repating sTEXT = @"<table class='table'><thead>"; sTEXT += @"<th>"; sTEXT += @"Caption A"; sTEXT += @"</th>"; sTEXT += @"</thead>"; sTEXT += @"<tr>"; sTEXT += @"<td>abcdef</td>"; sTEXT += @"<td>ghjklm</td>"; sTEXT += @"</tr>"; sTEXT += @"</table>"; // our screen literaldata sets lblData.Text = sTEXT; // excel output code // carefull end of the "Responce.Write"" sentence Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254"); Response.Charset = "windows-1254"; Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls"); Response.Buffer = true; this.EnableViewState = false; Response.ContentType = "application/vnd.ms-excel"; Response.Write("<style> TABLE { border: 1px solid gray; } TD { border: 1px solid gray; } </style> " + lblData.Text); Response.End(); 

在excel中一切都很好,没有错误。

但是,当您将此导出的Excel文件保存到另一个位置(d:\,桌面…)时,Excel会收到此错误“missing file:… html”。 在这种情况下,我发现这个方法是创build一些其他的目录和HTML文件。 结果:excel文件不是独立的excel文件。

所以我想要一个独立的Excel文件。 我需要你的build议?

你有没有考虑过使用Open XML SDK ? 我已经在MVC应用程序中使用它将MVC视图转换为Excel。 这是一个这样的实现的例子