libreoffice 文档中插入表格-保存-打印

环境信息:

OS :win7 x64

IDE:VC3013

LibreOffice_5.2.2_Win_x64

LibreOffice_5.2.2_Win_x64_sdk


【代码】

// libreoffice.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <stdio.h>
#include <osl/file.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/awt/XToolkit2.hpp>
#include <com/sun/star/awt/Rectangle.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/text/XWordCursor.hpp>
#include <com/sun/star/awt/WindowAttribute.hpp>
#include <osl/process.h>
#include <rtl/process.h>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <cppuhelper/weak.hxx>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XBookmarksSupplier.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/awt/XSystemChildFactory.hpp>
#include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
#include <com/sun/star/lang/SystemDependent.hpp>
#include <com/sun/star/view/XPrintable.hpp>
#include <com/sun/star/frame/XDesktop.hpp>




using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace com::sun::star::awt;
using namespace rtl;
using namespace cppu;
using namespace com::sun::star::uno;
using namespace com::sun::star::beans;
using namespace com::sun::star::registry;
using ::rtl::OUString;
using ::rtl::OUStringToOString;
using namespace com::sun::star::text;
using namespace com::sun::star::table;
using namespace com::sun::star::container;
using ::rtl::OString;
using namespace com::sun::star::view;


#pragma comment(lib, "icppu.lib")
#pragma comment(lib, "icppuhelper.lib")
#pragma comment(lib, "ipurpenvhelper.lib")
#pragma comment(lib, "isal.lib")
#pragma comment(lib, "isalhelper.lib")




// a procedure for what the so called boostrap
Reference< XMultiServiceFactory > ooConnect()
{
// create the initial component context
Reference< XComponentContext > rComponentContext(::cppu::bootstrap());




// retrieve the servicemanager from the context
Reference< XMultiComponentFactory > rServiceManager = rComponentContext->getServiceManager();




// instantiate a sample service with the servicemanager.
Reference< XInterface > rInstance = rServiceManager->createInstanceWithContext(
OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver"), rComponentContext);




// Query for the XUnoUrlResolver interface
Reference< XUnoUrlResolver > rResolver(rInstance, UNO_QUERY);
if (!rResolver.is())
{
printf("Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n");
return NULL;
}




try {
// resolve the uno-url
rInstance = rResolver->resolve(OUString::createFromAscii("uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager"));
if (!rInstance.is()){
printf("StarOffice.ServiceManager is not exported from remote counterpart\n");
return NULL;
}




// query for the simpler XMultiServiceFactory interface, sufficient for scripting
Reference< XMultiServiceFactory > rOfficeServiceManager(rInstance, UNO_QUERY);




if (!rOfficeServiceManager.is())
{
printf("XMultiServiceFactory interface is not exported forStarOffice.ServiceManager\n");
return NULL;
}
return rOfficeServiceManager;
}
catch (Exception &e)
{
OString o = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
printf("Error: %s\n", o.pData->buffer);
return NULL;
}
return NULL;
}


int NewDoc()
{
#if 1
//retrieve an instance of the remote service manager
Reference< XMultiServiceFactory > rOfficeServiceManager;
rOfficeServiceManager = ooConnect();
if (rOfficeServiceManager.is())
{
printf("Connected sucessfully to the office\n");
}
else
{
printf("Connected failure to the office\n");
return 0;
}
//get the desktop service using createInstance returns an XInterface type
Reference< XInterface > Desktop = rOfficeServiceManager->createInstance(
OUString::createFromAscii("com.sun.star.frame.Desktop"));




//query for the XComponentLoader interface
Reference< XComponentLoader > rComponentLoader(Desktop, UNO_QUERY);
if (rComponentLoader.is())
{
printf("XComponentloader successfully instanciated\n");
}




//get an instance of the spreadsheet
Reference< XComponent > xWriterComponent = rComponentLoader->loadComponentFromURL(
OUString::createFromAscii("private:factory/swriter"),
OUString::createFromAscii("_blank"),
0,
Sequence < ::com::sun::star::beans::PropertyValue >());
// add code here




Reference < XTextDocument > xTextDocument(xWriterComponent, UNO_QUERY);
Reference< XText > xText = xTextDocument->getText();
Reference< XTextCursor> xTextCursor = xText->createTextCursor();
xTextCursor->setString(OUString::createFromAscii("Hello"));
xTextCursor->setString(OUString::createFromAscii(" All around the world"));


Reference < XWordCursor > xWordCursor(xTextCursor, UNO_QUERY);
xWordCursor->gotoEndOfWord(0);
// Jump to the end of the text




// Create a new text table from the document's factory
// Don't forget to add : #include <com/sun/star/text/XTextTable.hpp>
// Don't forget to add "com.sun.star.text.XTextTable \" in the makefile
// getting MSF of the document
Reference<XMultiServiceFactory> oDocMSF(xTextDocument, UNO_QUERY);
OUString sTableName("com.sun.star.text.TextTable"); //OUString::createFromAscii("com.sun.star.text.TextTable")
Reference <XTextTable> xTable(
oDocMSF->createInstance(sTableName),
UNO_QUERY);
if (!xTable.is())
{
printf("Erreur creation XTextTable interface !\n");
return 0;
}


// Specify that we want the table to have 4 rows and 4 columns
xTable->initialize(25, 6);


Reference<XTextRange> xTextRange = xWordCursor->getEnd();




// Don't forget to add : #include <com/sun/star/text/XTextContent.hpp>
// Don't forget to add "com.sun.star.text.XTextContent \" in the makefile
Reference <XTextContent>xTextContent(xTable, UNO_QUERY);


// Insert the table into the document
xText->insertTextContent(xTextRange, xTextContent, (unsigned char)0);


//----------------------------
Reference<XPropertySet> xTableProps(xTable, UNO_QUERY);
Any prop;
prop <<= (sal_Bool)false;
xTableProps->setPropertyValue(OUString::createFromAscii("BackTransparent"), prop);
prop <<= (long)0x0000FF; // color blue
// xTableProps->setPropertyValue(OUString::createFromAscii("BackColor"),prop);




// I want to change the first row color
// Don't forget to add : using namespace com::sun::star::table;
// Don't forget to add : #include <com/sun/star/table/XTableRows.hpp>
// Don't forget to add "com.sun.star.table.XTableRows \" in the makefile
Reference<XTableRows> xTableRows = xTable->getRows();
Reference<XIndexAccess> theRows(xTableRows, UNO_QUERY);
Reference<XPropertySet> xRowProps(theRows->getByIndex((short)0), UNO_QUERY);
prop <<= (sal_Bool)false;
xRowProps->setPropertyValue(OUString::createFromAscii("BackTransparent"), prop);
prop <<= (long)0x000099;
xRowProps->setPropertyValue(OUString::createFromAscii("BackColor"), prop);




// it's time to add some text now
Reference<XCell> xCell = xTable->getCellByName(OUString::createFromAscii("A1"));
xText = Reference<XText>(xCell, UNO_QUERY);
xTextCursor = xText->createTextCursor();
xTextCursor->setString(OUString::createFromAscii("Titre1"));
xCell = xTable->getCellByName(OUString::createFromAscii("A2"));
//xCell->setValue(11171);




//----------------------
xTextCursor->getEnd();
// Jump to the end of the text




// Create a new text table from the document's factory
// Don't forget to add : #include <com/sun/star/text/XTextTable.hpp>
// Don't forget to add "com.sun.star.text.XTextTable \" in the makefile
// getting MSF of the document
Reference<XMultiServiceFactory> oDocMSF1(xTextDocument, UNO_QUERY);
Reference <XTextTable> xTable1(oDocMSF->createInstance(sTableName), UNO_QUERY);
if (!xTable1.is())
{
printf("Erreur creation XTextTable interface !\n");
return 1;
}




// Specify that we want the table to have 4 rows and 4 columns
xTable1->initialize(2, 3);
/// --------------------------------------------
xCell = xTable->getCellByName(OUString::createFromAscii("A3"));
xCell->setValue(14);
xCell = xTable->getCellByName(OUString::createFromAscii("A4"));
xCell->setFormula(OUString::createFromAscii("=sum(<A2>|<A3>)"));
xCell = xTable->getCellByName(OUString::createFromAscii("B1"));
xText = Reference<XText>(xCell, UNO_QUERY);
xTextCursor = xText->createTextCursor();
Reference<XPropertySet> oCPS(xTextCursor, UNO_QUERY);
prop <<= (long)0xFF0000; // color red
oCPS->setPropertyValue(OUString::createFromAscii("CharColor"), prop);
xTextCursor->setString(OUString::createFromAscii("Titre2"));
xCell = xTable->getCellByName(OUString::createFromAscii("B2"));
xCell->setValue(20);
xCell = xTable->getCellByName(OUString::createFromAscii("B3"));
xCell->setValue(5);
xCell = xTable->getCellByName(OUString::createFromAscii("B4"));
xCell->setFormula(OUString::createFromAscii("=<B2>*<B3>"));
//-----------------------------








//保存文档
Reference< XStorable > rcomponentStore(xWriterComponent, UNO_QUERY);
if (!rcomponentStore.is())
{
printf("XStorable Not successfully instanciated\n");
}
else
{




rcomponentStore->storeAsURL(OUString::createFromAscii(
"file:///E|/Works/libre-file/tata2.doc"),
Sequence < ::com::sun::star::beans::PropertyValue >());
}








//打印文档
Reference<XPrintable> printer(xWriterComponent, UNO_QUERY);
::css::uno::Sequence< ::css::beans::PropertyValue >  proValue = printer->getPrinter();




proValue[0].Name = "Name";
proValue[0].Value <<= OUString::createFromAscii("\"<Favoured printer>\" \"<URL|path>\" \"<Pages>\"");
//proValue[0].Value <<= OUString::createFromAscii("<Favoured printer> <URL|path> <Pages>");
printer->setPrinter(proValue);




proValue[0].Name = "Name";
proValue[0].Value <<= OUString::createFromAscii("file:///E|/Works/libre-file/tata2.doc");
proValue[0].Value <<= OUString::createFromAscii("1-1");
printer->print(proValue);
#endif
return 1;
}






int _tmain(int argc, _TCHAR* argv[])
{
NewDoc();
system("pause");

return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
YOLO高分设计资源源码,详情请查看资源内容使用说明 YOLO高分设计资源源码,详情请查看资源内容使用说明 YOLO高分设计资源源码,详情请查看资源内容使用说明 YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明YOLO高分设计资源源码,详情请查看资源内容使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值