如何使用OpenOffice Spreadsheet从Excel文件中获取图像

我有一个代码,从Excel中导出图像到一个图片框,在这里。

Dim appExcel As Object Set appExcel = CreateObject("Excel.Application") appExcel.Visible = False Dim xlsBook As New excel.Workbook Dim xlsSheet As New excel.Worksheet Dim rowlocation As Integer Dim columnlocation As Integer Dim celladdress As String Set xlsBook = appExcel.Workbooks.Open(Text1.Text) Set xlsSheet = xlsBook.Worksheets("Sheet1") Dim x As excel.Shapes For Each x In xlsSheet.Shapes x.Copy Picture1.Picture = Clipboard.GetData(vbCFBitmap) Text2.Text = x.Name rowlocation = x.TopLeftCell.Row columnlocation = x.TopLeftCell.Column celladdress = xlsSheet.Cells(x.BottomRightCell.Row + 1, x.TopLeftCell.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False) MsgBox ActiveSheet.Range(celladdress) Next End If 

不幸的是这个代码不会在我的朋友PC上工作,因为他没有安装Excel,但他有OpenOffice电子表格。 我试图在OpenOffice中打开Excel,然后打开文件,我的目标是如何转换OpenOffice上面的代码? 我的意思是运行OpenOffice文件的代码。

这是我的代码,但它不工作

  Dim objServiceManager As Object Dim objDesktop As Object Dim objDocument As Object Dim objText As Object Dim objCursor As Object Dim oDoc As Object Dim ARG() Dim oGraph As Object Dim oView As Object Dim oDrawPage As Object Dim oSheet As Object Dim Image As System_Drawing.Image Dim oimage As Object Dim osize As Object Set objServiceManager = CreateObject("com.sun.star.ServiceManager") Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop") Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\Testing.ods", "_blank", 0, ARG()) Set oSheet = oDoc.getSheets().getByIndex(0) Set oGraph = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape") Set oView = oDoc.CurrentController Set oDrawPage = oView.getActiveSheet.DrawPage For i = 0 To 2 For j = 0 To 9 ' Form1.Image1.Picture = Clipboard.GetData Form1.Image1.Picture = LoadPicture(oDrawPage) Next Next 

TYSM为未来的帮助

这是VB6中的最新代码,它有一个错误,说vnd.sun.star丢失

  Dim objServiceManager As Object Dim objDesktop As Object Dim objDocument As Object Dim objText As Object Dim objCursor As Object Dim oDoc As Object Dim ARG() Dim oGraph As Object Dim oView As Object Dim oDrawPage As Object Dim oSheet As Object Dim Image As System_Drawing.Image Dim oimage As Object Dim osize As Object Dim Cell As Object Dim sGraphicUrl As String Dim oDisp Dim oFrame Dim opos As Object Set objServiceManager = CreateObject("com.sun.star.ServiceManager") Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop") Set osize = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Size") Set opos = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Point") Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\ACE Express - Fairview_Sample PC of Gondola.ods", "_blank", 0, ARG()) Set oSheet = oDoc.getSheets().getByIndex(0) Set oimage = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape") Set oView = oDoc.CurrentController Set oDrawPage = oView.getActiveSheet.DrawPage Set oimage = oDrawPage.getByIndex(0) Image1.Picture = LoadPicture(oimage.GraphicURL) 

这是解压缩图片的输出

在这里输入图像说明

我不知道你的代码到底是什么,因为我通常不使用Microsoft Office。 不过看起来这个任务可以使用OpenOffice Basic完成。 Andrew Pitonyak的macros文档是学习OpenOffice Basic最好的地方之一。

首先看5.9.5. Convert all linked images5.9.5. Convert all linked images 5.9.5. Convert all linked images

编辑

为了在Calc中做到这一点,首先我去了Tools -> Macros -> Organize Dialogs并创build了一个名为“ImageViewerForm”的对话框,名为“MyImageControl”。

然后我去了Tools -> Macros -> Organize Macros -> OpenOffice Basic和添加以下代码:

 Sub ShowImageViewerDialog oDoc = ThisComponent oDlg = CreateUnoDialog(DialogLibraries.Standard.ImageViewerForm) oControl = oDlg.Model.MyImageControl oDrawPage = oDoc.getDrawPages().getByIndex(0) oImage = oDrawPage.getByIndex(0) oControl.ImageURL = oImage.GraphicURL oDlg.execute() End Sub 

要运行代码,请转至Tools -> Macros -> Run Macro 。 结果如下:

在这里输入图像说明

通过添加事件处理程序,“Next Image”button应该相当简单。

有关文档,请参阅GraphicObjectShape和UnoControlButtonModel 。 但大多数情况下,我只是使用MRI工具来弄清楚。

编辑2

关于您发布的错误,本例中的GraphicURL属性是一个引用内存中对象而不是文件path的string。 string示例如下所示: https : //www.openoffice.org/api/docs/common/ref/com/sun/star/graphic/XGraphicObject.html 。

所以它不适合传递给LoadPicture ,它需要一个文件名。

也许你可以从oImage.Bitmap或oImage.Graphic获取实际的图像数据。 使用MRI来查看可用的属性。

例如,它看起来像有一个getDIB()方法可能会像这样工作:

 Form1.Image1.Picture = oImage.Bitmap.getDIB() 

还有一个想法:不用使用Office应用程序,您可以编写解压缩文件并读取Pictures子目录中的每个图像的代码。

我从来没有尝试过,但根据文档,您可以通过COM自动化控制Open Office。

上面的VB6代码是通过COM自动控制Excel的。 您可以在您的机器上安装Open Office,然后查看是否可以从VB6中自动运行Calc以打开电子表格并提取图像。 我不知道是否允许。 Excel COM自动化function非常强大,可以让你几乎做任何事情,Open Office可能没有那么强大(我不知道)。

我会build议仔细考虑你正在试图解决什么问题,以及是否有另一种方法完全!