Knowledgebase (2311)
Children categories
Images are universally supported and can be seamlessly embedded and displayed in a variety of web platforms and documents. When you intend to publish Excel charts on websites or include them in videos or presentations, converting them to images is a practical option. By doing so, you can eliminate any potential compatibility issues and ensure that these charts are accessible to a wider audience. In this article, we will demonstrate how to convert Excel charts to images in Java using Spire.XLS for Java.
- Convert a Specific Chart in an Excel Worksheet to an Image in Java
- Convert All Charts in an Excel Worksheet to Images in Java
- Convert a Chart Sheet to an Image in Excel in Java
Install Spire.XLS for Java
First of all, 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.12.15</version>
</dependency>
</dependencies>
Convert a Specific Chart in an Excel Worksheet to an Image in Java
Spire.XLS for Java provides the Workbook.saveChartAsImage(Worksheet worksheet, int chartIndex) method to convert a specific chart in a worksheet as an image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet by its index using Workbook.getWorksheets().get(int index) method.
- Save a specific chart in the worksheet as an image using Workbook.saveChartAsImage(Worksheet worksheet, int chartIndex) method.
- Save the image to a PNG file.
- Java
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertAExcelChartToImage {
public static void main(String[] args) throws IOException {
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("Charts.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Save the first chart in the first worksheet as an image
BufferedImage image = workbook.saveChartAsImage(sheet, 0);
//Save the image to a .png file
ImageIO.write(image, "PNG", new File("output\\chart.png"));
workbook.dispose();
}
}

Convert All Charts in an Excel Worksheet to Images in Java
To convert all charts in an Excel worksheet to images, you can use the Workbook.saveChartAsImage(Worksheet worksheet) method. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet by its index using Workbook.getWorksheets().get(int index) method.
- Save all charts in the worksheet as images using Workbook.saveChartAsImage(Worksheet worksheet) method.
- Save the images to PNG files.
- Java
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertAllExcelChartsToImage {
public static void main(String[] args) throws IOException {
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("Charts.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Save all charts in the first worksheet as images
BufferedImage[] imgs = workbook.saveChartAsImage(sheet);
//Save the images to .png files
for (int i = 0; i < imgs.length; i++)
{
File file = new File("output\\" + String.format(("chart-%d.png"), i));
ImageIO.write(imgs[i], "PNG", file);
}
workbook.dispose();
}
}

Convert a Chart Sheet to an Image in Excel in Java
A chart sheet in Excel is a separate sheet dedicated solely to displaying a chart. You can use the Workbook.saveChartAsImage(ChartSheet chartSheet) method to convert a chart sheet in an Excel workbook to an image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific chart sheet by its index using Workbook.getChartsheets().get(int index) method.
- Save the chart sheet as an image using Workbook.saveChartAsImage(ChartSheet chartSheet) method.
- Save the image to a .png file.
- Java
import com.spire.xls.ChartSheet;
import com.spire.xls.Workbook;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertExcelChartSheetToImage {
public static void main(String[] args) throws IOException {
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("ChartSheet.xlsx");
//Get the first chart sheet
ChartSheet chartSheet = workbook.getChartsheets().get(0);
//Save the first chart sheet as an image
BufferedImage image = workbook.saveChartAsImage(chartSheet);
//Save the image to a .png file
ImageIO.write(image, "PNG", new File("output\\chartSheet.png"));
workbook.dispose();
}
}

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.
Comments can be added to almost every element in Word, such as text, images, charts, tables, etc. This article focuses on how to add comments to selected text (phrase) in a Word document 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>14.1.3</version>
</dependency>
</dependencies>
Add a Comment to a Specific Phrase
The Paragraph.appendComment() method is used to add comments to an entire paragraph. By default, the comment mark will be placed at the end of the paragraph. To add a comment to a specific phrase, you need to place comment marks (represented by the CommentMark class) at the beginning and end of the text range. The following are the steps to add a comment to a specific phrase.
- Create a Document object, and load the sample Word file using Document.loadFromFile() method.
- Find the specific string from the document using Document.findAllString() method.
- Create a comment start mark and a comment end mark, which will be placed at the beginning and end of the selected phrase respectively.
- Create a Comment object, specifying content and author.
- Get the oner paragraph of the selected phrase, and add the comment to the paragraph as a child object.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.CommentMark;
import com.spire.doc.documents.CommentMarkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.Comment;
public class AddCommentToSpecificText {
public static void main(String[] args) {
//Create a Document object
Document doc = new Document();
//Load the sample Word file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");
//Find the specific string to add comment
TextSelection[] finds = doc.findAllString("Spire.Doc for Java", false, true);
TextSelection specificText = finds[0];
//Create a start mark
CommentMark commentmarkStart = new CommentMark(doc);
commentmarkStart.setType(CommentMarkType.Comment_Start);
//Create an end mark
CommentMark commentmarkEnd = new CommentMark(doc);
commentmarkEnd.setType(CommentMarkType.Comment_End);
//Create a comment
Comment comment = new Comment(doc);
comment.getBody().addParagraph().setText("Developed by E-iceblue");
comment.getFormat().setAuthor("Shaun");
//Find the paragraph where the string is located
Paragraph para = specificText.getAsOneRange().getOwnerParagraph();
//Get the index of the string in the paragraph
int index = para.getChildObjects().indexOf(specificText.getAsOneRange());
//Add the comment to the paragraph
para.getChildObjects().add(comment);
//Insert the start mark and end mark to the paragraph based on the index
para.getChildObjects().insert(index, commentmarkStart);
para.getChildObjects().insert(index + 2, commentmarkEnd);
//Save to file
doc.saveToFile("AddComment.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.
Headers and footers in Excel are the text or images placed at the top and bottom of each page, respectively. These texts/images give basic information about the pages or document, such as the file name, company logo, page number, date/time, and so on. In this article, you will learn how to programmatically add text, images, as well as fields (like page number) to Excel headers or footers using Spire.XLS for Java.
Spire.XLS for Java provides the PageSetup class to work with the page setup in Excel including headers and footers. Specifically, it offers the setLeftHeader() method, setCenterHeader() method, setRightHeader() method, setLeftFooter() method, etc. to add content to the left section, center section and right section of a header or footer. To add fields to headers or footers, or to apply formatting to text, you'll need to use the scripts listed in the following table.
| Script | Description |
| &P | The current page numbers. |
| &N | The total number of pages. |
| &D | The current data. |
| &T | The current time. |
| &G | A picture. |
| &A | The worksheet name. |
| &F | The file name. |
| &B | Make text bold. |
| &I | Italicize text. |
| &U | Underline text. |
| &"font name" | Represents a font name, for example, &"Arial". |
| & + Integer | Represents font size, for example, &12. |
| &K + Hex color code | Represents font color, for example, &KFF0000. |
Install Spire.XLS for Java
First of all, 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.12.15</version>
</dependency>
</dependencies>
Add Images and Formatted Text to Header
The steps to add images and formatted text an Excel header using Spire.XLS for Java are as follows.
- Create a Workbook object.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet using Workbook.getWorksheets().get() method.
- Load an image using ImageIO.read() method.
- Set the image as the image source of the header’s left section using PageSetup.setLeftHeaderImage() method.
- Display image in the left header section by passing the value “&G” to PageSetup.setLeftHeader() method as a parameter.
- Save the workbook to another Excel file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
public class AddImageAndTextToHeader {
public static void main(String[] args) throws IOException {
//Create a Workbook object
Workbook wb = new Workbook();
//Load an existing Excel file
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Load an image
BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\your-logo.png"));
//Add image to header’s left section
sheet.getPageSetup().setLeftHeaderImage(bufferedImage,0.4f);
sheet.getPageSetup().setLeftHeader("&G");
//Add formatted text to header’s right section
sheet.getPageSetup().setRightHeader("&\"Calibri\"&B&10&K4253E2X Information Technology, Inc. \n www.xxx.com");
//Save the file
wb.saveToFile("output/Header.xlsx", ExcelVersion.Version2016);
}
}

Add the Current Date and Page Number to Footer
The following are the steps to add the current date and page number to an Excel footer using Spire.XLS for Java.
- Create a Workbook object.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet using Workbook.getWorksheets.get() method.
- Add page numbers with formatting to the footer’s left section by passing the value “&\"Calibri\"&B&10&K4253E2Page &P” to PageSetup.setLeftFooter() method. You can customize the page numbers’ formatting according to your preference.
- Add the current date to the footer’s right section by passing the value “&\"Calibri\"&B&10&K4253E2&D” to PageSetup.setRightFooter() method. Likewise, you can change the appearance of the date string as desired.
- Save the workbook to another Excel file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class AddDateAndPageNumberToFooter {
public static void main(String[] args) {
//Create a Workbook object
Workbook wb = new Workbook();
//Load an existing Excel file
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Add page number to footer's left section
sheet.getPageSetup().setLeftFooter("&\"Calibri\"&B&10&K4253E2Page &P");
//Add current date to footer's right section
sheet.getPageSetup().setRightFooter("&\"Calibri\"&B&10&K4253E2&D");
//Save the file
wb.saveToFile("output/Footer.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.