Documents > Cookbook >Cell

Get Cell
If you want to get the specified cell in a table,you can use the getCellByPosition method of Table.
The first parameter is the column index,the second parameter is the row index.

         TextDocument document = (TextDocument) TextDocument.loadDocument(filePath);
Table table=document.getTableByName("stringTable");
Cell cell=table.getCellByPosition(1, 1);

If you are manipulating a spreadsheet,you can get the cell by its address:

     Table sheet1 = document.getTableByName("Sheet1");
Cell odsCell=sheet1.getCellByPosition("A1");

If you want to get a cell from a row,you can specify the index of the cell in the row.

     Row row=table.getRowByIndex(1);
Cell cell2=row.getCellByIndex(1);

What can I do if I have a Cell instance and want to know which column and row it belongs to ?
The code below shows how you can do that:

     Row row1=cell.getTableRow();
Column column1=cell.getTableColumn();

Control Cell Attributes
Use the getStyleName() method you can get the style name of the cell.
If you want to change the style,it must be set when you set the display text.

     String cellSytle=cell.getStyleName();
cell.setDisplayText("content", cellSytle);

What can I do if I want to control the display alignment of the cell?
You can set the horizontal and vertical alignment to do so.
The code below shows how to get and set the alignment.You can refer to the javadoc about the alignment type.

      StyleTypeDefinitions.HorizontalAlignmentType horizontalAlign=cell.getHorizontalAlignmentType();
StyleTypeDefinitions.VerticalAlignmentType verticalAlign=cell.getVerticalAlignmentType();

If the content of the cell is too long,you can set the wrap option of the cell.


If don't know the cell is wrapped or not,you can use the method:

       boolean isTextWrapped=cell.isTextWrapped();

If you want to set the background color of the cell,be care that the color type is org.odftoolkit.odfdom.type.Color.

        Color cellBackgroundColor=cell.getCellBackgroundColor();

How can I control the spanned number of the column/row:

        int spannedNum=cell.getcgetColumnSpannedNumber();
int rowSpannedNum=cell.getRowSpannedNumber();

For column,maybe you want to know the column repeated number:

        int repeatedNum=cell.getColumnsRepeatedNumber();

How about formatting a cell's content? You can set the format string of the cell to do so.
For example you want to format the date to yyyy-MM-dd ,you can:

          String cellFormatStr=cell.getFormatString();
cell.setDateValue(new GregorianCalendar(2010,5,1));

Be care that the setFormatString only works for float, date and percentage.
You may be confused by the difference between getFormatString and getFormula,the difference is that:
For the setFormula method,it just sets as a formula attribute,the cell value will not be calculated.

          String formula=cell.getFormula();

How can I clear the content of the cell?
RemoveContent remove all of the cell while the removeTextContent only remove the text content of the cell.


Get&Set Cell Value Type
The cell value can have different types,for the setValueType method: the parameter can be If the parameter type is not a valid cell type, an IllegalArgumentException will be thrown.

       String valueType=cell.getValueType();

For the following getXXXValue() method:it gets the cell value as xxx type.An IllegalArgumentException will be thrown if the cell type is not xxx.
Get&Set boolean type Cell
For setBooleanValue method:it sets the cell value as a boolean and sets the value type to be boolean.

       boolean booleanValue=cell.getBooleanValue();

Get&Set currency type Cell
For the following getting methods,if the value type is not "currency", an IllegalArgumentException will be thrown.
The currency code of the cell is like "USD", "EUR", "CNY", and the currency symbol is like "$"

       String currencyCode=cell.getCurrencyCode();  

You can also set currency value and currency format.Please note the overall format includes the symbol character, for example: $#,##0.00.

       cell.setCurrencyValue(100.00, "USD");
cell.setCurrencyFormat("$", "$#,##0.00");

Get&Set date type Cell
       Calendar dateValue=cell.getDateValue(); 
cell.setDateValue(new GregorianCalendar(2010,5,1));

Get&Set float type Cell
      double floatValue=cell.getDoubleValue();  
cell.setDoubleValue(new Double(22.99f));

Get&Set percentage type Cell
      double percentageValue=cell.getPercentageValue();

Get&Set string type Cell
If the cell type is not string, the display text will be returned.

      String stringValue=cell.getStringValue();   

Deal with the Time Value
If you want to get the string type of time value,you can format it:

SimpleDateFormat simpleFormat = new SimpleDateFormat("'PT'HH'H'mm'M'ss'S'");
String timeString= simpleFormat.format(cell.getTimeValue().getTime());

Something about Display Text
Please note the display text in ODF viewer might be different from the value set by this method, because the displayed text in ODF viewer is calculated and set by editor.

      String displayText=cell.getDisplayText();

Set image
From version 0.5.5, we support high level APIs for images. You can use following codes to set an image to a cell.

        Image myImage = cell.setImage(new URI(""));

You can use following codes to access an image in a cell.

        Image image = cell.getImage();
String imagename = image.getName();
FrameRectangle rect = image.getRectangle();

Powered by the Apache CMS.

Apache "ODF Toolkit" is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Copyright © 2011 The Apache Software Foundation Licensed under the Apache License, Version 2.0. Contact Us
Apache and the Apache feather logos are trademarks of The Apache Software Foundation.
Other names appearing on the site may be trademarks of their respective owners.