如何从Perl创建或读取OpenOffice电子表格?

如何从Perl创建或读取OpenOffice电子表格?,perl,openoffice.org,spreadsheet,Perl,Openoffice.org,Spreadsheet,用Perl创建和读取OpenOffice电子表格的好方法是什么?OpenOffice支持多种格式,如果您正在寻找可以读取/写入Excel兼容电子表格的内容,请查看以进行读取和写入。我已经使用了这两种方法,它们都非常成熟,并且工作得很好。我曾用于创建大型彩色编码表。我非常喜欢它。 对于更简单的任务,我经常使用任何电子表格程序都可以轻松打开的纯CSV格式 但是,为了与其他人兼容,您可能会选择Excel格式。我认为OpenOffice本机文档格式基于OpenDocument规范,基本上是一种zip压缩

用Perl创建和读取OpenOffice电子表格的好方法是什么?

OpenOffice支持多种格式,如果您正在寻找可以读取/写入Excel兼容电子表格的内容,请查看以进行读取和写入。我已经使用了这两种方法,它们都非常成熟,并且工作得很好。

我曾用于创建大型彩色编码表。我非常喜欢它。 对于更简单的任务,我经常使用任何电子表格程序都可以轻松打开的纯CSV格式


但是,为了与其他人兼容,您可能会选择Excel格式。

我认为OpenOffice本机文档格式基于OpenDocument规范,基本上是一种zip压缩的XML格式。如果是这样,您可能可以使用您选择的PerlXML操作工具来操作它

另外,还有一个为OpenDocument规范提供高级API的


据我所知,这些模块中的表方法应该允许读取和写入OO计算文档中的数据。

这是在2.4版中,因为最新版本不稳定。但有时它确实能与最新版本配合使用。这将允许在Calc中使用实时DDE功能,而不是操纵文件。这是在XP上使用Cygwin Perl,但应该与其他人一起使用。我只想说,Excel VBA编程API比以前好10倍,也比以前少10倍。那太糟糕了

[守则]

use Win32::OLE;

Win32::OLE->Option(Warn => 3); # Turn on warnings for easier debugging

#Win32::OLE->GetActiveObject
# Get the currently running process or create a new one
$objServiceManager = Win32::OLE->GetActiveObject("com.sun.star.ServiceManager") || Win32::OLE->new("com.sun.star.ServiceManager") || die "CreateObject: $!"; 

$Stardesktop = $objServiceManager->createInstance("com.sun.star.frame.Desktop");

# $Stardesktop->terminate();exit; # will kill ALL OpenOffice docs!!!
# Doc = StarDesktop.loadComponentFromURL(sURL, "_default", 0, aMediaDesc)

$propValue[0] = $objServiceManager->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$propValue[0]->{Name} = "Hidden"; # This does not work!
$propValue[0]->{Value} = 1;

#Open the file and update its links if you have DDE links in your file
$propValue[1] = $objServiceManager->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$propValue[1]->{Name} = "UpdateDocMode";
$propValue[1]->{Value} = 3; # com.sun.star.document.UpdateDocMode.FULL_UPDATE

$calc = $Stardesktop->loadComponentfromUrl("file:///C:/Documents and Settings/Chloe/Desktop/MyFile.ods", "MyCalc", 0, \@propValue );
# load a new blank spreadsheet
$calc = $Stardesktop->loadComponentFromURL( "private:factory/scalc", "_blank", 0, [] );

# How to hide, as loading the document hidden does not work.
$calc->getCurrentController->getFrame->getContainerWindow()->setVisible(0);

$oSheet = $calc->getSheets->getByIndex(0);

# how to execute an UNO command, such as menu items
# http://wiki.services.openoffice.org/wiki/Framework/Article/OpenOffice.org_2.x_Commands
$frame = $calc->getCurrentController->getFrame;
$dispatchHelper = $objServiceManager->createInstance("com.sun.star.frame.DispatchHelper");
$dispatchHelper->executeDispatch(
    $frame, 
    ".uno:CalculateHard",
    #".uno:UpdateAll", 
    #".uno:UpdateAllLinks", 
    #".uno:DataAreaRefresh",
    "_self",
    0,
    []
);


$row = 5;
$cellValue = $oSheet->getCellByPosition(0, $row)->getString(); # get a cell value

# sort in decending order
$range = $oSheet->getCellRangeByName("A1:P$row");
$fields[0] = $objServiceManager->Bridge_GetStruct("com.sun.star.table.TableSortField");
$fields[0]->{Field} = 7; # column number
$fields[0]->{IsAscending} = 0;
$unoWrap = $objServiceManager->Bridge_GetValueObject;
$unoWrap->Set ("[]com.sun.star.table.TableSortField", \@fields);
$sortDx = $range->createSortDescriptor();
$sortDx->[0]->{Name} = "ContainsHeader";
$sortDx->[0]->{Value} = 1;
$sortDx->[3]->{Name} = "SortFields";
$sortDx->[3]->{Value} = $unoWrap;
#$sortDx->[3]->{Value} = \@fields; # You would think this would work? It doesn't.
$range->sort($sortDx);


# create a new sheet to paste to
$calc->getSheets->insertNewByName("NewSheet", 1 );
$sheet2 = $calc->getSheets->getByIndex(1);
$calc->CurrentController->Select($sheet2);

# copy row
$pasteHere = $sheet2->getCellByPosition(0, 0)->CellAddress;
$copyRange = $oSheet->getCellRangeByName("A1:Q1")->RangeAddress;
$oSheet->copyRange($pasteHere, $copyRange);

$cellValue = $sheet2->getCellByPosition(16, $row)->getValue()); # get cell value as integer
$date = $sheet2->getCellByPosition(5, $row)->getString(); # must get dates as strings

$calc->getCurrentController->getFrame->getContainerWindow()->setVisible(1); # set visible
$calc->close(0); # close program window
#print Win32::OLE->LastError, "\n";

[/code]

@Brad Gilbert:这和我的链接不一样吗?@cms:Brad的链接指向发行版,而不是特定版本。这被认为是更好的形式。看见我已经相应地编辑了你的文章。@Michael Carman:谢谢,这比评论更清楚。这很有道理。