Apache POI is the core tool for processing Office documents in the Java ecosystem, supporting dynamic generation and data binding of Excel charts. This article takes the POI version as an example to explain in detail how to create common charts such as line charts, bar charts, pie charts and other charts in Excel, and provides code examples and best practices.
1. Environment configuration and dependency management
Using POI to generate charts requires the following core dependencies (taking Maven as an example):
<dependency> <groupId></groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency>
Note: The POI version is quite different from the old version (such as) API, so dependency conflicts should be avoided.
2. Data source preparation and worksheet construction
Create workbooks and worksheets
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = ("Data table");
Fill in data
Taking the country's GDP data as an example, the first line writes the country name, and the subsequent line fills the value:
// Create a title lineRow headerRow = (0); (0).setCellValue("nation"); (1).setCellValue("Russia"); (2).setCellValue("China"); // Fill in the data rowsRow dataRow = (1); (0).setCellValue("GDP (trillion dollars)"); (1).setCellValue(1.78); (2).setCellValue(17.96);
3. Core steps for chart generation
1. Line Chart
// Define the chart position and sizeXSSFDrawing drawing = (); XSSFClientAnchor anchor = (0, 0, 0, 0, 0, 5, 10, 20); // Create a chart objectXSSFChart chart = (anchor); ("National GDP Trend Analysis"); // Bind the data sourceXDDFDataSource<String> countries = (sheet, new CellRangeAddress(0, 0, 1, 2)); XDDFNumericalDataSource<Double> values = (sheet, new CellRangeAddress(1, 1, 1, 2)); // Configure the axis and styleXDDFCategoryAxis xAxis = (); XDDFValueAxis yAxis = (); XDDFLineChartData data = (XDDFLineChartData) (, xAxis, yAxis); // Add data series and render series = (countries, values); ("GDP", null); (data);
Key points:passCellRangeAddress
Bind data area and support dynamic expansion
2. Bar Chart
The code structure of the bar chart is similar to that of the line chart, and you only need to modify the chart type and style:
XDDFBarChartData data = (XDDFBarChartData) (, xAxis, yAxis);
AvailableXDDFShapeProperties
Customize cylinder color and spacing
3. Pie Chart
The pie chart needs to set the data label and percentage display separately:
XDDFPieChartData data = (XDDFPieChartData) (, null, null); series = (countries, values); (data); // Enable the percentage tag().getPlotArea().getPieChartArray(0).addNewDLbls().addNewShowPercent().setVal(true);
4. Frequently Asked Questions and Optimization
1. Chart position offset
passXSSFClientAnchor
Parameter adjustment coordinates (e.g.col1, row1, col2, row2
) Control the chart position
2. Dynamic data update
useCellRangeAddress
Dynamically expand data range to avoid hard coding. For example:
CellRangeAddress valuesRange = new CellRangeAddress(1, 1, 1, (1).getLastCellNum());
3. Style customization
-
Axis title:
("nation")
-
Legend position:
().setPosition(LegendPosition.TOP_RIGHT)
-
Color settings:pass
XDDFSolidFillProperties
Specify RGB value
5. Summary
The core of generating Excel charts through POI isData bindingandFlexible API calls. Developers need to pay attention to:
- The data area must match the chart type (such as classification axis and numerical axis);
- Recommended use of higher version POI (≥5.0)
XDDF
Series API, more compatibility; - Complex chart recommendations for encapsulating tool classes (e.g.
ChartUtils
) Improve code reusability
This is the article about Java practical use of POI to generate Excel charts. This is all about this. For more related Java POI to generate Excel content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!