使用Delphi从Excel工作表中提取图像

问题描述 投票:-7回答:1

我有一张Excel表格,在几个单元格中,我在单元格的左上角有一张图片。这些图片表现得好像它们“附着”在给定的单元格上,因为如果我改变单元格的边界,它的图像随之移动。

如何使用Delphi提取这些图片并将其保存到文件中?

excel image delphi extract
1个回答
2
投票

更新#4 OP迟迟未提供说明,以准备他试图提取的图片示例:

1)转到nbbclubsites.nl/club/8000/uitslagen 2)点击“TKDmm,ronde 1 [1]”3)点击-14- 13/3 4)Clik on“BC Den Dungen-1”5)选择de 4和hearts symbol 6)复制Ctrl + C 7)打开Excel并选择单元格(1,1)8)过去Ctrl + V在单元格中,您在单元格中看到4,心脏符号锁定在左上角

我做了这个,心符号粘贴到我的工作表中没有任何问题。在这之后,项目SavePicture中的1 Insert Picture方法正确地提取并将心脏符号保存到磁盘作为.Jpg文件。卫生署!

更新#3回答此问题的一个问题是,没有关于如何插入OP电子表格中的图片的信息。到目前为止,已经确定了三种不同的方法:

  • 使用插入 - Excel的插入选项卡中的图片
  • 使用Insert - Excel的“插入”选项卡中的对象
  • 使用所选单元格的上下文菜单中的“插入注释”

下面我将展示每种方法的代码示例。

1.插入 - 图片

procedure TForm1.InsertPicture;
begin
  Worksheet.Pictures.Insert('C:\Users\ma\Pictures\photo-2.JPG');
end;

procedure TForm1.SavePicture;
var
  Picture : OleVariant;
begin
  Picture := Worksheet.Pictures[1];
  Picture.Select;
  Picture.Copy;
  SaveClipboard;
end;

2.插入 - 对象

procedure TForm1.InsertAsObject;
begin
  WorkSheet.OLEObjects.Add(Filename:='C:\Users\ma\Pictures\wall.bmp', Link :=False,
    DisplayAsIcon:=False).Select;
end;

procedure TForm1.SaveObjectBmp;
var
  Shape : OleVariant;
begin
  Caption := IntToStr(WorkSheet.OleObjects.Count);
  WorkSheet.OLEObjects[1].Select;
  WorkSheet.OLEObjects[1].CopyPicture;
  Shape := WorkSheet.OLEObjects[1].ShapeRange.Item(1);
  Shape.CopyPicture(xlScreen, xlBitMap);
  SaveClipboard;
end;

3.作为单元格注释插入

procedure TForm1.InsertCommentPicture;
var
  Cell,
  Comment : OleVariant;
begin
  Cell := WorkSheet.Cells.Range['b2', 'b2'];
  Comment := Cell.AddComment;
  Comment.Shape.Fill.UserPicture('C:\Users\ma\Pictures\photo-2.JPG');
  Comment.Visible := True;
end;

procedure TForm1.SaveCommentPicture;
var
  Cell,
  Comment,
  Shape,
  Picture : OleVariant;
begin
  Cell := WorkSheet.Cells.Range['B2', 'B2'];
  Comment := Cell.Comment;
  Comment.Visible := True;

  Shape := Comment.Shape;
  Shape.CopyPicture(xlScreen, xlBitMap);
  SaveClipBoard;
end;

SaveClipBoard方法和FormCreate方法如下所示。 ExcelWorkBookWorkSheet都是该形式的OleVariant成员。

procedure TForm1.SaveClipboard;
// With thanks to the author of http://delphi.cjcsoft.net/viewthread.php?tid=46877
var
  myBitmap: TBitmap;
  myJpegImg: TJpegImage;
  SaveFileName: string;
begin
  Caption := IntToStr(Clipboard.FormatCount)  + ':' + IntToStr(Clipboard.Formats[0]);
  SaveFileName := ExtractFilePath(FileName) + 'Saved.Jpg';
  myBitmap := TBitmap.Create;
  myJpegImg := TJpegImage.Create;
  try
    if Clipboard.HasFormat(cf_Bitmap) then
      begin
        myBitmap.Assign(clipboard);
        myJpegImg.Assign(myBitmap);
        myJpegImg.SaveToFile(SaveFileName);
      end
    else
      ShowMessage('No graphic on the clipboard');
  finally
    myBitmap.FreeImage;
    myJpegImg.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Excel := CreateOleObject('Excel.Application');
  Excel.Visible := True;
  FileName := ExtractFilePath(Application.ExeName) + 'PictureBook.Xlsx';
  WorkBook := Excel.Workbooks.Open(FileName);
  WorkSheet := WorkBook.ActiveSheet;
end;
© www.soinside.com 2019 - 2024. All rights reserved.