Calc C#CLI programming(转贴)

http://www.openoffice.org/servlets/ReadMsg?list=dev&msgNo=18569

 

My working sample code to open a sheet and access to cells:
----------------------------------------------------
using System;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;

using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.table;

namespace OOoCDIConsoleSample
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
unoidl.com.sun.star.uno.XComponentContext
m_xContext;
unoidl.com.sun.star.lang.XMultiServiceFactory
mxMSFactory;
unoidl.com.sun.star.sheet.XSpreadsheetDocument
mxDocument;
unoidl.com.sun.star.container.XIndexAccess
xSheetsIA;
unoidl.com.sun.star.sheet.XSpreadsheets
xSheets;
unoidl.com.sun.star.sheet.XSpreadsheet
xSheet;
unoidl.com.sun.star.table.XCell
xCell;

try
{
// get the remote office component context

m_xContext = uno.util.Bootstrap.bootstrap();

Console.WriteLine("Connected to a running
office ...");
mxMSFactory = (XMultiServiceFactory)
m_xContext.getServiceManager();
String available = (mxMSFactory != null ?
"available" : "not available");

Console.WriteLine( "remote ServiceManager is
" + available );

XComponentLoader aLoader =
(XComponentLoader)
mxMSFactory.createInstance(
"com.sun.star.frame.Desktop" );

XComponent xComponent =
aLoader.loadComponentFromURL
(
"file:///C:/x.ods",
"_blank",
0,
new
unoidl.com.sun.star.beans.PropertyValue[0]
);
mxDocument =
(unoidl.com.sun.star.sheet.XSpreadsheetDocument) xComponent;

xSheets = mxDocument.getSheets();
       
xSheetsIA =
(unoidl.com.sun.star.container.XIndexAccess) xSheets;
       
xSheet =
(unoidl.com.sun.star.sheet.XSpreadsheet) xSheetsIA.getByIndex( 1 ).Value;

// xSheet =
(unoidl.com.sun.star.sheet.XSpreadsheet) xSheetsIA.getByName("Project");

xCell = xSheet.getCellByPosition( 0, 0 );

Console.WriteLine("Cell value   = '" +
xCell.getValue() + "'");
Console.WriteLine("Cell Type1   = '" +
xCell.getType() + "'");
Console.WriteLine("Cell Type2   = '" +
xCell.GetType() + "'");
Console.WriteLine("Cell Formula = '" +
xCell.getFormula() + "'");
Console.WriteLine("Cell Error   = '" +
xCell.getError() + "'");

xCell.setValue(123);

Console.WriteLine("Cell value   = '" +
xCell.getValue() + "'");
Console.WriteLine("Cell Type1   = '" +
xCell.getType() + "'");
Console.WriteLine("Cell Type2   = '" +
xCell.GetType() + "'");
Console.WriteLine("Cell Formula = '" +
xCell.getFormula() + "'");
Console.WriteLine("Cell Error   = '" +
xCell.getError() + "'");

xSheets.insertNewByName("NewTabCDI", 0);
xSheets.moveByName("NewTabCDI", 2);

Console.ReadLine();

xComponent.dispose();

}
catch (System.Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值