簡體   English   中英

Asp.Net文件流IO異常:由於另一個進程正在使用該進程,因此無法訪問文件

[英]Asp.Net File Stream IO Exception : Cannot access the file because the process is being used by another process

我有一個關於我的Asp.Net應用程序的問題。 我正在從使用的DataTable創建一個excel文件。 這是相關的功能:

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);
            }
        } 

比我需要將此“新創建的” excel文件發送到用戶的計算機。 用戶下載此文件。 這是“下載功能”:

   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();

        }

這兩個功能是由我網頁上的按鈕控制的。

當代碼行到達該行時

fs.Read(buffer, 0, (int)fs.Length);

我得到了例外:

IO異常未由用戶代碼處理:無法訪問文件,因為該進程正在被另一個進程使用

我不明白問題的原因。 原因我正在保存並關閉文件。 而且在任務管理器上,我看不到有關excel文件的任務。 而且,當我在功能行上遇到斷點並逐行處理代碼時,異常無法處理。

那么可能是什么問題呢?

不應嘗試在服務器應用程序中自動執行Office

就您而言,您可能會發現一個孤立的Excel進程正在運行,並且已打開工作簿。

我建議您使用第三方組件(例如EPPlus或Aspose)來創建工作簿。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM