使用Open Office从命令行将Excel电子表格的单个工作表导出为CSV
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Open Office从命令行将Excel电子表格的单个工作表导出为CSV相关的知识,希望对你有一定的参考价值。
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"`
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
以上是关于使用Open Office从命令行将Excel电子表格的单个工作表导出为CSV的主要内容,如果未能解决你的问题,请参考以下文章
如何将此功能从 Excel 转换为 Open Office?