【问题标题】:Editing and Saving Excel file from Matlab从 Matlab 编辑和保存 Excel 文件
【发布时间】:2013-12-12 15:30:23
【问题描述】:

我需要在 Matlab 中生成一个代码来编辑,然后保存一个 Excel 文件。代码必须执行以下操作:

  1. 打开 Excel 文件

  2. 转到第二张表 (sheet2)

  3. 更改单元格A2的值

  4. 转到第一张工作表 (sheet1)

  5. 将 sheet1 保存为制表符分隔的文本文件 (.txt)

表 1 包含依赖于 sheet2 中的单元格 A2 的单元格(对于 sheet2-A2 的不同值,我需要多次执行此操作,这就是我需要对其进行编码的原因)。

浏览网页,我已经能够编写步骤 1-4 的代码(参见下面的代码)。但是,我一直无法找到执行 5 的方法。非常感谢您的帮助。谢谢!!


% Open Excel Server:    
Excel = actxserver('Excel.Application');     

% Makes Excel visible in the screen:    
set(Excel, 'Visible', 1);     

% Open Excel file:    
Workbooks = Excel.Workbooks.Open('E:\TEST.xlsx');     

% Make the second sheet active:    
Sheets = Excel.ActiveWorkBook.Sheets;    
sheet2 = get(Sheets, 'Item', 2);    
invoke(sheet2, 'Activate');    

% Get a handle in the active sheet:    
Activesheet = Excel.Activesheet;


% Edit cell A2:    
A = 2;    
ActivesheetRange = get(Activesheet,'Range','A2');    
set(ActivesheetRange, 'Value', A);    

pause(5) %Pauses the code for 5 seconds (so sheet 1 updates the formulas)


% Going to Sheet1 where the focal data is:    
Sheets = Excel.ActiveWorkBook.Sheets;    
sheet1 = get(Sheets, 'Item', 1);    
invoke(sheet1, 'Activate');

【问题讨论】:

  • 不会有sheet2 == Activesheet 吗?如果你让它可见,你可能想要硬编码工作簿的句柄而不是使用Excel.ActiveWorkBook,因为一旦用户在你没有完成的情况下打开另一个工作簿,这将失败。

标签: excel matlab


【解决方案1】:

这类东西,你可以直接去微软文档。

这些应该可以帮助你:

http://msdn.microsoft.com/en-us/library/office/ff841185.aspx

http://msdn.microsoft.com/en-us/library/office/ff198017.aspx

基本上,您可以在此处发出命令,就像在 Excel GUI 中的 SaveAs 对话框中一样。

【讨论】:

  • 谢谢!!!有效。第 5 点的命令是:Activesheet.SaveAs(name,20); %20 是第二个链接中给出的制表符分隔文本文件的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多