平台:Win 7 64位,Matlab R2015a,MS Office Professional Plus 2013
问题:Matlab R2015a - 与MS Excel 2013的COM连接(actxserver)。
以前使用Excel 2007或Excel 2010执行的相同代码现在给出了以下错误。任何帮助解决这个问题的人都表示赞赏。
Error using Interface.000208DB_0000_0000_C000_000000000046/Open
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Open method of Workbooks class failed
Help File: xlmain11.chm
Help Context ID: 0
Error in funWriteExcelAllResults (line 174)
Workbook = Excel.Workbooks.Open(excelOutputFileName);
答案 0 :(得分:0)
在分析我的代码后,我想我找到了原因:
//% Reference to Image Analyst: http://www.mathworks.com/matlabcentral/answers
//% /87487-i-have-excel-2003-and-2010-
//% installed-but-xlsread-fails-to-import-a-simple-xlsx-file
excelVersion = str2double(Excel.Version)
if excelVersion < 12
excelExtension = '.xls';
else
excelExtension = '.xlsx';
end
% Determine the proper format to save the files in.
% It depends on the extension (Excel version).
switch excelExtension
case '.xls' %xlExcel8 or xlWorkbookNormal
xlFormat = -4143;
case '.xlsb' %xlExcel12
xlFormat = 50;
case '.xlsx' %xlOpenXMLWorkbook
xlFormat = 51;
case '.xlsm' %xlOpenXMLWorkbookMacroEnabled
xlFormat = 52;
otherwise
xlFormat = -4143;
end
% File='C:\YourFileFolder\FileName';
File = excelOutputFileName
if ~exist(File,'file')
Workbook = Excel.Workbooks.Add;
Workbook.SaveAs(File, xlFormat);
Workbook.Close(false);
end
Workbook = Excel.Workbooks.Open(excelOutputFileName);
最后一部分是尝试重新打开已经打开的工作簿,并导致错误。我把它改成了:
if ~exist(File,'file')
Workbook = Excel.Workbooks.Add;
Workbook.SaveAs(File, xlFormat);
else
Workbook = Excel.Workbooks.Open(File);
Workbook.Save
end
问题似乎已经解决了。没有Save,我收到以下错误:
Error: The object invoked has disconnected from its clients.
Worksheet = Workbook.Worksheets;
我想知道为什么之前版本的Excel没有给我一个错误?