gpt4 book ai didi

c# - 错误无法评估表达式,因为代码已优化

转载 作者:行者123 更新时间:2023-12-03 07:49:49 24 4
gpt4 key购买 nike

我想用 StreamWriter 导出 excel。
但错误:无法评估表达式,因为代码已优化或 native 框架位于调用堆栈顶部。

代码 C#:

private void ExportToExcel()
{
string filePath = string.Empty;
try
{
if (fn.CheckRowOnDataTable(dtExport))
{
string path = Server.MapPath(@"~/EXCEL/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

string fileName = string.Format("{0}_{1}.xls", this.PROGRAM_ID, DateTime.Now.ToString("yyyyMMdd_HHmmss"));
filePath = string.Format("{0}{1}", path, fileName);

if (File.Exists(filePath))
{
File.Delete(filePath);
}

using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(GenerateExcel());
}

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("filename={0}", fileName));
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.WriteFile(filePath); <<< Line Error
HttpContext.Current.Response.End();
}
else
{
.....
}
}
catch (OverflowException e)
{
.....
}
catch (Exception ex)
{
.....
}
}

如何解决这个问题?
谢谢提前;)

最佳答案

试试这个:代替HttpContext.Current.Response.End()使用 HttpContext.Current.ApplicationInstance.CompleteRequest()

关于c# - 错误无法评估表达式,因为代码已优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36789069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com