Casa > W > What Is The Code To Change Background Color Of Excel Cell In Java?

What is the code to change background color of Excel cell in Java?

Please see the following sample code that uses Aspose.Cells API to change the background color of the cell in Java.

The code creates an empty workbook, accesses the first worksheet, accesses the cell C4 and its style and using the style object, it then changes the background color of the cell to yellow.

Here is the sample code for your reference. Please read the comments inside the code for more understanding.

Java

  1. // Create empty workbook. 
  2. com.aspose.cells.Workbook wb = new Workbook(); 
  3.  
  4. // Access first worksheet 
  5. Worksheet ws = wb.getWorksheets().get(0); 
  6.  
  7. // Access cell C4 
  8. Cell c4 = ws.getCells().get("C4"); 
  9.  
  10. // Get the style of the cell 
  11. Style st = c4.getStyle(); 
  12. st.setPattern(BackgroundType.SOLID); 
  13. st.setForegroundColor(Color.getYellow()); 
  14. c4.setStyle(st); 
  15.  
  16. // Save the workbook in XLSX format 
  17. wb.save("D:/Download/Output.xlsx", SaveFormat.XLSX); 

The following screenshot shows the output Excel file (as you see cell C4 is filled with Yellow color) generated by the code.

main-qimg-c0a1b07b066e76f2d870e4c22e5b59c2

Thanks for reading. Hopefully, it will be helpful for you.

De Gorman

Como ser mais rápido a digitar com uma mão :: O iPhone 11 Pro precisa de vidro temperado?