我有一个按钮,可将来自telerik的RadGrid的数据导出到Excel文档中。
但是,当我尝试导出时遇到以下错误:
[System.Threading.ThreadAbortException]:
{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
这是我的代码:
private void GenerateFile(object structure, string fileName)
{
Workbook workbook = structure as Workbook;
var formatProvider = new XlsxFormatProvider();
try
{
using (MemoryTributary ms = new MemoryTributary())
{
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
formatProvider.Export(workbook, ms);
ms.Position = 0;
ms.WriteTo(Response.OutputStream);
Response.End();
}
}
catch (System.OutOfMemoryException ex)
{
}
}
}
我根据我在网上找到的处理相同问题的帖子尝试了以下解决方案,但没有一个解决方案有效:
以上解决方案均未解决此问题。有关如何解决此错误的任何进一步建议非常感谢。
感谢。
答案 0 :(得分:1)
通过调用Response.End导致异常。 <%= button_tag(type: "submit", class: "new_post_submit") do %>
<span><i class="glyphicon glyphicon-pencil"></i> Post</span>
<% end %>
记录的行为是刷新缓冲区并通过中止立即中止当前请求。它没有在.NET编程中使用。它只与旧的ASP代码兼容。
只需从代码中删除<button type="submit" class="new_post_submit">
<span><i class="glyphicon glyphicon-plus"></i> Post</span>
</button>
您还应该删除End
声明。应该调查和修复例外,而不是掩盖。 OutOfMemoryException意味着导致泄漏的代码存在严重错误。它可能是因为内存不足或内存碎片太多导致.NET无法分配足够大的块