在 MATLAB 中将图像插入 excel 单元格

Insert an image into a excel cell in MATLAB

我正在关注 this post,我需要做同样的事情,只是我想将图像(m x n x 3 矩阵)放入 excel 中的单元格中。

此行不起作用,因为我的图像 im 是矩阵而不是句柄:

print(im, '-dbitmap');

我是否需要以某种方式为图像创建句柄?有another\better的方法吗?

最后我想更改单元格,使其适合图像(不改变图像的大小)。

print statement prints the contents of a figure window 到一个文件,所以你必须先绘制你的图像:

image(im);                        % Plot image
set(gca, 'Visible', 'off', ...    % Turn off axes visibility
         'Position', [0 0 1 1]);  %   and make axes fill figure window
hFigure = gcf;                    % Get handle to figure
pos = get(hFigure, 'Position');   % Get current figure position
set(hFigure, 'Position', [pos(1:2) size(im, 2) size(im, 1)]);  % Set position so image
                                                               %   is scaled properly

然后您可以 create a COM server 并将图形打印到 Excel 文件,如下所示:

excel = actxserver('Excel.Application');  % Create server object
excelWorkbook = excel.Workbooks.Add(1);   % Add a workbook
excelSheet = excel.ActiveSheet;           % Get the active sheet

dpi = get(groot, 'ScreenPixelsPerInch');  % Get screen dpi
print(hFigure, sprintf('-r%d', dpi), ...  % Print the figure at the screen resolution
      '-clipboard', '-dbitmap');          %   to the clipboard as a bitmap
excelSheet.Range('B2').PasteSpecial();    % Paste from clipboard (top left corner
                                          %   of image will be in the cell 'B2')

excelSheet.Range('B2').RowHeight = ...    % Set cell height to image height
  excelSheet.Shapes.Item(1).Height;
widthScale = excelSheet.Range('B2').ColumnWidth./...  % Column width (in characters)
             excelSheet.Range('B2').Width;            % Column width (in points)
excelSheet.Range('B2').ColumnWidth = ...  % Set cell width to scaled image width
  excelSheet.Shapes.Item(1).Width.*widthScale;

excelWorkbook.SaveAs('figtest.xlsx');  % Save workbook to a file
excelWorkbook.Close();                 % Close workbook
excel.Quit();                          % Quit server
excel.delete();                        % Delete server object

以上尝试缩放单元格以适合整个图像。这适用于行高,但由于某种原因列宽关闭。确定适当的缩放比例似乎有很多困难,因为列宽是根据字符定义的,而图像宽度是根据 points/inches 定义的。我不确定是否有好的解决方法。

例如,示例 MATLAB 图像的结果如下所示 'peppers.png':

我从 gnovice's 那里借用了一些代码。这对我写这个答案真的很有帮助。


这是一个在单元格 B2 中插入 peppers.png 的工作示例:

im=imread('peppers.png');
imshow(im);

dpi = get(groot, 'ScreenPixelsPerInch');  % Get screen dpi
print(gcf, sprintf('-r%d', dpi), ...      % Print the figure at the screen resolution
      '-clipboard', '-dbitmap');          %   to the clipboard as a bitmap

excel = actxserver('Excel.Application');  % Create server object
excelWorkbook = excel.Workbooks.Add(1);   % Add a workbook
excelSheet = excel.ActiveSheet;           % Get the active sheet
excelSheet.Range('B2').PasteSpecial();    % Paste from clipboard (top left corner
                                          %   of image will be in the cell 'B2')   
%%%%%%%%%%%%%%%% My contribution %%%%%%%%%%%%%%%%
excelSheet.Shapes.Item(1).LockAspectRatio='msoFalse';            %Unlocking aspect ratio
excelSheet.Shapes.Item(1).Width=excelSheet.Range('B2').Width;    %Adjusting width
excelSheet.Shapes.Item(1).Height=excelSheet.Range('B2').Height;  %Adjusting height
%Uncomment the next line if you want the cell to keep the image fit in its particular 
%cell even if the size of the cell is changed later
% excelSheet.Shapes.Item(1).Placement='xlMoveandSize';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

excelWorkbook.SaveAs('figtest.xlsx');     % Save workbook to a file
excelWorkbook.Close();                    % Close workbook
excel.Quit();                             % Quit server
excel.delete();                           % Delete server object

输出: