Domanda

Qual è un buon modo per creare e leggere un foglio di calcolo OpenOffice in Perl?

È stato utile?

Soluzione

Penso che il formato del documento nativo per l'ufficio aperto sia basato sulle specifiche OpenDocument e sia essenzialmente un formato XML compresso zip. Essendo vero, potresti probabilmente manipolarlo usando i tuoi strumenti di manipolazione XML perl preferiti.

In alternativa, c'è la Suite di moduli Open Office OpenDocument Connector sul CPAN che offre un'API di alto livello alle specifiche OpenDocument.

Per quanto ne so, i metodi di tabella in questi moduli dovrebbero consentire la lettura e la scrittura dei dati nei documenti OO Calc.

Altri suggerimenti

Ho usato OpenOffice :: OOCBuilder per creare grandi tabelle con codice colore. Mi è piaciuto molto. Per compiti più semplici, utilizzo spesso il semplice formato CSV che viene prontamente aperto da qualsiasi programma di fogli di calcolo

Tuttavia, potresti scegliere le cose di Excel per la compatibilità con altre persone.

OpenOffice supporta numerosi formati, se stai cercando qualcosa in grado di leggere / scrivere fogli di calcolo compatibili con Excel, controlla Foglio di calcolo :: ParseExcel per la lettura e Foglio elettronico :: WriteExcel per la scrittura. Li ho usati entrambi, sono abbastanza maturi e funzionano bene.

Questo è con la versione 2.4, perché l'ultima versione è instabile. Funziona con l'ultimo anche se a volte. Ciò consentirà le funzionalità DDE in tempo reale in Calc anziché manipolare i file. Questo sta usando Cygwin Perl su XP ma dovrebbe funzionare con altri. Voglio solo dire che l'API di programmazione VBA di Excel è 10 volte migliore e 10 volte meno confusa. Questo è piuttosto male.

[code]

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]

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top