Knowledgebase (2300)
When working with an Excel document that contains a lot of data, setting color or pattern for selected cells can make it very easy for users to locate specific types of information. In Microsoft Excel, you can achieve this function by simply clicking the "Fill Color" button on the formatting toolbar. In this article, you will learn how to programmatically set background color and pattern style for a specified cell or cell range in Excel using Spire.XLS for Java.
Install Spire.XLS for Java
First, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. 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.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>15.11.3</version>
</dependency>
</dependencies>
Set Background Color and Pattern for Excel Cells
The detailed steps are as follows.
- Create a Workbook object.
- Load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Get a specified cell range using Worksheet.getRange().get() method.
- Set background color for the specified cell range using CellRange.getStyle().setColor() method.
- Set fill pattern style for the specified cell range using CellRange.getStyle().setFillPattern() method.
- Save the result to another file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*;
import java.awt.*;
public class CellBackground {
public static void main(String[] args) {
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel document
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");
//Get the first worksheet
Worksheet worksheet= workbook.getWorksheets().get(0);
//Set background color for range "A1:E1" and "A2:A10"
worksheet.getRange().get("A1:E1").getStyle().setColor(Color.green);
worksheet.getRange().get("A2:A10").getStyle().setColor(Color.yellow);
//Set background color for cell E8
worksheet.getRange().get("E8").getStyle().setColor(Color.red);
//Set fill pattern style for range "C4:D5"
worksheet.getRange().get("C4:D5").getStyle().setFillPattern(ExcelPatternType.Percent25Gray);
//Save the document
workbook.saveToFile("CellBackground.xlsx", ExcelVersion.Version2013);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
An XML file is a plain text file that uses custom tags to display a document's structure and other features. In daily work, you sometimes need to convert Word to XML for storing and organizing data, or convert XML to Word for working on them more easily and efficiently. This article will demonstrate how to programmatically convert XML to Word using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. 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.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.11.2</version>
</dependency>
</dependencies>
Convert XML to Word
The following are steps to convert XML to Word using Spire.Doc for Java.
- Create a Document instance.
- Load an XML sample document using Document.loadFromFile() method.
- Save the document as a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class XMLToWord {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load an XML sample document
document.loadFromFile(sample.xml");
//Save the document to Word
document.saveToFile("output/XMLToWord.docx", FileFormat.Docx );
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
By inserting a page break into your Word document, you can end a page at the place you want and begin a new page at once without hitting the enter key repeatedly. In this article, we will demonstrate how to insert page breaks into a Word document in Java using Spire.Doc for Java library.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. 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.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.11.2</version>
</dependency>
</dependencies>
Insert Page Break after a Specific Paragraph
The following are the steps to insert page break after a specific paragraph:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Get the desired section using Document.getSections().get(sectionIndex) method.
- Get the desired paragraph using Section.getParagraphs().get(paragraphIndex) method.
- Add a page break to the paragraph using Paragraph.appendBreak(BreakType.Page_Break) method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.FileFormat;
public class InsertPageBreakAfterParagraph {
public static void main(String[] args){
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("Sample.docx");
//Get the first section
Section section = document.getSections().get(0);
//Get the 2nd paragraph in the section
Paragraph paragraph = section.getParagraphs().get(1);
//Append a page break to the paragraph
paragraph.appendBreak(BreakType.Page_Break);
//Save the result document
document.saveToFile("InsertPageBreak.docx", FileFormat.Docx_2013);
}
}

Insert Page Break after a Specific Text
The following are the steps to insert a page break after a specific text:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Find a specific text using Document.findString() method.
- Access the text range of the searched text using TextSelection.getAsOneRange() method.
- Get the paragraph where the text range is located using ParagraphBase.getOwnerParagraph() method.
- Get the position index of the text range in the paragraph using Paragraph.getChildObjects().indexOf() method.
- Initialize an instance of Break class to create a page break.
- Insert the page break after the searched text using Paragraph.getChildObjects().insert() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.Break;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
public class InsertPageBreakAfterText {
public static void main(String[] args){
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("Sample.docx");
//Search a specific text
TextSelection selection = document.findString("celebration", true, true);
//Get the text range of the seached text
TextRange range = selection.getAsOneRange();
//Get the paragraph where the text range is located
Paragraph paragraph = range.getOwnerParagraph();
//Get the position index of the text range in the paragraph
int index = paragraph.getChildObjects().indexOf(range);
//Create a page break
Break pageBreak = new Break(document, BreakType.Page_Break);
//Insert the page break after the searched text
paragraph.getChildObjects().insert(index + 1, pageBreak);
//Save the result document
document.saveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx_2013);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.