Java Apache POI 生成 Excel 折线图教程
Java Apache POI 生成 Excel 折线图教程
本教程将引导你使用 Java 和 Apache POI 库在 Excel 文件中创建折线图。
1. 准备工作
- 确保你已安装 Java 开发环境。
- 下载 Apache POI 库并将其添加到你的项目中。
- 准备一份包含你要用来生成折线图数据的 Excel 文件。
2. 代码示例
以下代码示例展示了如何创建一个包含两个数据系列的折线图,并将图表放置在新的工作表 '温度' 中:
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, bottomAxis, leftAxis);
chart.plot(drawing2, bottomAxis, leftAxis);
// 保存 Excel 文件
FileOutputStream fileOut = new FileOutputStream('巴新EDEVU水库数据处理表.xlsx');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到 Excel 文件中。');
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 常见问题
问题: 出现 'java: 不兼容的类型: org.apache.poi.xssf.usermodel.XSSFDrawing无法转换为org.apache.poi.xddf.usermodel.chart.XDDFChartData' 错误。
解决办法: 将以下代码段:
chart.plot(data);
chart.plot(drawing2);
修改为:
chart.plot(data, bottomAxis, leftAxis);
chart.plot(drawing2, bottomAxis, leftAxis);
4. 总结
本教程介绍了如何使用 Java 和 Apache POI 库在 Excel 中创建折线图。通过修改代码示例中的数据源和图表参数,你可以轻松地创建满足自己需求的自定义折线图。
原文地址: http://www.cveoy.top/t/topic/f38H 著作权归作者所有。请勿转载和采集!