473,416 Members | 1,496 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,416 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 11690

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.