将特定的Excel工作表另存为.csv [英] Saving specific Excel sheet as .csv

查看:58
本文介绍了将特定的Excel工作表另存为.csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何在Linux上通过命令行将特定的Excel工作表另存为CSV.我可以使用以下命令保存第一张纸:

I am trying figure out how to save a specific Excel sheet as CSV via command line on Linux. I am able to save the first sheet with the command below:

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

似乎应该有一种方法可以指定我要保存的工作表,但我找不到它.

It seems that there should be a way to specify the sheet I want to save, but I am not able to find one.

是否可以通过LibreOffice保存它?

Is there a way to save it via LibreOffice?

推荐答案

命令:

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

这篇关于将特定的Excel工作表另存为.csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆