This content originally appeared on DEV Community and was authored by Jane
In my previous posts, I've introduced how to create an Excel pivot table with the 3rd party library--Free Spire.XLS for Java. This article will demonstrates how to create a pivot chart based on the data in pivot table.
Installation
Method 1: Download the Free Spire.XLS for Java and unzip it, then add the Spire.Xls.jar file to your project as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
Java Code:
import com.spire.xls.*;
import com.spire.xls.core.IPivotTable;
public class CreatePivotChart {
public static void main(String[] args) {
//Load the Excel file
Workbook workbook = new Workbook();
workbook.loadFromFile("data.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//get the first pivot table in the worksheet
IPivotTable pivotTable = sheet.getPivotTables().get(0);
//Add a clustered column chart based on the pivot table data
Chart chart = sheet.getCharts().add(ExcelChartType.ColumnClustered, pivotTable);
//Set chart position
chart.setTopRow(14);
chart.setBottomRow(25);
//Set chart title
chart.setChartTitle("Total");
//Save the result file
workbook.saveToFile("CreatPivotChart.xlsx", ExcelVersion.Version2013);
}
}
This content originally appeared on DEV Community and was authored by Jane
Jane | Sciencx (2021-03-12T08:02:04+00:00) Create Pivot Chart in Excel using Java. Retrieved from https://www.scien.cx/2021/03/12/create-pivot-chart-in-excel-using-java/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.