使用Apache POI创建Excel折线图

本教程将演示如何使用Apache POI库在Java中创建Excel折线图。

1. 添加依赖

首先,确保你的项目中包含了Apache POI的依赖。你可以在Maven或Gradle项目中添加以下依赖:xml org.apache.poi poi 5.2.3 org.apache.poi poi-ooxml 5.2.3

2. 代码示例

以下是一个完整的Java代码示例,演示了如何创建包含三个数据系列的折线图:javaimport org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xddf.usermodel.chart.;import org.apache.poi.xssf.usermodel.;

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;

public class ExcelChartExample01 {

public static void main(String[] args) {        try {            // 读取Excel文件            FileInputStream fileIn = new FileInputStream('巴新EDEVU水库数据处理表.xls');            XSSFWorkbook workbook = new XSSFWorkbook(fileIn);            XSSFSheet sheet1 = workbook.getSheet('P9');            XSSFSheet sheet2 = workbook.getSheet('P10');            XSSFSheet sheet3 = workbook.getSheet('P11');

        // 创建折线图            XSSFDrawing drawing = sheet1.createDrawingPatriarch();            XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 1, 10, 20);            XSSFChart chart = drawing.createChart(anchor);            chart.setTitleText('数据折线图');            chart.setTitleOverlay(false);

        // 设置图例位置            chart.getOrAddLegend().setPosition(LegendPosition.BOTTOM);

        // 创建数据系列            XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);            XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);            leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        // 创建数据源            XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 86, 0, 0));            XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 86, 2, 2));            XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 0, 0));            XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 2, 2));            XDDFNumericalDataSource<Double> xs3 = XDDFDataSourcesFactory.fromNumericCellRange(sheet3, new CellRangeAddress(11, 159, 0, 0));            XDDFNumericalDataSource<Double> ys3 = XDDFDataSourcesFactory.fromNumericCellRange(sheet3, new CellRangeAddress(11, 159, 2, 2));

        // 添加数据系列            XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);            XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1);            series1.setTitle('数据折线图1', null);            XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs2, ys2);            series2.setTitle('数据折线图2', null);            XDDFLineChartData.Series series3 = (XDDFLineChartData.Series) data.addSeries(xs3, ys3);            series3.setTitle('数据折线图3', null);

        // 绘制图表            chart.plot(data);

        // 保存Excel文件            FileOutputStream fileOut = new FileOutputStream('巴新EDEVU水库数据处理表.xls');            workbook.write(fileOut);            fileOut.close();

        System.out.println('折线图已创建并保存到Excel文件中。');

    } catch (IOException e) {            e.printStackTrace();        }    }}

3. 代码解释

  • 代码首先加载一个名为'巴新EDEVU水库数据处理表.xls'的Excel文件,并获取要操作的三个工作表。- 然后,使用XSSFDrawingXSSFClientAnchor创建图表对象,并设置标题和图例位置。- 接下来,创建数据系列,包括设置X轴和Y轴的数据源,以及每个数据系列的标题。- 最后,将数据系列添加到图表中,并使用chart.plot(data)绘制图表。- 最后,将修改后的Excel文件保存到磁盘。

4. 总结

本教程介绍了如何使用Apache POI库在Java中创建Excel折线图。通过使用该库,可以轻松地将数据可视化,并生成专业的Excel报告。

使用Apache POI创建Excel折线图

原文地址: https://www.cveoy.top/t/topic/f3R2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录