如何读取excel文件中的多个图片

Description of your first forum.

如何读取excel文件中的多个图片

帖子yfoxyfox » 星期五, 2009年7月17日 12:06


delphi读取excel文件,文件中包含多个图片,如何处理?

var
  i, j, idx: Integer;
  AExcel, ASheet: Variant;
  sText:  string;
begin
  try
    AExcel := CreateOleObject('Excel.Application');
    try
      AExcel.DisplayAlerts := False;
      AExcel.Workbooks.Open('E:\Temp\test.xls');
      for i := 1 to AExcel.Workbooks.Count do begin
        ASheet := AExcel.Worksheets[i];
//读图片(Pictures)
        for j := 1 to ASheet.Pictures.Count do begin //看不到count数量
          ASheet.Pictures[j].Copy;               //虽然有多个图片,但是j>1时报错
          if Clipboard.HasFormat(CF_PICTURE) then begin
            Clipboard.Open;
            try
              Image1.Picture.Assign(Clipboard);  //借助于剪贴板
            finally
              Clipboard.Close;
            end;
          end;
        end;

      end;
    finally
      AExcel.Quit;
      AExcel := Unassigned;
    end;
  except
    on E: Exception do
      ShowMessage(E.Message);
  end;
 
 
 

如何读取excel文件中的多个图片

帖子japhe » 星期四, 2009年8月20日 16:13


你的代码在我这里运行没有问题(d2006+excel2007)
不过,你可以试下下面的代码。
for j := 1 to ASheet.Shapes.Count do begin
  ASheet.Shapes.Item(j).Copy;              
或者
for j := 1 to ASheet.Pictures.Count do begin
  ASheet.Pictures.Item(j).Copy;