楼主: lixuewei3
2982 1

[问答] 如何用MATLAB将某个文件夹下的图片存入到Excel的指定位置中 [推广有奖]

  • 6关注
  • 5粉丝

已卖:127份资源

副教授

67%

还不是VIP/贵宾

-

威望
0
论坛币
-330314 个
通用积分
3352.3510
学术水平
20 点
热心指数
38 点
信用等级
24 点
经验
30041 点
帖子
1066
精华
0
在线时间
710 小时
注册时间
2014-1-13
最后登录
2024-4-23

楼主
lixuewei3 发表于 2019-2-22 14:09:48 |AI写论文
100论坛币
如何用MATLAB将某个文件夹下的图片存入到Excel的指定位置中

关键词:MATLAB atlab matla EXCEL xcel

沙发
lixuewei3 发表于 2019-3-5 16:59:05
问题已解决,可以直接用次函数即可:
  1. function addimage = img2xlsx(imagcell, varargin)
  2. % This function adds one or multiple image(s) automatically to a new or
  3. % existing Excel file. Firstly, this function is based on the tutorial of
  4. % the MathWorks Support Team. see: http://tinyurl.com/imgtoxls.
  5. % This method works via ActiveX.
  6. %
  7. % The following parameter has to be filled in to execute this function
  8. % succesfully:
  9. %
  10. % - imagecell = a cell array with the name(s) of the image(s) e.g.
  11. % exampleOne = {image.eps}; or examplemulti = {'image1.eps';'image2.eps'};
  12. %
  13. % varargin input:
  14. %
  15. % 1. img2xlsx(imagcell, name)
  16. % varargin{1} = name). If you have an existing xlsx-file,
  17. % give the the name of the xlsx-file. Default = 'myfile.xlsx' thus creating a
  18. % new xlsx-file.
  19. %
  20. % 2. img2xlsx(imagcell, name, DIR)
  21. % varargin{2} = DIR. Directory where the exising xlsx-file and/or image(s)
  22. % is/are situated. Default = pwd
  23. % IMPORTANT: the image(s), which has to be transfered, should be in this
  24. % directory! Otherwise this function cannot find the image(s). See lines
  25. % 115/116.
  26. %
  27. % 3. img2xlsx(imagcell, name, DIR, sheet)
  28. % varargin{3} = sheet. Select the sheet-number in which the image(s) has to
  29. % be transfered too. If you want to add a sheet, fill in 'Add'. Default = 1.
  30. %
  31. % 4. imgxlsx(imagcell, name, DIR, sheet, dimensions)
  32. % varargin{4} = dimensions. Provide the dimensions of the image(s) in a
  33. % matrix like [LinkToFile,...           (The file to link to.)
  34. %               SaveWithDocument,...    (To save the picture with the document.)
  35. %               Left,...                (The position (in points) of the upper-left corner of the picture relative to the upper-left corner of the document.)
  36. %               Top,...                 (The position (in points) of the upper-left corner of the picture relative to the top of the document.)
  37. %               Width,...               (The width of the picture, in points.)
  38. %               Height]                 (The height of the picture, in points.)
  39. % Default = [0,1,1,1,450,352]
  40. % see: https://msdn.microsoft.com/en-us/library/bb209605(v=office.12).aspx
  41. %
  42. % Version 1.0 06/13/2015 ? H.K.Berg 2015
  43. % -------------------------------------------------------------------------
  44. % If this function errors somewhere and you re-run it, a common error will
  45. % be: READ-ONLY ERROR excel: (e.g. with existing xlsx-file: 'Test.xlsx'
  46. % in directory 'di')
  47. %
  48. % img2xlsx(cell, 'Test.xlsx',di)
  49. % Error using Interface.Microsoft_Excel_14.0_Object_Library._Workbook/SaveAs
  50. % Invoke Error, Dispatch Exception:
  51. % Source: Microsoft Excel
  52. % Description: Cannot access read-only document 'Test.xlsx'.
  53. % Help File: xlmain11.chm
  54. % Help Context ID: 0
  55. % Error in img2xlsx (line 113)
  56. % invoke(Workbook, 'SaveAs', [DIR '\' name]);
  57. %
  58. % SOLUTION: End all Excel processes via the Task Manager and execute this
  59. % function again.
  60. % -------------------------------------------------------------------------
  61. % Default varargin:
  62. name = 'myfile.xlsx';
  63. DIR = pwd;
  64. sheet = 1;
  65. dimensions = [0,1,1,1,450,352];
  66. if nargin > 1
  67.     name = varargin{1};
  68.     if nargin > 2
  69.         DIR = varargin{2};
  70.         if nargin > 3
  71.             sheet = varargin{3};
  72.             if nargin > 4
  73.                 dimensions = varargin{4};
  74.                 if nargin > 5
  75.                     error('myfuns:somefun2Alt:TooManyInputs', ...
  76.                         'function img2xlsx requires at most 4 optional inputs');
  77.                 end
  78.             end
  79.         end
  80.     end
  81. end

  82. % Get handle to Excel COM Server
  83. Excel = actxserver('Excel.Application');
  84. % Add a Workbook to a new excel-file
  85. if strcmp(name,'myfile.xlsx')
  86.     Workbook = invoke(Excel.Workbooks, 'Add');
  87. else % Add a Workbook to existing excel-file
  88.     ResultFile = strcat(DIR, '\', name);
  89.     Workbook = invoke(Excel.Workbooks,'Add', ResultFile);
  90. end
  91. % Add a sheet if sheet = 'Add'
  92. if strcmp(sheet, 'Add')
  93.     Workbook.Worksheets.Add([], Workbook.Worksheets.Item(Workbook.Worksheets.Count));
  94.     sheet = Excel.Sheets.Count;
  95. else
  96. end
  97. % Get a handle to Sheets and select Sheet No
  98. Sheets = Excel.ActiveWorkBook.Sheets;
  99. SheetNo = get(Sheets, 'Item', sheet);
  100. SheetNo.Activate;
  101. % Get a handle to Shapes for Sheet No n
  102. Shapes = SheetNo.Shapes;
  103. % Add image(s - adjacent to each other)
  104. for i = 1:length(imagcell);
  105.     image = char(imagcell(i));
  106.     if i == 1;
  107.         dimensions(3) = i;
  108.     else
  109.         dimensions(3) = ((i-1)*dimensions(5));
  110.     end
  111.     Shapes.AddPicture([DIR '\' image] ,dimensions(1), dimensions(2),...
  112.         dimensions(3), dimensions(4), dimensions(5), dimensions(6));
  113.    Shapes.AddLabel(2, dimensions(3), dimensions(4)+dimensions(6), 20,5).TextFrame.Characters.Text = "Test Label";
  114.        end
  115. xlswrite([DIR '/' name],'123');
  116. %Save and Quite Excel file
  117. invoke(Workbook, 'SaveAs', [DIR '\' name]);
  118. invoke(Excel,'Quit');
  119. delete(Excel);
  120. addimage = 'image(s) added succesfully';
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-3 23:36