本文将介绍如何使用LibOpenOffice库实施OpenOffice表格自动化。这个库被开发用来打开或编辑所有OpenOffice表格文档。
开始工作之前你需要下载LibOpenOffice库及示例项目,示例项目可以使用微软vs 2010或更高的版本打开。
下载Apache OpenOffice 3.4.14.0或更高版本,把它安装到电脑上。
解压下载的文档,打开LibOpenOffice_sample_project.sln。
在打开的项目中你可以看到一个简单有按钮的表格,可以用它来创建CalcWriterImpress文件。
你也可以查看描述LibOpenOffice功能的帮助文件OOAPI.chm。
创建一个新的或打开一个已存在的OpenOffice表格文档:
OOAPI3 *t = new OOAPI3(); if( t ) { if( t->isInstall() ) // check if OpenOffice is installed { if( t->connect2Calc("private:factory/scalc", "localhost", "2083", false, "DEMO") ) { // here your code to work with openedcreated document // to open existing document you need to call // t->connect2Calc("file:///C:/test.xls", // "localhost", "2083", false, "DEMO") } } }
在打开的OpenOffice表格文档中编辑文字:
if( t->connect2Calc("private:factory/scalc", "localhost", "2083", false, "DEMO") ) { t->setSheet(0); // sets first sheet to edit t->setText(0,0,"Hello world!"); // sets "Hello world" string for A1 cell }
在OpenOffice表格文档中使用样式:
if( t->connect2Calc("private:factory/scalc", "localhost", "2083", false, "DEMO") ) { t->SetSheet(0); // sets first sheet to edit t->setText(0,0,"244"); // sets "244" string for A1 cell t->setCellStyle(0,0,S_PERCENT); // sets "percent" style for A1 cell t->setCellStyle(0,1,S_DATE_F); // sets "date" style for A2 cell } //Note: all other styles you can see in OOAPI.chm help file.
保存当前文档:
if( t->connect2Calc("private:factory/scalc", "localhost", "2083", false, "DEMO") ) { // some code to edit document t->export2XLS("file:///D:/test.xls"); // saving document to "D: est.xls" }
LibOpenOffice中也能使用文本,格式,样式,幻灯片,图片及其他功能。你可以在这里看到其他一些示例。
LibOpenOffice是Apache OpenOffice自动化的一个强有力的工具。免费及专业团队的支持是它的重大优点。考虑到这个库的低开销,它的确是使用OpenOffice时自动化的一个好的解决方案。我希望这篇文章能在工作中帮到你。
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。 2KB翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。2KB项目(www.2kb.com,源码交易平台),提供担保交易、源码交易、虚拟商品、在家创业、在线创业、任务交易、网站设计、软件设计、网络兼职、站长交易、域名交易、链接买卖、网站交易、广告买卖、站长培训、建站美工等服务