473,287 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,287 developers and data experts.

One of the easiest ways to set the background color of Excel documents in Java

Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important information or to differentiate between different sections of the document. Additionally, setting background colors can help to improve accessibility for users with visual impairments. What’s far more significant is that setting the background color through programming can be automated, improve repeatability and accuracy, and have greater flexibility. These advantages can help improve work efficiency and document quality, especially for tasks that require large amounts of data or frequent updates.

Programming Environment

Method 1:
Introduced manually. Download Free Spire.XLS for Java locally, unzip it, and find the Spire.Xls.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:


If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.
Expand|Select|Wrap|Line Numbers
  1. <repositories>
  2.     <repository>
  3.         <id>com.e-iceblue</id>
  4.         <name>e-iceblue</name>
  5.         <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
  6.     </repository>
  7. </repositories>
  8. <dependencies>
  9.     <dependency>
  10.         <groupId>e-iceblue</groupId>
  11.         <artifactId>spire.xls.free</artifactId>
  12.         <version>5.1.0</version>
  13.     </dependency>
  14. </dependencies>
Set the Background Color of Excel Documents in Java

Free Spire.XLS for Java offers Worksheet.getAllocatedRange().getStyle().setColor( ) method and Worksheet.getCellRange().getStyle().setColor() method to set background color of Excel Documents. More details are listed below.

Step 1: Create a Workbook instance.
Step 2: Load a sample Excel file using Workbook.loadFromFile() method.
Step 3: Get a specific worksheet from the workbook using Workbook.getWorksheets.get(index) method.
Step 4: Using Worksheet.getAllocatedRange().getStyle().setColor( ) method to set background color for the used cell range or Worksheet.getCellRange().getStyle().setColor() method to set background color for a specified cell range in the worksheet.
Step 5: Save the result file using Workbook.saveToFile() method.

Full Code:
Java
Expand|Select|Wrap|Line Numbers
  1. import com.spire.xls.ExcelVersion;
  2. import com.spire.xls.Workbook;
  3. import com.spire.xls.Worksheet;
  4.  
  5. import java.awt.*;
  6.  
  7. public class BackgroundColor{
  8.     public static void main(String[] args){
  9.         //Create a Workbook instance
  10.         Workbook workbook = new Workbook();
  11.         //Load an Excel file
  12.         workbook.loadFromFile("input.xlsx");
  13.  
  14.         //Get the first worksheet
  15.         Worksheet sheet = workbook.getWorksheets().get(0);
  16.         //Set background color for the used cell range in the worksheet
  17.         sheet.getAllocatedRange().getStyle().setColor(Color.yellow);
  18.         //Set background color for a specified cell range in the worksheet
  19.         //sheet.getCellRange("A1:E19").getStyle().setColor(Color.blue);
  20.  
  21.         //Save the file
  22.         workbook.saveToFile("SetBackColor.xlsx", ExcelVersion.Version2013);
  23.     }
  24. }
  25.  
  26.  
Effective Shot


Conclusion:

In this post, you have learned how to set the background color of Excel documents in Java. Not only that, we also have other functions, such as, Java: Merge Excel Files into OneJava: Apply Color to Alternate Rows in Excel Using Conditional Formatting and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.XLS for Java.
Jun 2 '23 #1
0 11501

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: James | last post by:
I'm trying to make a select box where each of 3 elements shows up as a different color. I've gotten it working on the PC, but seem to be having some troubles with the Mac/IE combo. Apparently...
4
by: coolsti | last post by:
I have been playing around with this task for some hours now, and can't seem to find a way. I have an assortment of buttons on a web page, which have their background colors defined via style...
6
by: Nancy Carter | last post by:
I'm trying to set the background color of a frameset to black. The following code doesn't work in either IE 6.0 or Firefox. Any ideas why? <frameset bgcolor="#000000" frameborder="0" border="0"...
4
by: Lewis Edward Moten III | last post by:
I have a file that users can download through a web page protected by forms authentication: Download.aspx?ID=45 and within that file ... FileInfo fileToDownload = new FileInfo(fileName);
1
by: cut123 | last post by:
I have a query that I would like to export to different Excel documents based on the product. For each type of product I want all pertinent data to export to a different Excel document. There will...
2
by: patrick.waldo | last post by:
Hi all, I am trying to figure out a way to read colors with xlrd, but I did not understand the formatting.py module. Basically, I want to sort rows that are red or green. My initial attempt...
19
by: david.karr | last post by:
If in my CSS I set the "background-color" property on the "body" element, it only covers the background of the elements defined in the body, up to the current width and height of the page. However,...
0
by: lenniekuah | last post by:
Hullo Good Friends, I am using VBNET2008 and Excel 2003. I need your help. Please help me. I am trying to develop coding to specific set the Excel 2003 Row BackGround Color to Yellow and it's...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.