Skip to content

Instantly share code, notes, and snippets.

@astoeckel
Created April 29, 2020 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astoeckel/fae7fd37a0f5c5f48bce03bee301fbcd to your computer and use it in GitHub Desktop.
Save astoeckel/fae7fd37a0f5c5f48bce03bee301fbcd to your computer and use it in GitHub Desktop.
Libre Office Export All Sheets As CSV
Sub ExportAllToCsv
document = ThisComponent
' Use the global string tools library to generate a base filename for each CSV
' based on the current prefixless filename
GlobalScope.BasicLibraries.loadLibrary("Tools")
DocumentName = Left(document.Namespace, Len(document.Namespace) - 1)
BasePath = Tools.Strings.DirectoryNameoutofPath(DocumentName, "/")
BaseFilename = Tools.Strings.GetFileNameWithoutExtension(DocumentName, "/")
' Work out number of sheets for looping over them later.
Sheets = document.Sheets
NumSheets = Sheets.Count - 1
' 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,0,1,,0"
For I = 0 to NumSheets
' For each sheet, assemble a filename and save using the filter
SheetName = LCase(Sheets(I).Name)
SheetName = Replace(SheetName, " ", "_")
document.getCurrentController.setActiveSheet(Sheets(I))
Filename = BasePath + "/" + BaseFilename + "_" + SheetName + ".csv"
FileURL = convertToURL(Filename)
document.StoreToURL(FileURL, Propval())
Next I
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment