package 折线图;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFChart;
import org.apache.poi.hssf.usermodel.HSSFDrawing;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFChartTitle;
import org.apache.poi.hssf.usermodel.HSSFLegend;
import org.apache.poi.hssf.usermodel.HSSFChartLegend;
import org.apache.poi.hssf.usermodel.HSSFAxis;
import org.apache.poi.hssf.usermodel.HSSFCategoryAxis;
import org.apache.poi.hssf.usermodel.HSSFValueAxis;
import org.apache.poi.hssf.usermodel.HSSFLineChartData;
import org.apache.poi.hssf.usermodel.HSSFLineChartData.Series;
import org.apache.poi.hssf.usermodel.HSSFDataSources;
import org.apache.poi.ss.usermodel.ChartTypes;
import org.apache.poi.ss.usermodel.AxisPosition;
import org.apache.poi.ss.usermodel.AxisCrosses;

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');
            HSSFWorkbook workbook = new HSSFWorkbook(fileIn);
            HSSFSheet sheet1 = workbook.getSheet('P9');
            HSSFSheet sheet2 = workbook.getSheet('P10');
            HSSFSheet sheet3 = workbook.getSheet('P11');

            // 创建折线图
            HSSFDrawing drawing = sheet1.createDrawingPatriarch();
            HSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 1, 10, 20);
            HSSFChart chart = drawing.createChart(anchor);
            HSSFChartTitle title = chart.getTitle();
            title.setText('数据折线图');
            chart.setTitle(title);
            chart.setHasTitle(true);

            // 设置图例位置
            HSSFLegend legend = chart.getOrCreateLegend();
            legend.setPosition(HSSFChartLegend.BOTTOM);

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

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

            // 添加数据系列
            HSSFLineChartData data = (HSSFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
            Series series1 = data.addSeries(xs1, ys1);
            series1.setTitle('数据折线图1', null);
            Series series2 = data.addSeries(xs2, ys2);
            series2.setTitle('数据折线图2', null);
            Series series3 = 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();
        }
    }
}
Java Apache POI 生成 Excel 折线图教程

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

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