Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关闭EXCEL.EXE*32进程_C# - Fatal编程技术网

C# 关闭EXCEL.EXE*32进程

C# 关闭EXCEL.EXE*32进程,c#,C#,每次我运行它,我都会得到新的进程EXCEL.EXE*32。如何在从Excel获取信息后关闭它 FileStream fileStream; string temp = "D:\\Temp"; string file = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); string path = Path.Combine(temp, file + ".xslx")

每次我运行它,我都会得到新的进程EXCEL.EXE*32。如何在从Excel获取信息后关闭它

        FileStream fileStream;
        string temp = "D:\\Temp";
        string file = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
        string path = Path.Combine(temp, file + ".xslx");
        using (fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
        {
            memoryStream.WriteTo(fileStream);
        }

        Excel.Application excelApplication;
        Excel.Workbook excelWorkbook;
        Excel.Worksheet excelWorkSheet;
        Excel.Range excelRange;
        Excel.Range excelCheckRange;
        DataTable dataTable = null;

        try
        {
            excelApplication = new Excel.Application();
            excelWorkbook = excelApplication.Workbooks.Open(path, ReadOnly: true);
            excelWorkSheet = (Excel.Worksheet)excelWorkbook.Sheets[1];
        }
        catch(Exception ex) 
        {
            //TO DO
        }

在工作结束时,您应该使用

if (excelWorkbook != null)    
  excelWorkbook.Close();
if(excelApplication != null)
  excelApplication.Quit();

在工作结束时,您应该使用

if (excelWorkbook != null)    
  excelWorkbook.Close();
if(excelApplication != null)
  excelApplication.Quit();
这确实会启动Excel

要关闭该过程,您需要确保没有对Excel应用程序对象或其任何其他部分的引用(例如a
范围
)。然后GC将最终清除它。您可以使用加快速度(但请注意文档中关于未保存工作簿的说明)

这确实会启动Excel


要关闭该过程,您需要确保没有对Excel应用程序对象或其任何其他部分的引用(例如a
范围
)。然后GC将最终清除它。您可以使用加快速度(但请注意文档中关于未保存工作簿的说明)。

请注意,OP以只读模式打开工作簿,因此未保存的工作簿不会出现问题:)请注意,OP以只读模式打开工作簿,因此未保存的工作簿不会出现问题:)