Hướng dẫn matlab close excel file

try

% See if there is an existing instance of Excel running.

% If Excel is NOT running, this will throw an error and send us to the catch block below.

Excel = actxGetRunningServer('Excel.Application');

% If there was no error, then we were able to connect to it.

Excel.Quit; % Shut down Excel.

catch

% No instance of Excel is currently running.

end

Hướng dẫn matlab close excel file

  • Direct link to this question

 ⋮ 

  • Direct link to this question

I want to use writetable() for an Excel-file in a for-loop. But at the second loop it stops with an error, because the first file is still open and there is no permission. Before I'd open a new folder for the new excel-file, but it doesn't work in this way now, because the file is still open.

Accepted Answer

Hướng dẫn matlab close excel file

  • Direct link to this answer

 ⋮ 

  • Direct link to this answer

I got this from the Mathworks directly:

You can use the following SYSTEM command via MATLAB to kill the process:

>> system('taskkill /F /IM EXCEL.EXE');

  4 Comments

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

Many thx. It works perfect! :-)

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

You're welcome. Can you go ahead and "Accept this answer" then? Thanks in advance.

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

Hi Image Analyst,

instead of closing excel completely with unwished consequences I've found a similiar command like following which only is closing the specific spreadsheet:

system('taskkill /F /FI "WINDOWTITLE eq xy*"');

But once more thanks for your hint, because this was the way to solve it!

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

I created an excel file named Comparison.xlsx. How do I only close that excel uding this command?

Sign in to comment.

More Answers (2)

Hướng dẫn matlab close excel file

  • Direct link to this answer

 ⋮ 

  • Direct link to this answer

Now I've an addtional problem. In a for-loop I've to use wait, until the batch is finished, otherwise it doesn't work in they way I want to have. And it's closing "all" excelfiles.

So now I'm looking for a function to close only the specific file after writing.


Hướng dẫn matlab close excel file

  • Direct link to this answer

 ⋮ 

  • Direct link to this answer

Use table2cell() to create a cell array, and usexlswrite1() from the File Exchange, as that gives you an interface in which you have specific control over closing the spreadsheet.

  5 Comments

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

Okay and thanks. I've already used xlswrite, but I'ven't seen any options or parameter to control closing excel-sheet!

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

You can deduce it from the Description -- you would invoke the workbook save function when you were finished writing, and then invoke the workbook close function to close that connection. Eventually you tell the excel session to quit, after you have written out all the files.

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

  • ExcelDemo.m

% Open Excel

Excel = actxserver('Excel.Application');

excelWorkbook = Excel.workbooks.Open(excelFullFileName);

Excel.visible = true; % Make Excel appear so we can see it, otherwise it is hidden.

% Call xlswrite1().....

% Save the workbook

Excel.ActiveWorkbook.Save();

% Close the workbook.

excelWorkbook.Close(false);

% Shut down Excel.

Excel.Quit;

See attached demo.

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

I am getting the following error:

Undefined function or variable 'workbooks'

Hướng dẫn matlab close excel file

Direct link to this comment

 ⋮ 

  • Link

    Direct link to this comment

  • ExcelDemo.m
  • Excel_put_formula_into_cell.m

You must have changed the file. There is no workbooks variable in the attached function. You forgot to give the complete error which included the line of code so I don't know how you modified the function. Nevertheless, that demo is from 4 years ago and I have a new one, which I'm attaching here. And I just ran this so I know it works.

Plus I attached a second, separate demo showing how to put a formula into an Excel cell.

Sign in to comment.

See Also

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.