[英]Asp.Net File Stream IO Exception : Cannot access the file because the process is being used by another process
I have a problem about one of my Asp.Net applications. 我有一个关于我的Asp.Net应用程序的问题。 I am creating an excel file from a DataTable that I use.
我正在从使用的DataTable创建一个excel文件。 Here is the related function :
这是相关的功能:
public static void ExportToExcel(System.Data.DataTable Tbl, string ExcelFilePath)
{
try
{
if (Tbl == null || Tbl.Columns.Count == 0)
throw new Exception("ExportToExcel: Null or empty input table!\n");
// load excel, and create a new workbook
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Workbooks.Add();
// single worksheet
Microsoft.Office.Interop.Excel._Worksheet workSheet = excelApp.ActiveSheet;
// column headings
for (int i = 0; i < Tbl.Columns.Count; i++)
{
workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
}
// rows
for (int i = 0; i < Tbl.Rows.Count; i++)
{
// to do: format datetime values before printing
for (int j = 0; j < Tbl.Columns.Count; j++)
{
workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
}
}
// check fielpath
if (ExcelFilePath != null && ExcelFilePath != "")
{
try
{
workSheet.SaveAs(ExcelFilePath);
excelApp.Quit();
}
catch (Exception ex)
{
throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
+ ex.Message);
}
}
else // no filepath is given
{
excelApp.Visible = true;
}
}
catch (Exception ex)
{
throw new Exception("ExportToExcel: \n" + ex.Message);
}
}
Than I need to send this 'new created' excel file to the user's computer. 比我需要将此“新创建的” excel文件发送到用户的计算机。 The user downloads this file.
用户下载此文件。 And here is the 'download function' :
这是“下载功能”:
public void FileDownload(string fileName)
{
Session["dosya"] = fileName;
string dosyaUrl = @Server.MapPath("/") + Session["dosya"].ToString() + ".xlsx"; //buradan sonra dosyamızı indirme işlemine başlıyoruz.
string yeni_dosya = Session["dosya"].ToString() + ".xlsx"; //biz işlem yapılacak dosyanın adını sessina attık oyle kullandık siz istediğiniz yolla yapabilirsiniz.
FileStream fs;
if (File.Exists(dosyaUrl))
{
fs = new FileStream(dosyaUrl, FileMode.Open, FileAccess.Read, FileShare.Read);
}
else
{
return;
}
byte[] buffer = new byte[(int)fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
fs.Flush();
fs.Close();
fs.Dispose();
Response.Clear();
Response.AddHeader("Content-Length", buffer.Length.ToString());//içeriğin uzunluğu AddHeader fonksiyonuna gonderiliyor...
Response.AddHeader("Content-Disposition", "attachment; filename=" + yeni_dosya);
Response.BinaryWrite(buffer);
File.Delete(dosyaUrl);
Response.End();
}
And this two functions is controlling by a button on my web page. 这两个功能是由我网页上的按钮控制的。
When the code line comes to the line 当代码行到达该行时
fs.Read(buffer, 0, (int)fs.Length);
I am getting the exception : 我得到了例外:
IO Exception was unhandled by the user code : Cannot access the file because the process is being used by another process IO异常未由用户代码处理:无法访问文件,因为该进程正在被另一个进程使用
I don't understand the cause of the problem. 我不明白问题的原因。 Cause I am saving and closing the file.
原因我正在保存并关闭文件。 And on the task manager I can't see a task about an excel file.
而且在任务管理器上,我看不到有关excel文件的任务。 And the exception is not handling when I got a breakpoint on the function lines and proceed the code line by line.
而且,当我在功能行上遇到断点并逐行处理代码时,异常无法处理。
So what can be the problem ? 那么可能是什么问题呢?
You should not attempt to automate Office in a server application . 您不应尝试在服务器应用程序中自动执行Office 。
In your case, you'll probably find an orphaned Excel process is running, and has the workbook open. 就您而言,您可能会发现一个孤立的Excel进程正在运行,并且已打开工作簿。
I'd suggest you use a third party component such as EPPlus or Aspose to create the workbook. 我建议您使用第三方组件(例如EPPlus或Aspose)来创建工作簿。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.