Apache POI Excel 折线图 - Java 代码示例
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;
public class ExcelChartExample02 {
public static void main(String[] args) {
try {
// 读取Excel文件
FileInputStream fileIn = new FileInputStream('巴新EDEVU水库数据处理表.xlsx');
XSSFWorkbook workbook = new XSSFWorkbook(fileIn);
XSSFSheet sheet1 = workbook.getSheet('P9');
XSSFSheet sheet2 = workbook.getSheet('P10');
// 创建折线图
XSSFDrawing drawing = sheet2.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);
// 创建数据系列
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
// 创建数据源
XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 0, 0));
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 2, 2));
XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 0, 0));
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 2, 2));
// 添加数据系列
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1);
series1.setTitle('P9数据折线图', null);
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs2, ys2);
series2.setTitle('P10数据折线图', null);
// 绘制图表
chart.plot(data);
// 创建'温度'工作表
XSSFSheet sheet3 = workbook.createSheet('温度');
// 将图表复制到'温度'工作表
XSSFDrawing drawing2 = sheet3.createDrawingPatriarch();
XSSFClientAnchor anchor2 = drawing2.createAnchor(0, 0, 0, 0, 0, 0, 10, 20);
chart.plot(data, drawing2, anchor2);
// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream('巴新EDEVU水库数据处理表.xlsx');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
} catch (IOException e) {
e.printStackTrace();
}
}
}
java: 无法将类 org.apache.poi.xddf.usermodel.chart.XDDFChart中的方法 plot应用到给定类型; 需要: org.apache.poi.xddf.usermodel.chart.XDDFChartData 找到: org.apache.poi.xddf.usermodel.chart.XDDFLineChartData,org.apache.poi.xssf.usermodel.XSSFDrawing,org.apache.poi.xssf.usermodel.XSSFClientAnchor 原因: 实际参数列表和形式参数列表长度不同 解决办法内容:根据错误提示,plot方法需要的参数类型是XDDFChartData,而实际传入的是XDDFLineChartData、XSSFDrawing和XSSFClientAnchor。这说明plot方法的参数传递有误。
要解决这个问题,可以尝试使用不同的plot方法,或者检查导入的包是否正确。可以参考官方文档或其他资源,查看plot方法的正确使用方式。
原文地址: https://www.cveoy.top/t/topic/f38s 著作权归作者所有。请勿转载和采集!