Сохранение определенного листа Excel в формате .csv - PullRequest
0 голосов
/ 10 июня 2019

Я пытаюсь понять, как сохранить конкретный лист Excel в формате CSV с помощью командной строки в Linux. Я могу сохранить первый лист с помощью команды ниже:

libreoffice --headless --convert-to csv --outdir /tmp /tmp/test.xls

Кажется, должен быть способ указать лист, который я хочу сохранить, но я не могу его найти.

Есть ли способ сохранить его через LibreOffice?

1 Ответ

0 голосов
/ 10 июля 2019

Команда:

soffice --headless "macro:///Library1.Module1.ConvertSheet(~/Desktop/Software/OpenOffice/examples/input/Test1.ods, Sheet2)"

Код:

Sub ConvertSheet( SpreadSheetPath as String, SheetNameSeek as String)
REM IN SpreadSheetPath is the FULL PATH and file
REM IN SheetName sheet name to be found and converted to CSV

Dim Doc As Object  
Dim Dummy()

SheetNameSeek=trim(SheetNameSeek)

If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
  GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If

REM content of an opened window can be replaced with the help of the frame parameter and SearchFlags:

SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
com.sun.star.frame.FrameSearchFlag.ALL

REM Set up a propval object to store the filter properties
Dim Propval(1) as New com.sun.star.beans.PropertyValue
Propval(0).Name = "FilterName"
Propval(0).Value = "Text - txt - csv (StarCalc)"
Propval(1).Name = "FilterOptions"
Propval(1).Value = "44,34,76,1"

Url=ConvertToUrl(SpreadSheetPath)

  Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", _SearchFlags, Dummy)
  FileN=FileNameoutofPath(Url)

  BaseFilename = Tools.Strings.GetFileNameWithoutExtension(FileN)

  DirLoc=DirectoryNameoutofPath(ConvertFromUrl(Url),"/")+"/"

  Sheets = Doc.Sheets

  NumSheets = Sheets.Count - 1
    For J = 0 to NumSheets

        SheetName = Sheets(J).Name

        if (SheetName = SheetNameSeek)  then

          Doc.getCurrentController.setActiveSheet(Sheets(J))        

          Filename = DirLoc + BaseFilename + "."+ SheetName + ".csv"

          FileURL = convertToURL(Filename)

          Doc.StoreAsURL(FileURL, Propval())             
    end if
    Next J
Doc.close(true)
NextFile = Dir
End Sub
...