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

时间:2016-12-02 07:58:09

标签: excel vb.net vb6 openoffice.org openoffice-calc

我有一个代码可以将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)

这是解压缩图片的输出

enter image description here

2 个答案:

答案 0 :(得分:1)

我不确切知道您的代码是做什么的,因为我通常不使用Microsoft Office。但是看起来这个任务可以使用OpenOffice Basic完成。学习OpenOffice Basic的最佳地点之一是Andrew Pitonyak's Macro Document

首先,请查看5.9.5. Convert all linked images部分。

修改

要在Calc中执行此操作,首先我转到Tools -> Macros -> Organize Dialogs并创建了一个名为" 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。结果如下:

enter image description here

"下一张图片"通过添加事件处理程序,按钮应该相当简单。

有关文档,请参阅GraphicObjectShapeUnoControlButtonModel。但大多数情况下我只是用MRI tool来解决这个问题。

编辑2

关于您发布的错误,在这种情况下,GraphicURL属性是一个引用内存中对象而不是文件路径的字符串。此处显示了字符串的示例: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()

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

答案 1 :(得分:0)

我从未尝试过,但according to the docs您可以通过COM自动化控制Open Office。

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

我建议仔细考虑一下你要解决的问题,以及是否还有另一种方法!