import 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 ExcelChartExample03 {

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

            // 创建折线图
            XSSFDrawing drawing = sheet2.createDrawingPatriarch();
            XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 3, 9, 9, 19);
            XSSFChart chart = drawing.createChart(anchor);
            chart.setTitleText('数据折线图');
            chart.setTitleOverlay(false);

            // 设置图例位置
            XDDFChartLegend legend = chart.getOrAddLegend();
            legend.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, 85, 0, 0));
            XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 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));

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

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

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

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

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

这段代码演示了如何使用 Apache POI 库在 Java 中创建 Excel 折线图。它涵盖以下步骤:

  1. 读取 Excel 文件: 使用 FileInputStreamXSSFWorkbook 读取现有的 Excel 文件。
  2. 获取数据: 使用 XSSFSheetCellRangeAddress 选择包含图表数据的单元格范围。
  3. 创建图表对象: 使用 XSSFDrawing, XSSFClientAnchorXSSFChart 创建图表对象并设置其位置和标题。
  4. 配置图表元素: 添加图例、轴和数据系列,并设置其格式和属性。
  5. 绘制图表: 使用 chart.plot(data) 方法将数据绘制到图表上。
  6. 保存文件: 使用 FileOutputStream 将修改后的 Excel 文件保存到磁盘。

请注意,此代码示例将图表保存到与源数据相同的 Excel 文件中。您也可以修改代码以将图表保存到新的 Excel 文件或其他支持的格式,例如 PNG 或 JPEG 图像。

Java Apache POI 生成 Excel 折线图

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

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