Java Apache POI 创建 Excel 折线图详解
Java Apache POI 创建 Excel 折线图详解
本教程将指导您使用 Apache POI 库在 Java 中创建 Excel 折线图。我们将详细介绍如何添加数据系列、自定义轴、设置图例和标题,并解决常见问题,例如图表无法正确显示。
1. 准备工作
在开始之前,请确保您已设置好以下内容:
-
添加 Apache POI 依赖项: 将 Apache POI 库添加到您的项目中。您可以从 Maven 存储库中下载 JAR 文件,或者将以下依赖项添加到您的
pom.xml文件中:xmlorg.apache.poi poi 5.2.3 org.apache.poi poi-ooxml 5.2.3 -
准备 Excel 文件: 创建一个包含您要用于创建图表的数据的 Excel 文件。
2. 创建 Java 项目
创建一个新的 Java 项目,并将 Apache POI 库添加到您的项目中。
3. 编写 Java 代码
以下代码演示了如何使用 Apache POI 库在 Java 中创建一个简单的折线图:javaimport org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellType;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xddf.usermodel.chart.*;import org.apache.poi.xssf.usermodel.XSSFClientAnchor;import org.apache.poi.xssf.usermodel.XSSFDrawing;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer;
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;
public class ExcelChartExample04 {
public static void main(String[] args) { try { // 读取 Excel 文件 FileInputStream fileIn = new FileInputStream('input-1.xlsx'); XSSFWorkbook workbook = new XSSFWorkbook(fileIn); XSSFSheet sheet1 = workbook.getSheet('P1');
// 创建折线图 XSSFDrawing drawing = sheet1.createDrawingPatriarch(); XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 129, 10, 149); XDDFChart chart = drawing.createChart(anchor); chart.setTitleText('数据折线图'); chart.setTitleOverlay(false);
// 设置图例位置 XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(LegendPosition.BOTTOM);
// 设置横坐标轴为日期坐标轴 XDDFDataSource<?> dateSource = XDDFDataSourcesFactory.fromArray(new String[] {'A2:A127'}); XDDFDateAxis bottomAxis = chart.createDateAxis(AxisPosition.BOTTOM); bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO); bottomAxis.setTitle('时间'); bottomAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO);
// 设置时间格式 SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy/MM/dd'); for (int i = 0; i < dateSource.getPointCount(); i++) { Row row = sheet1.getRow(i + 1); if (row != null) { Cell cell = row.getCell(0); if (cell != null && cell.getCellType() == CellType.NUMERIC) { Date date = cell.getDateCellValue(); String formattedDate = dateFormat.format(date); cell.setCellValue(formattedDate); } } }
// 设置左侧坐标轴为温度坐标轴 XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); leftAxis.setTitle('温度');
// 设置右侧坐标轴为数值坐标轴 XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT); rightAxis.setCrosses(AxisCrosses.MAX); rightAxis.setTitle('数据值');
// 设置数据源 XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 127, 0, 0)); XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 127, 1, 1)); XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 127, 2, 2));
// 添加数据系列 XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis); XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1); series1.setTitle('折线图1', null); series1.setSmooth(false); series1.setMarkerStyle(MarkerStyle.NONE);
// 添加第二个数据系列 XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs1, ys2); series2.setTitle('折线图2', null); series2.setSmooth(false); chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).getOrder().setVal(1);
// 设置次要垂直坐标轴 CTLineSer ctLineSer = chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1); ctLineSer.addNewDLbls(); ctLineSer.getDLbls().addNewShowLegendKey().setVal(false); ctLineSer.getDLbls().addNewShowVal().setVal(false); ctLineSer.getDLbls().addNewShowCatName().setVal(false); ctLineSer.getDLbls().addNewShowSerName().setVal(false);
// 保存 Excel 文件 FileOutputStream fileOut = new FileOutputStream('input-1.xlsx'); workbook.write(fileOut); fileOut.close();
System.out.println('折线图已创建并保存到 Excel 文件中。');
} catch (IOException e) { e.printStackTrace(); } }}
4. 运行代码
运行上述代码,将在您的项目目录中创建一个名为 input-1.xlsx 的 Excel 文件,其中包含一个名为 数据折线图 的折线图。
5. 常见问题
- 图表无法正确显示: 确保您已正确设置数据源,并且数据范围与代码中指定的范围匹配。
6. 总结
本教程介绍了如何使用 Apache POI 库在 Java 中创建 Excel 折线图。您可以使用此库来自定义图表的各个方面,例如数据系列、轴、图例和标题。
原文地址: https://www.cveoy.top/t/topic/fSGe 著作权归作者所有。请勿转载和采集!