MATLAB failing to read/edit Excel file on first-time code run
조회 수: 37 (최근 30일)
이전 댓글 표시
I am attempting to have MATLAB create and then modify an excel file, to format the cells so they're easier to read. Below is my code, which specifies what will fill cells A1 to B4, and then is meant to format them so that the cells fit the strings inside them (and center those strings). However, when I run the code, I receive the following error:
Error using Interface.000208DB_0000_0000_C000_000000000046/Open
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Sorry, we couldn't find Output.xlsx. Is it possible it was moved, renamed or deleted?
Help File: xlmain11.chm
Help Context ID: 0
Error in readandwrite (line 113)
hWorkbook = hExcel.Workbooks.Open(outputfile);
This error only occurs on the first run of the code. After the code has created the file and is run a second time, the error stops occuring. However, I would like the formatting to be completed without needing a second pass of the code. As well, in previous iterations of my code, I did not have this issue—this is a new problem that has appeared. Thank you for the help.
outputfile = 'Output.xlsx';
worksheet = 'Sheet1';
sheetBasics = {
'Value' 'Description';
'1' 'Example 1';
'2' 'Example 2';
'3' 'Example 3';
};
xlswrite(outputfile,sheetBasics,worksheet);
% formatting the cells
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(outputfile);
hWorksheet = hWorkbook.Sheets.Item(1);
hExcel.Cells.Select; % select entire spreadsheet
hExcel.Cells.EntireColumn.AutoFit; % auto fit all the columns
hExcel.Selection.VerticalAlignment = 2; % vertically align all columns
hExcel.Range('A1:A4').Select; % select first column
hExcel.Selection.HorizontalAlignment = 3; % center+vertical align first column
hExcel.Selection.VerticalAlignment = 2;
hExcel.Range('A1:B1').Select; % select first row
hExcel.Selection.HorizontalAlignment = 3; % center+vertical align first row
hExcel.Selection.VerticalAlignment = 2;
hExcel.Range('A1').Select; % Put "cursor" or active cell at A1, the upper left cell
hWorkbook.Save;
hWorkbook.Close;
hExcel.Quit;
댓글 수: 0
채택된 답변
kei hin
2023년 8월 8일
try full path, like
hWorkbook = hExcel.Workbooks.Open([pwd,'\',outputfile])
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!