Writing MATLAB image to Excel file at a specific position
70 次查看(过去 30 天)
显示 更早的评论
I have a function like [summary,image1,image2,image3] = funca(x,y). I wonder how I can write each image to a separate sheet in an Excel workbook at a specific position. Thanks for your comments!
0 个评论
回答(2 个)
Image Analyst
2012-6-27
MATLAB can't do this directly. You'd need to use ActiveX programming to call Excel API functions. Fortunately MATLAB can do that (make ActiveX calls). I don't recall doing that (stuffing images into Excel) so I don't have any demo code for you.
It's going to look somewhat similar to this:
% Open Excel file.
objExcel = actxserver('Excel.Application');
objExcel.Workbooks.Open(fullfile(excelFilePath, excelFileName)); % Full path is necessary!
oWB = objExcel.Workbooks.Add();
oSheet = oWB.ActiveSheet;
oSheet.Range("A1").Select;
oSheet.Pictures.Insert("<< full path to image file >>").Select();
But you can look up ActiveX in Answers or elsewhere. Here's one example:
The Microsoft Excel methods you need are listed here:
This code may also be useful:
3 个评论
Morteza
2018-10-23
I faced an error during compiling the above code: "Undefined function or variable 'Pictures'"
Image Analyst
2018-10-24
Strange - that is the command that gets recorded if you record a macro. But I was also able to reproduce the error. Not sure why it errored out but I found an alternate way. I've tested this and it works:
% Get the name of the workbook you want to paste a picture into.
folder = pwd;
excelFileName = 'PicturePaste.xlsx';
fullFileName = fullfile(folder, excelFileName);
if ~exist(fullFileName, 'file')
message = sprintf('Existing Excel workbook not found"\n%s', fullFileName);
uiwait(errordlg(message));
return;
end
% Open Excel as an ActiveX server.
objExcel = actxserver('Excel.Application');
objExcel.Visible = true;
% Open the workbook we want to paste the image onto.
ExcelWorkbook = objExcel.Workbooks.Open(fullFileName); % Full path is necessary!
% ExcelWorkbook = objExcel.Workbooks.Add(); % Add new, blank workbook.
oSheet = objExcel.ActiveSheet;
% oSheet.Range('A1').Select;
% Get the name of the image file.
imageFolder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
imageFullFileName = fullfile(imageFolder, 'cameraman.tif')
% Get a handle to Shapes for Sheet 1
Shapes = oSheet.Shapes;
% Add image by importing one from an image file on disk.
Shapes.AddPicture(imageFullFileName, 0, 1, 400, 18, 300, 235);
% Save the workbook.
% Tell it to not wait and pop up alerts like "This file exists. Do you want to overwrite it."
objExcel.DisplayAlerts = false;
% Save this workbook we just created to disk. Image will be saved with the workbook.
ExcelWorkbook.SaveAs(fullFileName);
% Close the workbook. Excel will still be open though.
ExcelWorkbook.Close(false);
objExcel.Quit; % Shut down Excel.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!