要实现你的要求,你可以使用Apache POI库来读取Excel文件并生成折线图。以下是一个示例代码,可以帮助你开始:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

public class ExcelLineChartExample {
    public static void main(String[] args) {
        String filePath = "input-1.xlsx";
        String sheetName = "P1";
        
        try {
            // 读取Excel文件
            FileInputStream file = new FileInputStream(filePath);
            Workbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheet(sheetName);
            
            // 创建折线图
            Drawing drawing = sheet.createDrawingPatriarch();
            ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
            Chart chart = drawing.createChart(anchor);
            ChartLegend legend = chart.getOrCreateLegend();
            legend.setPosition(LegendPosition.BOTTOM);
            
            LineChartData data = chart.getChartDataFactory().createLineChartData();
            
            // 设置横坐标轴
            ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
            bottomAxis.setTitle("时间");
            bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
            
            // 设置纵坐标轴
            ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
            leftAxis.setTitle("数值");
            leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
            
            // 读取数据并添加到折线图中
            Row row = sheet.getRow(0);
            for (int i = 0; i < 127; i++) {
                Cell xCell = row.getCell(i);
                Cell yCell = sheet.getRow(2).getCell(i);
                double xValue = xCell.getNumericCellValue();
                double yValue = yCell.getNumericCellValue();
                
                data.addSeries(
                    chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM),
                    chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT),
                    chart.getChartDataFactory().createCategoryDataSource(sheet, new CellRangeAddress(0, 0, i, i)),
                    chart.getChartDataFactory().createNumericDataSource(sheet, new CellRangeAddress(2, 2, i, i))
                );
            }
            
            chart.plot(data, bottomAxis, leftAxis);
            
            // 保存Excel文件
            FileOutputStream outFile = new FileOutputStream(filePath);
            workbook.write(outFile);
            outFile.close();
            
            System.out.println("折线图已生成并保存在P1工作表中。");
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请确保你已经将Apache POI库添加到你的项目依赖中。这段代码会读取名为"input-1.xlsx"的Excel文件中的"P1"工作表,并根据你的要求生成折线图,然后将结果保存回原始文件。

我本身就有input-1xlsx的Excel文件文件中本身就包含P1工作表我要的只是一个读取内容然后按照我的要求制作一个折线图就好Java代码。我的要求将A1-A127做为横向坐标轴横向坐标轴格式为时间C1-C127为竖向坐标轴生成的折线图保存在P1工作表就可以了

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

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