0

我正在尝试创建一个 libreoffice 基本宏,它可以让您将文件的全部内容复制到表格行中。下面的代码适用于文本文件,例如 .ods 或 .txt,但对于 .pdf 和 .ods 文件有一些问题。特别是它在 getText() 方法上崩溃。你知道我可以使用什么其他方法来解决我的问题吗?

`

REM ***The file Url***
sUrlDoc = "file:///C:/Users/user/Desktop/Test.ods"

REM ***It correctly opens the file***
oDoc = StarDesktop.loadComponentFromURL(sUrlDoc, "_blank", 0, Prop() )

REM ***Correctly inserts a new row in the table***
oTable.Rows.insertByIndex(oTable.getRows().getCount(),1)

REM ***It goes into the right position***
oCell = oTable.getCellByPosition(0,1)

REM ***Should read from file (only works with .odt and .txt)***
oCursor = oDoc.getText(1)
oCell.setString(oCursor.string)

oDoc.close(true)`
4

1 回答 1

0

您可以通过多种方式获取 ODS 文件的上下文。

其中最慢的是逐个工作表和逐个单元格地迭代工作簿中的所有数据,取出每个单元格的文本内容。

我建议使用Andrew Pitonyak在第5.23 章中展示的方法。操作剪贴板把这本书放在手边,你就不用写很多宏来解决日常任务——你只需要现成的代码

Function getContentODS(sDocName As String) As String 
Dim oDoc As Variant         ' Spreadsheet as object
Dim bDisposable As Boolean  ' Can be closed
Dim oSheets As Variant      ' All sheets of oDoc
Dim oSheet As Variant       ' Single sheet
Dim i As Long           
Dim oCurrentController As Variant
Dim oCursor As Variant      ' Get Used Area
Dim oTransferable As Variant    ' Content of selection
Dim oTransferDataFlavors As Variant
Dim oConverter As Variant   ' Util
Dim j As Integer, iTextLocation As Integer
Dim oData As Variant
Dim sResult As String       ' All content as very long string
    GlobalScope.BasicLibraries.loadLibrary("Tools")
    If Not FileExists(sDocName) Then Exit Function 
    oDoc = OpenDocument(ConvertToURL(sDocName), Array(), bDisposable)
    sResult = FileNameoutofPath(sDocName) & ": "
    oCurrentController = oDoc.getCurrentController()
    oSheets = oDoc.getSheets()
    oConverter = createUnoService("com.sun.star.script.Converter")
    For i = 0 to oSheets.getCount()-1
        oSheet = oSheets.getByIndex(i)
        oCursor = oSheet.createCursor()
        oCursor.gotoEndOfUsedArea(True)
        oCurrentController.select(oCursor)
        oTransferable = oCurrentController.getTransferable()
        oTransferDataFlavors = oTransferable.getTransferDataFlavors()
        iTextLocation = -1
        For j = LBound(oTransferDataFlavors) To UBound(oTransferDataFlavors)
            If oTransferDataFlavors(j).MimeType = "text/plain;charset=utf-16" Then
                iTextLocation = j
                Exit For
            End If
        Next
        If (iTextLocation >= 0) Then
            oData = oTransferable.getTransferData(oTransferDataFlavors(iTextLocation))
            sResult = sResult & oSheet.getName() & "=" & _
                oConverter.convertToSimpleType(oData, com.sun.star.uno.TypeClass.STRING) & "; "
        End If
    Next i
    If bDisposable Then oDoc.close(True)
    getContentODS = sResult
End Function

此函数将打开电子表格,它将在参数中接收到的路径和名称,遍历所有工作表,取出文本内容并将其连接成一个长字符串变量,最后关闭文档。

您可以使用以下过程测试此代码:

Sub tst
    MsgBox getContentODS("C:\Users\user\Desktop\Test.ods")
End Sub

因此该函数将为您返回一个字符串。考虑如何处理这一行(或查看第 7 章。Writer Macros)

要获取 PDF 文档的文本部分,您可以使用类似的技术(将内容从 AcrobatReader 复制到剪贴板并仅取出复制的文本部分)或在 Draw 中打开它并遍历所有图形元素,以便从他们那里获取文本片段。

于 2020-10-07T11:10:04.327 回答