import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xddf.usermodel.chart.; import org.apache.poi.xssf.usermodel.;

import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; 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');
        XSSFSheet sheet3 = workbook.getSheet('温度');

        // 创建折线图
        XSSFDrawing drawing = sheet3.createDrawingPatriarch();
        XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 10, 20);
        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);

        // 绘制图表到图片
        BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        chart.plot(data);

        // 保存图片文件
        ImageIO.write(image, 'png', new File('折线图.png'));

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

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

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

}

// 解决办法内容:根据错误提示,plot方法的参数类型不正确。需要将chart.plot(data, new Rectangle2D.Double(0, 0, 800, 600));这行代码修改为chart.plot(data);即可。

// 修改后的代码如下:

package 折线图;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
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');
            XSSFSheet sheet3 = workbook.getSheet('温度');

            // 创建折线图
            XSSFDrawing drawing = sheet3.createDrawingPatriarch();
            XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 10, 20);
            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);

            // 绘制图表到图片
            BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
            image.createGraphics();
            Graphics2D graphics = (Graphics2D) image.getGraphics();
            chart.plot(data);

            // 保存图片文件
            ImageIO.write(image, 'png', new File('折线图.png'));

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

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

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Java Apache POI Excel 折线图生成教程

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

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