OOo on ready—C#篇

随着经济的不景气,越来越多的公司将办公软件从较昂贵的MS Office转换为便宜甚至免费的Open Office.org(简称OOo),毕竟OOo几乎可以满足所有的日常办公需求,并且兼容MS Office,那么作为开发人员的我们必须对OOo的开发进行了解。
本序列包括OOo on ready---VB篇和OOo on ready---C#篇
本文开篇使用C#做开发语言对scalc来做一个Demo,演示OOo的一些常用开发。

Open Office.org好像格外对.net用户眷顾,在其发布的SDK下已经为用户准备好了几个dll文件,路径在<sdkpath>\cli\下,如图:

image

这样的话,.net对OOo的编程就简单多了。

首先,引用这么几个dll文件

image

引用完毕后,就可以使用对象浏览器查看OOo所提供的相关方法了。下面具体讲一下如何启动OOo并且建立一个工作薄。

首先要引用几个命名空间:

using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;

然后使用bootstrap获得一个OOo组件,如果OOo没有打开就会新建一个:

unoidl.com.sun.star.uno.XComponentContext m_xContext = uno.util.Bootstrap.bootstrap();
然后使用该组件获得一个服务管理器:
unoidl.com.sun.star.lang.XMultiServiceFactory mxMSFactory=(XMultiServiceFactory)m_xContext.getServiceManager()
这样就实现了OOo的连接,使用该服务管理器,我们可以创建一个桌面实例,我们将该桌面实例强制转换为XComponent loader以便打开一个文档:
XComponentLoader aLoader = (XComponentLoader)
mxMSFactory.createInstance("com.sun.star.frame.Desktop");
然后我们使用该loader创建一个新文件或者打开一个已经存在的文件:
XComponent xComponent =
 aLoader.loadComponentFromURL(
"private:factory/scalc", "_blank", 0,
myArgs);

或者

XComponent xComponent =
 aLoader.loadComponentFromURL(
filepath, "_blank", 0,
myArgs);
unoidl.com.sun.star.sheet.XSpreadsheetDocument mxDocument=
(unoidl.com.sun.star.sheet.XSpreadsheetDocument)xComponent

注意:

1、此处的filepath不是我们一般的文件路径,因为OOo的路径格式比较特殊,必须是:file:\\\样式的,我们使用以下的一个函数进行转换:

/// <summary>
/// OOo路径转换
/// </summary>
/// <param name="file">windows 原始路径</param>
/// <returns>OOo的路径格式</returns>
public string PathConverter(string file)
{
file = file.Replace(@"\", "/");
return "file:///" + file;
}
2、此处的myArgs是打开时的参数,不使用任何参数就可以直接用一个默认值
new unoidl.com.sun.star.beans.PropertyValue[0]
如果有参数,比如不显式打开文档,就可以定义一个这样的属性参数数组:
unoidl.com.sun.star.beans.PropertyValue[] myArgs=new unoidl.com.sun.star.beans.PropertyValue[1];
myArgs[0] = new unoidl.com.sun.star.beans.PropertyValue();
myArgs[0].Name = "Hidden";
myArgs[0].Value =new uno.Any(IsHidden) 
有了XSpreadsheetDocument这个接口,就可以获得一张工作薄了:
/// <summary>
/// Returns the spreadsheet with the specified index (0-based).
/// </summary>
/// <param name="nIndex">The index of the sheet</param>
/// <returns>XSpreadsheet interface of the sheet.</returns>
public unoidl.com.sun.star.sheet.XSpreadsheet getSpreadsheet(int nIndex)
{
// Collection of sheets
unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
mxDocument.getSheets();
unoidl.com.sun.star.container.XIndexAccess xSheetsIA =
(unoidl.com.sun.star.container.XIndexAccess)xSheets;
unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
(unoidl.com.sun.star.sheet.XSpreadsheet)
xSheetsIA.getByIndex(nIndex).Value;
return xSheet;
}
有了这张工作薄,就可以“为所欲为”了,比如获得单元格,我们可以用:
unoidl.com.sun.star.table.XCell xCell = xSheet.getCellByPosition(col, row);
对单元格赋值我们使用:xCell.setValue(fValue);对单元格设置格式我们用xCell.setFormula(aFormula)等。
最后,我们看一下如何存储:
unoidl.com.sun.star.frame.XStorable xStorable = mxDocument as XStorable;
xStorable.storeAsURL(PathConverter(storeUrl), new unoidl.com.sun.star.beans.PropertyValue[] { });
存储之后需要关闭文档:
unoidl.com.sun.star.util.XCloseable xCloseable = 
mxDocument as unoidl.com.sun.star.util.XCloseable;
xCloseable.close(true);
本人将OOo的.net操作结合OOo的SDK内的实例封装成一个helper类,见附件,下面以生成一个chart图的实例说明如何使用该类:
private void doChartFunction(string filename)
{
SpreadSheetHelper helper = new SpreadSheetHelper(true);
// for common usage
unoidl.com.sun.star.sheet.XSpreadsheet xSheet =helper.getSpreadsheet(0);
unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
unoidl.com.sun.star.table.XCell xCell = null;
unoidl.com.sun.star.table.XCellRange xCellRange = null;
// Create a cell series with the values 1 ... 7.
for (int nRow = 8; nRow < 15; ++nRow)
xSheet.getCellByPosition(0, nRow).setValue(nRow - 7);
// *** Inserting CHARTS ***
unoidl.com.sun.star.table.XTableChartsSupplier xChartsSupp =
(unoidl.com.sun.star.table.XTableChartsSupplier)xSheet;
unoidl.com.sun.star.table.XTableCharts xCharts =
xChartsSupp.getCharts();
// The chart will base on the last cell series, initializing all values.
String aName = "newChart";
unoidl.com.sun.star.awt.Rectangle aRect =
new unoidl.com.sun.star.awt.Rectangle();
aRect.X = 10000;
aRect.Y = 3000;
aRect.Width = aRect.Height = 5000;
unoidl.com.sun.star.table.CellRangeAddress[] aRanges =
new unoidl.com.sun.star.table.CellRangeAddress[1];
aRanges[0] = helper.createCellRangeAddress(xSheet, "A9:A14");
// Create the chart.
xCharts.addNewByName(aName, aRect, aRanges, false, false);
// Get the chart by name.
uno.Any aChartObj = xCharts.getByName(aName);
unoidl.com.sun.star.table.XTableChart xChart =
(unoidl.com.sun.star.table.XTableChart)aChartObj.Value;
//change the chart style
//Diagram Service Names
//com.sun.star.chart.BarDiagram
//com.sun.star.chart.AreaDiagram
//com.sun.star.chart.LineDiagram
//com.sun.star.chart.PieDiagram
//com.sun.star.chart.DonutDiagram
//com.sun.star.chart.NetDiagram
//com.sun.star.chart.XYDiagram
//com.sun.star.chart.StockDiagram
unoidl.com.sun.star.chart.XChartDocument aChartDocument = 
(xChart as unoidl.com.sun.star.document.XEmbeddedObjectSupplier).getEmbeddedObject() 
as unoidl.com.sun.star.chart.XChartDocument;
unoidl.com.sun.star.lang.XMultiServiceFactory aFact = 
aChartDocument as unoidl.com.sun.star.lang.XMultiServiceFactory;
unoidl.com.sun.star.chart.XDiagram aDiagram = 
aFact.createInstance("com.sun.star.chart.AreaDiagram") as unoidl.com.sun.star.chart.XDiagram;
aChartDocument.setDiagram(aDiagram);
// Query the state of row and column headers.
aText = "Chart has column headers: ";
aText += xChart.getHasColumnHeaders() ? "yes" : "no";
helper.getCellByPosition(xSheet, 2, 8).setFormula(aText);
//xSheet.getCellByPosition(2, 8).setFormula(aText);
aText = "Chart has row headers: ";
aText += xChart.getHasRowHeaders() ? "yes" : "no";
helper.getCellByPosition(xSheet, 2, 9).setFormula(aText);
//xSheet.getCellByPosition(2, 9).setFormula(aText);
helper.storeDocComponent(filename);
helper.closeDocCompant();
}

更多技术文章,敬请登陆http://www.qx-net.cn

转载于:https://www.cnblogs.com/frankie/archive/2009/08/16/1547567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值