Free Support Forum - aspose.com

How to copy and paste Font color using java aspose?


#1

I need to get font color one cell then that color set to another cell.

Color clr = null;
	for(int i=2;i<=maxDataRow;i++){
		Cell cell = cells.get("K"+i);
		String out=(String) cell.getValue();
		try{
			
		}catch(Exception e){
			if(out.trim().equals("STOP")){
				Style style = cell.getStyle();
				Font font = style.getFont();
				 clr=font.getColor();
				
			}else{
				break;
				
			}
		}
		
	}
	
	
	for(int i=2;i<=maxDataRow;i++){
		
		Cell cell = cells.get("K"+i);
		String out=(String) cell.getValue();
		System.out.println(out);
		
		try{
			
			if(out.trim().equals("ORGANIC")){
				Style style = cell.getStyle();
				Font font = style.getFont();
				font.setName("Arial");
				font.setBold(true);
				font.setColor(clr);
				cell.setStyle(style);
				
			}else{
				
			}
		}catch(Exception e){

			
		}
	
	}

please give solution.

thanks in advance
Raj.K


#2

@Raj1995Java,

See the following sample code for your needs for your reference. This is simple sample code for demonstration, you may refer to it and then update your code segment accordingly:
e.g
Sample code:

//Instantiating a Workbook object
		Workbook workbook = new Workbook();

		//Obtaining the reference of the newly added worksheet by passing its sheet index
		Worksheet worksheet = workbook.getWorksheets().get(0);

		//Accessing the "A1" cell from the worksheet
		Cell cell1 = worksheet.getCells().get("A1");

		//Adding some value to the "A1" cell
		cell1.putValue("Hello Aspose!");

		Style style = cell1.getStyle();
		
		Font font = style.getFont();

		//Setting the font name to "Times New Roman"
		font.setName("Arial");

		//Setting font size to 14
		font.setSize(5);

		//setting font color as Red
		font.setColor(com.aspose.cells.Color.getRed());
		
		//Set the style to A1 cell
	    cell1.setStyle(style);
		
		//Accessing the "A2" cell from the worksheet
		Cell cell2 = worksheet.getCells().get("A2");
		//Adding some value to the "A1" cell
		cell2.putValue(123);
	
		
		Style style2 = cell2.getStyle();
		style2.getFont().setColor(font.getColor());
		//Set the style to A2 cell
	    cell2.setStyle(style2);	
		
		System.out.println("A1 Cell has font color: " + cell1.getStyle().getFont().getColor());

		System.out.println("A2 Cell has font color: " + cell2.getStyle().getFont().getColor());

Hope, this helps a bit.