翻译:OpenOffice.org API介绍(三)

Posted on 2007-07-18 11:37 Tommy Jian 阅读(3194) 评论(2)  编辑  收藏 所属分类: OpenOffice.org

  3.3  数据的设置

   我们需要使用的有三种类型的数据。在开始之前,我们需要有个方法来获得对单元格的访问。这一节的其他辅助性方法也将使用此方法,定义如下:

public XCell getXCellByPosition(XSpreadsheet xSpreadsheet, int x, int y)
    throws Exception 
{
    return xSpreadsheet.getCellByPosition(x, y);
}

 

   

   首先,我们将要接触到文本类型的数据,比如说Javalobby文章的标题。对于这种数据,辅助性方法需要电子表对象、列位置、行位置以及数据本身作为参数。

public void setTextValueOfXCellAtPosition(XSpreadsheet
    xSpreadsheet, 
int x, int y, String value) throws Exception 
{
    //We first identify the cell we need to work with,
    //using the incoming x and y values:
    XCell xCell = getXCellByPosition(xSpreadsheet, x, y);
    //Next, since we're working with text, we define
    //a text object and a cursor object and insert the received content into the cell:
    XText xText = (com.sun.star.text.XText)UnoRuntime.queryInterface(com.sun.
        star.text.XText.
class, xCell);
    XTextCursor xTextCursor 
= xText.createTextCursor();
    xText.insertString(xTextCursor, value, 
false);
}

 

   其次,对于数字类型的数据,比如说“Reply”列的数据,辅助性方法要求传递double类型的参数:

public void setNumValueOfXCellAtPosition(XSpreadsheet
    xSpreadsheet, 
int x, int y, double value) throws Exception 
{
    //First we get the cell identified by the received x and y values:
    XCell xCell = getXCellByPosition(xSpreadsheet, x, y);
    //Then we add the received value to the identified cell:
    xCell.setValue(value);
}

 

   最后,尽管Calc的公式是普通的字符串,我们可以使用OpenOffice.org的API所包含的单元格样式属性来为单元格设置预定义的“Result”样式,这主要是针对我们汇总回复总数的计算公式来进行设置:

public void setFormulaOfXCellAtPosition(XSpreadsheet
    xSpreadsheet, 
int x, int y, String formula) throws Exception 
{
    //We get the cell defined by the incoming x and y values"
    XCell xCell = getXCellByPosition(xSpreadsheet, x, y);
    //We add a Calc formula to the cell, as received by the helper method:
    xCell.setFormula(formula);
    //We attach a property set to our cell, so that we can define a property:
    XPropertySet xCellProps = (XPropertySet)UnoRuntime.
        queryInterface(XPropertySet.
class, xCell);
    //We set the style of the cell, using a predefined "Result" style,
    //which comes out of the box with the OpenOffic.org API:
    xCellProps.setPropertyValue("CellStyle""Result");
}

 

  3.4  颜色的使用

   下面的代码将在随后被使用:

if (position%2 == 0
{
    oooHelper.setColorRow(xSpreadsheet, position, 
0xFF9933);
}

 

   在ARGB颜色空间中,0xFF9933代表橙色。如果行数是偶数,那么电子表、行数以及橙色会被作为参数传递给方法:

public void setColorRow(XSpreadsheet
    xSpreadsheet, 
int row, int color) throws Exception 
{
    //First we get the range of cells we want to deal with,
    //which is the whole spreadsheet:
    XCellRange xCellRange = (XCellRange)UnoRuntime.queryInterface
        ( XCellRange.
class, xSpreadsheet );
    //Next, we narrow down our selection further,
    //going from column 0/current row to column 3/current row,
    //which is a whole row from left to right:
    XCellRange xSelectedCells = xCellRange.getCellRangeByPosition(0, row, 3, row);
    //Next, we create a property set and assign it to our selected range:
    XPropertySet xCellProps =
        (XPropertySet)UnoRuntime.queryInterface(XPropertySet.
class,xSelectedCells);
    //This line sets the color to white, which basically
    //refreshes the row color before we add our new row color:
    xCellProps.setPropertyValue("CellBackColor"new Integer(16777215));
    //This line sets the color to whatever is received,
    //in this case orange:
    xCellProps.setPropertyValue("CellBackColor"new Integer(color));
}

 

   如果用户需要看到“Most Replies”或者“Least Replies”,我们将使用以下代码进行设置:

ooHelper.setColorCell(xSpreadsheet, 2, jTable1.getRowCount()+50x008000);

 

   以下的方法需要电子表、列数、行数以及颜色值作为参数:

public void setColorCell(XSpreadsheet xSpreadsheet, int column, int row, int color)
    throws Exception 
{
    //First, we select the entire received spreadsheet:
    XCellRange xCellRange = (XCellRange)UnoRuntime.queryInterface( XCellRange.class,
        xSpreadsheet );
    //From the received spreadsheet, we select a single cell,
    //defined by the row and column received:
    XCellRange xSelectedCells = xCellRange.getCellRangeByPosition(column,
        row, column, row);
    //We define a property set, an object to contain the cell's properties:
    XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface
        (XPropertySet.
class, xSelectedCells);
    //This line sets the color to white, to refresh the cell:
    xCellProps.setPropertyValue("CellBackColor"new Integer(16777215));
    //This line sets the background color of the cell to whatever is received:
    xCellProps.setPropertyValue("CellBackColor"new Integer(color));
}

 


本文译自NetBeans.org中的文章,其中的代码也都经过译者测试。未完待续!!!

Feedback

# 如何获取单元格的合并数据信息呀  回复  更多评论   

2007-12-20 16:54 by lhslove@126.com
如何获取单元格的合并数据信息呀
比如我想得到某个单元格合并了几个单元格,用什么方法得到?谢谢

# re: 翻译:OpenOffice.org API介绍(四)  回复  更多评论   

2008-07-09 15:47 by xiaowu
aaa

只有注册用户登录后才能发表评论。


网站导航: