使用Apache POI创建Excel折线图教程

本教程将指导您使用Java和Apache POI库在Excel工作表中创建一个折线图。我们将涵盖以下内容:

  • 读取现有的Excel文件* 创建折线图* 设置图表标题和图例* 设置日期坐标轴和时间格式* 添加多个数据系列* 设置次要纵坐标轴* 保存修改后的Excel文件

以下是完整的Java代码示例:javaimport org.apache.poi.ss.usermodel.;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;import java.text.SimpleDateFormat;import java.util.Date;

public class ExcelChartExample05 {

public static void main(String[] args) {        try {            // 读取Excel文件            FileInputStream fileIn = new FileInputStream('input.xlsx');            XSSFWorkbook workbook = new XSSFWorkbook(fileIn);            XSSFSheet sheet1 = workbook.getSheet('P1');

        // 创建折线图            XSSFDrawing drawing = sheet1.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);

        // 设置横坐标轴为日期坐标轴            XDDFDateAxis bottomAxis = chart.createDateAxis(AxisPosition.BOTTOM);            bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);            bottomAxis.setTitle('时间');

        // 设置时间格式            SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy-MM-dd');            for (int i = 1; i <= 66; i++) {                XSSFRow row = sheet1.getRow(i);                if (row != null) {                    Cell cell = row.getCell(0);                    if (cell != null && cell.getCellType() == CellType.NUMERIC) {                        Date date = cell.getDateCellValue();                        String formattedDate = dateFormat.format(date);                        bottomAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO);                    }                }            }

        // 设置纵坐标轴            XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);            leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);            leftAxis.setTitle('数据值');

        // 设置次要纵坐标轴            XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);            rightAxis.setCrosses(AxisCrosses.MAX);            rightAxis.setTitle('次要数据值');

        // 设置数据源            XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 66, 0, 0));            XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 66, 1, 1));            XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 66, 2, 2));            XDDFNumericalDataSource<Double> ys3 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 66, 2, 2));

        // 添加数据系列            XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);            XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1);            series1.setTitle('P1数据折线图', null);            series1.setSmooth(false);            series1.setMarkerStyle(MarkerStyle.CIRCLE);            XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs1, ys2);            series2.setTitle('P1数据折线图', null);            series2.setSmooth(false);            series2.setMarkerStyle(MarkerStyle.CIRCLE);

        // 添加次要数据系列            XDDFLineChartData.Series series3 = (XDDFLineChartData.Series) data.addSeries(xs1, ys3);            series3.setTitle('次要数据折线图', null);            series3.setSmooth(false);            series3.setMarkerStyle(MarkerStyle.CIRCLE);            // 获取次要数据系列的序列标识符            long series3AxId = chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(2).getAxIdArray(0).getVal();            // 添加次要纵坐标轴到图表            chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(series3AxId);

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

        // 保存Excel文件            FileOutputStream fileOut = new FileOutputStream('input.xlsx');            workbook.write(fileOut);            fileOut.close();

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

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

代码说明:

  1. 首先,我们使用FileInputStream读取现有的Excel文件,并使用XSSFWorkbook创建一个工作簿对象。2. 然后,我们使用createDrawingPatriarch()方法创建一个绘图管理器,并使用createAnchor()方法创建一个图表锚点。3. 接下来,我们使用createChart()方法创建一个图表对象,并设置图表标题和图例位置。4. 为了创建日期坐标轴,我们使用createDateAxis()方法,并使用setTickLabelPosition()方法设置时间格式。5. 我们使用createValueAxis()方法创建纵坐标轴和次要纵坐标轴。6. 然后,我们使用fromNumericCellRange()方法创建数据源,并使用addSeries()方法将数据系列添加到图表中。7. 最后,我们使用plot()方法绘制图表,并将修改后的工作簿保存到文件中。

希望本教程能够帮助您使用Apache POI库在Excel中创建折线图!

使用Apache POI创建Excel折线图教程

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

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