Calc C#CLI programming(转贴)

来源:互联网 发布:linux显示环境变量 编辑:程序博客网 时间:2024/06/05 18:58

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();
}
}
}

原创粉丝点击