Writing an excel sheet through MATLAB, then adding that sheet to an exisiting excel file (microsoft office 97-03 (.xls))
12 次查看(过去 30 天)
显示 更早的评论
I have tried many, many different things. For further context, the excel workbook I am trying to add to contains macros and it has several sheets on it already. The closest I feel I have came to doing so was this code I found on a redditt forum:
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open('C:\Users\user\OneDrive - User\Verification\Results.xls\',0,false);
sheet = workbook.Sheets.Item(4);
sheet.Activate();
range = sheet.Range('D1:D4');
range.Value = {'cool','this','actually','works'};
workbook.Save();
workbook.Close();
But this results in an error:
Error using Interface.000208DB_0000_0000_C000_000000000046/Open
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Sorry, we couldn't find C:\Users\user\OneDrive - User\Verification\Results.xls\. Is it possible it was moved, renamed or deleted?
Help File: xlmain11.chm
Help Context ID: 0
2 个评论
Walter Roberson
2024-1-9
workbook = excel.Workbooks.Open('C:\Users\user\OneDrive - User\Verification\Results.xls\',0,false);
I doubt that there should be the final \ in the Open.
workbook = excel.Workbooks.Open('C:\Users\user\OneDrive - User\Verification\Results.xls',0,false);
采纳的回答
Voss
2024-1-9
Have you tried this?
C = {'cool','this','actually','works'};
filename = 'C:\Users\user\OneDrive - User\Verification\Results.xls';
sheet = 4;
writecell(C,filename,'Sheet',sheet)
3 个评论
Voss
2024-1-10
"This does work"
Great! I'm glad to hear it.
"considering the simplicity of the example I provided I will accept this as an answer"
Thank you for (reluctantly) accepting my answer.
"I would need your variable "C" to be a large pre-exisiting xls file."
C is a cell array, not a file.
"Currently resulting to writing an xls file then using "readmatrix" to pull the sheet into matlab and outputting it using "writecell" onto my "Results.xls""
If you read the sheet using readmatrix, it seems like you would use writematrix to write it, not writecell, unless you do some processing which you haven't mentioned which makes the data into a cell array.
"If you have a better idea I am all ears!"
Maybe I would have an idea if I understood what the problem was. I gather you want to add a sheet to an existing xls file. Do I have that much correct? If so, note that you can use writecell/writematrix/writetable to add a sheet to an existing xls file (whether the existing file contains macros is immaterial). Example:
% existing file
filename = 'test.xls';
% the file contains one sheet
sn = sheetnames(filename)
% which contains this stuff
readcell(filename)
% now make a new sheet in the file
writecell({'here' 'is'; 'some' 'new';'stuff' '!'},filename,'Sheet',2)
% now the file contains two sheets
sn = sheetnames(filename)
% the first sheet still contains this stuff
readcell(filename,'Sheet',1)
% and the second sheet contains the new stuff
readcell(filename,'Sheet',2)
If I am missing some aspect of the task or the challenge you are facing, please clarify. In particular, does using writecell to create a new sheet in an existing xls file answer your question satisfactorily or not? If not, what's the problem?
更多回答(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!