使用Apache POI在Excel中创建折线图

本文将指导您使用Java和Apache POI库在Excel工作簿中创建折线图。

问题

您可能遇到错误信息'java: 程序包org.apache.poi.hssf.usermodel.HSSFLineChartData不存在'。这是因为HSSFLineChartData类在较旧版本的Apache POI中不可用。

解决方法

要解决此问题,您需要使用XSSFLineChartData类,该类适用于较新版本的Apache POI。以下是更新后的代码:javaimport 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.xssf.usermodel.XSSFLineChartData; // 更新后的导入import org.apache.poi.xssf.usermodel.XSSFLineChartData.Series; // 更新后的导入import org.apache.poi.xssf.usermodel.XSSFDataSources; // 更新后的导入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);

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

        // 添加数据系列            XSSFLineChartData data = (XSSFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis); // 更新后的类            Series series1 = data.addSeries(xs1, ys1);            series1.setTitle('数据折线图1'); // 设置系列标题            Series series2 = data.addSeries(xs2, ys2);            series2.setTitle('数据折线图2'); // 设置系列标题            Series series3 = data.addSeries(xs3, ys3);            series3.setTitle('数据折线图3'); // 设置系列标题

        // 绘制图表            chart.plot(data, bottomAxis, leftAxis); // 更新后的方法调用

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

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

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

在此代码中,我们进行了以下更改:

  • 我们导入了XSSFLineChartDataSeriesXSSFDataSources类。- 我们将HSSFLineChartData的所有实例替换为XSSFLineChartData。- 我们将HSSFDataSources的所有实例替换为XSSFDataSources。- 我们为每个数据系列设置了标题。- 我们更新了chart.plot()方法调用以包括轴。

通过这些更改,代码现在应该可以正确编译并生成包含折线图的Excel文件。

使用Apache POI创建Excel折线图

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

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