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); 
	 System .out.println(cell2.getStringValue()); 
 
        
      
      
         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(); 
	  cell.setHorizontalAlignment(StyleTypeDefinitions .HorizontalAlignmentType .CENTER ); 
	  cell.setVerticalAlignment(StyleTypeDefinitions .VerticalAlignmentType .BOTTOM ); 
 
        
      
      
         If the content of the cell is too long,you can set the wrap option of the cell. 
      
 
      
      
        
	   cell.setTextWrapped(true ); 
 
        
      
      
         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(); 
	    cell.setCellBackgroundColor(Color .valueOf("#000000")); 
 
        
      
      
         How can I control the spanned number of the column/row: 
      
 
      
      
        
	    int  spannedNum=cell.getcgetColumnSpannedNumber(); 
	    cell.setColumnSpannedNumber(spannedNum); 
	    int  rowSpannedNum=cell.getRowSpannedNumber(); 
	    cell.setRowSpannedNumber(rowSpannedNum); 
 
        
      
      
         For column,maybe you want to know the column repeated number: 
      
 
      
      
        
	    int  repeatedNum=cell.getColumnsRepeatedNumber(); 
	    cell.setColumnsRepeatedNumber(repeatedNum); 
 
        
      
      
         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)); 
	      cell.setFormatString("yyyy-MM -dd"); 
 
        
      
      
         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(); 
	      cell.setFormula(formula);     
 
        
      
      
         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. 
      
 
      
      
        
	   cell.removeContent(); 
	   cell.removeTextContent(); 
 
        
      
      
      Get&Set Cell Value Type   
      
         The cell value can have different types,for the setValueType method: the parameter can be 
        
          "boolean"  
          "currency"  
          "date"  
          "float"  
          "percentage"  
          "string"  
          "time"  
          "void"  
          If the parameter type is not a valid cell type, an IllegalArgumentException will be thrown. 
      
  
      
      
        
	   String  valueType=cell.getValueType(); 
	   cell.setValueType(valueType); 
 
        
      
      
         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(); 
	   cell.setBooleanValue(booleanValue); 
 
        
      
      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();   
	   cell.setCurrencyCode("USD "); 
 
        
      
      
         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(); 
	  cell.setPercentageValue(0.89); 
 
        
      
      Get&Set string type Cell  
      
         If the cell type is not string, the display text will be returned. 
      
 
      
      
        
	  String  stringValue=cell.getStringValue();    
	  cell.setStringValue("simple"); 
 
        
      
      Deal with the Time Value  
      
         If you want to get the string type of time value,you can format it: 
      
 
      
      
        
	  cell.setTimeValue(Calendar .getInstance()); 
	  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(); 
	  cell.setDisplayText(displayText); 
 
        
      
      
      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 ("http://www.xxx.com/a.jpg")); 
 
        
      
      
         You can use following codes to access an image in a cell. 
      
 
      
      
        
	    Image  image = cell.getImage(); 
	    String  imagename = image.getName(); 
	    FrameRectangle  rect = image.getRectangle(); 
	    rect.setX(1); 
	    rect.setY(1); 
	    image.setRectangle(rect);