【问题标题】:Matlab R2015a COM connection (actxserver) with MS Excel 2013Matlab R2015a COM 连接(actxserver)与 MS Excel 2013
【发布时间】:2015-06-19 00:53:23
【问题描述】:

平台:Win 7 64 位、Matlab R2015a、MS Office Professional Plus 2013

问题:Matlab R2015a - COM 连接 (actxserver) 与 MS Excel 2013。

以前使用 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);

【问题讨论】:

    标签: excel matlab


    【解决方案1】:

    分析我的代码后,我想我找到了原因:

    //% 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
    

    问题似乎解决了。没有保存,我收到以下错误:

    Error: The object invoked has disconnected from its clients.
    Worksheet = Workbook.Worksheets;
    

    我想知道为什么以前的 Excel 版本没有给我错误?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-28
      • 2010-10-05
      • 2012-12-05
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 2016-01-18
      相关资源
      最近更新 更多