Knowledgebase (2300)
This article will introduce how to add a picture to Excel chart in java applications by using Spire.XLS for java.
import com.spire.xls.*;
import com.spire.xls.core.IPictureShape;
import com.spire.xls.core.IShape;
import java.awt.*;
public class addPictureToChart {
public static void main(String[] args) {
//Load the document from disk
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//get the first chart
Chart chart = sheet.getCharts().get(0);
//add the picture to chart and set its format
IShape picture = chart.getShapes().addPicture("48.png");
((IPictureShape) picture).getLine().setDashStyle(ShapeDashLineStyleType.DashDotDot);
((IPictureShape) picture).getLine().setForeColor(Color.blue);
//save the document
String result = "output/AddPictureToChart.xlsx";
workbook.saveToFile(result, ExcelVersion.Version2010);
}
}
Effective screenshot after adding picture to Excel Chart:

This article demonstrates how to mail merge image in Word document in Java using Spire.Doc for Java.
The template document:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.reporting.MergeImageFieldEventArgs;
import com.spire.doc.reporting.MergeImageFieldEventHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleMailMerge {
public static void main(String[] args) throws Exception {
//create a Document instance
Document document = new Document();
//load the template document
document.loadFromFile("template - Copy.docx");
//specify the merge field name
String[] filedNames = new String[]{"image"};
//specify the path of image
String[] filedValues = new String[]{"logo.png"};
//invoke the mail merge event to load image
document.getMailMerge().MergeImageField = new MergeImageFieldEventHandler() {
public void invoke(Object sender, MergeImageFieldEventArgs args) {
mailMerge_MergeImageField(sender, args);
}
};
//execute mail merge
document.getMailMerge().execute(filedNames, filedValues);
//save file
document.saveToFile("MailMergeImage.docx", FileFormat.Docx_2013);
}
//create a mail merge event to load image
private static void mailMerge_MergeImageField(Object sender, MergeImageFieldEventArgs field) {
String filePath = field.getImageFileName();
if (filePath != null && !"".equals(filePath)) {
try {
field.setImage(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
The output document:

If you have a lot of data in Excel, finding the special values can be challenging. In this situation, you can use the conditional formatting to automatically highlight the cells that contain the value that meets certain criteria. This article introduces how to highlight values that are above or below the average using conditional formatting in Java, 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>
Highlight Values Above or Below Average Excel in Java
Below are the steps to highlight values above or below average in Excel using Spire.XLS for Java.
- Create a Workbook object.
- Load an Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet from the workbook using Workbook.getWorksheets.get(index) method.
- Add a conditional formatting to the worksheet using Worksheet.getConditionalFormats().add() method and return an object of XlsConditionalFormats class.
- Set the cell range where the conditional formatting will be applied using XlsConditionalFormats.AddRange() method.
- Add an average condition using XlsConditionalFormats.addAverageCondition() method, specify the average type to above and change the background color of the cells that meet the condition to yellow.
- Add another average condition to change the background color of the cells that contain the value below average to light gray.
- Save the workbook to an Excel file using Workbook.saveToFile() method.
- Java
import com.spire.xls.AverageType;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class HighlightValuesAboveAndBelowAverage {
public static void main(String[] args) {
//Create a Workbook object
Workbook workbook = new Workbook();
//Load an Excel file
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Add a conditional format to the worksheet
XlsConditionalFormats format = sheet.getConditionalFormats().add();
//Set the range where the conditional format will be applied
format.addRange(sheet.getRange().get("F2:F14"));
//Add a condition to highlight values above average with yellow
IConditionalFormat condition1 = format.addAverageCondition(AverageType.Above);
condition1.setBackColor(Color.yellow);
//Add a condition to highlight values below average with light gray
IConditionalFormat condition2 = format.addAverageCondition(AverageType.Below);
condition2.setBackColor(Color.lightGray);
//Get the count of values below average
sheet.getRange().get("F17").setFormula("=COUNTIF(F2:F14,\"<\"&AVERAGE(F2:F14))");
//Get the count of values above average
sheet.getRange().get("F18").setFormula("=COUNTIF(F2:F14,\">\"&AVERAGE(F2:F14))");
//Save the workbook to an Excel file
workbook.saveToFile("output/HighlightValues.xlsx", ExcelVersion.Version2016);
}
}

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.