Return to Snippet

Revision: 23102
at January 29, 2010 20:28 by softmechanics


Initial Code
Sub ConvertXls2Csv( cFile, cSheet )
   cURL = ConvertToURL( cFile )

   'debug
   'Print "|"  + cURL + "|"


   ' Open the document.
   ' Just blindly assume that the document is of a type that OOo will correctly recognize and open -- without specifying an import filter.
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, Array(MakePropertyValue("Hidden", True, ) )
   
   ' Select the desired sheet
   sheets = oDoc.Sheets
   sheets.moveByName(cSheet, 0)
   
   'change the file ext to 'csv' or whatever you want
   cFile = Left( cFile, Len( cFile ) - 4 ) + "-" + cSheet + ".csv"
   cURL = ConvertToURL( cFile )

   ' Save the document using a filter.   
   oDoc.storeToURL( cURL, Array(MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ),)

   'close the document
   oDoc.close( True )
End Sub

'used to setup the propertyies array
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
   Dim oPropertyValue As New com.sun.star.beans.PropertyValue
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function

Initial URL

                                

Initial Description
modified from branflake2267's post at oooforum.org:
http://www.oooforum.org/forum/viewtopic.phtml?t=52942

usage:
Paste into the Standard.Module1 macro file.  Then, create xls2csv bash script containing:

    #!/bin/bash
    file="$(readlink -f "$1")"
    /usr/lib/openoffice/program/soffice -invisible -headless -norestore "macro:///Standard.Module1.ConvertXls2Csv(\"$file\", \"$2\")"


The first argument is the xls file name, second is the name of the sheet:

`xls2csv ./spreadsheet.xls "Some Sheet"`

Initial Title
Export a Single Sheet of an Excel Spreadsheet as CSV from the Command Line with Open Office

Initial Tags
linux, csv, excel

Initial Language
Other