import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.util.IOUtils;
import java.io.*;

public class ExcelLineChartExample {
    public static void main(String[] args) {
        try {
            // 读取Excel文件
            FileInputStream file = new FileInputStream(new File('input-1.xlsx'));
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheet('P1');

            // 创建折线图
            XSSFDrawing drawing = sheet.createDrawingPatriarch();
            XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 1, 12, 15);
            XSSFChart chart = drawing.createChart(anchor);
            XSSFChartLegend legend = chart.getOrCreateLegend();
            legend.setPosition(LegendPosition.BOTTOM);

            // 设置横坐标轴
            XSSFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
            bottomAxis.setTitle('时间');

            // 设置纵坐标轴
            XSSFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
            leftAxis.setTitle('数值');

            // 创建数据源
            int numRows = 127;
            int numCols = 3;
            String[] categories = new String[numRows];
            Double[][] values = new Double[numCols][numRows];

            for (int i = 0; i < numRows; i++) {
                Row row = sheet.getRow(i);
                Cell categoryCell = row.getCell(0);
                categories[i] = categoryCell.getStringCellValue();

                for (int j = 0; j < numCols; j++) {
                    Cell valueCell = row.getCell(j + 2);
                    values[j][i] = valueCell.getNumericCellValue();
                }
            }

            // 添加数据
            XSSFChartData data = chart.getChartDataFactory().createLineChartData();
            for (int i = 0; i < numCols; i++) {
                XSSFLineChartData.Series series = data.addSeries(bottomAxis, leftAxis);
                series.setTitle(sheet.getRow(0).getCell(i + 2).getStringCellValue(), null);
                series.setCategoryData(new String[]{});
                series.setValues(new Double[]{});
                series.setCategoryData(categories);
                series.setValues(values[i]);
            }

            // 设置图表标题
            chart.setTitleText('折线图');
            chart.setTitleOverlay(false);

            // 保存Excel文件
            FileOutputStream outFile = new FileOutputStream(new File('input-1.xlsx'));
            workbook.write(outFile);
            outFile.close();

            System.out.println('折线图已生成并保存在P1工作表中。');

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

这段代码使用 Apache POI 5.2.2 读取名为 'input-1.xlsx' 的Excel文件,并读取 'P1' 工作表中的数据生成折线图。

代码解释:

  1. 导入必要的类库
  2. 读取Excel文件:使用FileInputStream读取文件,并使用XSSFWorkbook创建工作簿对象。
  3. 获取工作表:使用getSheet()方法获取名为 'P1' 的工作表。
  4. 创建折线图:使用createDrawingPatriarch()创建绘图对象,使用createAnchor()创建图表锚点,使用createChart()创建图表对象。
  5. 设置图表元素:设置图例位置、横坐标轴标题、纵坐标轴标题等。
  6. 读取数据:从工作表中读取数据,存储到数组中。
  7. 添加数据到图表:创建数据系列,设置数据系列的类别和数值。
  8. 设置图表标题
  9. 保存Excel文件:使用FileOutputStream创建输出流,使用write()方法将工作簿写入文件。

注意

  • 将代码中的文件路径替换为实际的文件路径。
  • 确保Excel文件中存在名为 'P1' 的工作表,并且数据格式正确。
  • 运行代码后,生成的折线图将保存在原始的 'input-1.xlsx' 文件的 'P1' 工作表中。
Java POI 5.2.2 读取Excel生成折线图,并保存到指定工作表

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

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