使用 Apache POI 在 Excel 中创建折线图
使用 Apache POI 在 Excel 中创建折线图
本教程介绍如何使用 Apache POI 库在 Excel 工作簿中创建折线图。它展示了如何添加数据系列、设置图表标题和轴,以及将图表保存到 Excel 文件中。
导入必要的类
首先,导入必要的 Apache POI 类:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFChartLegend;
import org.apache.poi.xssf.usermodel.XSSFChartAxis;
import org.apache.poi.xssf.usermodel.XSSFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFCategoryAxis;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.LineChartData;
import org.apache.poi.xddf.usermodel.chart.LineChartData.Series;
import org.apache.poi.xddf.usermodel.chart.LineChart;
import org.apache.poi.ss.usermodel.DataSources;
import org.apache.poi.ss.usermodel.ChartDataSource;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
创建 Excel 工作簿和工作表
在示例中,我们假设要将折线图添加到名为 '巴新EDEVU水库数据处理表.xls' 的 Excel 文件中的名为 'P9' 的工作表。
// 读取 Excel 文件
FileInputStream fileIn = new FileInputStream('巴新EDEVU水库数据处理表.xls');
Workbook workbook = new XSSFWorkbook(fileIn);
Sheet sheet = workbook.getSheet('P9');
创建折线图
现在,使用以下代码创建一个折线图:
// 创建折线图
XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 1, 10, 20);
XSSFChart chart = drawing.createChart(anchor);
XSSFChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.BOTTOM);
创建数据系列
下一步,创建数据系列。这里,我们将使用来自工作表 'P9' 中的单元格范围 'A11:A85' 的数据作为 X 轴,以及来自单元格范围 'C11:C85' 的数据作为 Y 轴:
// 创建数据系列
XSSFChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
XSSFValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
// 创建数据源
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(11, 85, 0, 0));
ChartDataSource<Number> ys = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(11, 85, 2, 2));
添加数据系列到图表
最后,使用以下代码将数据系列添加到图表中:
// 添加数据系列
LineChartData data = chart.getChartDataFactory().createLineChartData();
Series series = data.addSeries(xs, ys);
series.setTitle('数据折线图');
// 绘制图表
chart.plot(data);
保存 Excel 文件
最后,使用以下代码将包含折线图的 Excel 文件保存到磁盘:
// 保存 Excel 文件
FileOutputStream fileOut = new FileOutputStream('巴新EDEVU水库数据处理表.xls');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
完整代码
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFChartLegend;
import org.apache.poi.xssf.usermodel.XSSFChartAxis;
import org.apache.poi.xssf.usermodel.XSSFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFCategoryAxis;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.LineChartData;
import org.apache.poi.xddf.usermodel.chart.LineChartData.Series;
import org.apache.poi.xddf.usermodel.chart.LineChart;
import org.apache.poi.ss.usermodel.DataSources;
import org.apache.poi.ss.usermodel.ChartDataSource;
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');
Workbook workbook = new XSSFWorkbook(fileIn);
Sheet sheet = workbook.getSheet('P9');
// 创建折线图
XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 1, 10, 20);
XSSFChart chart = drawing.createChart(anchor);
XSSFChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.BOTTOM);
// 创建数据系列
XSSFChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
XSSFValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
// 创建数据源
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(11, 85, 0, 0));
ChartDataSource<Number> ys = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(11, 85, 2, 2));
// 添加数据系列
LineChartData data = chart.getChartDataFactory().createLineChartData();
Series series = data.addSeries(xs, ys);
series.setTitle('数据折线图');
// 绘制图表
chart.plot(data);
// 保存 Excel 文件
FileOutputStream fileOut = new FileOutputStream('巴新EDEVU水库数据处理表.xls');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行代码
运行代码后,Excel 文件 '巴新EDEVU水库数据处理表.xls' 将包含一个名为 '数据折线图' 的折线图,该图显示了工作表 'P9' 中单元格范围 'A11:A85' 和 'C11:C85' 的数据。
总结
本教程介绍了如何使用 Apache POI 库在 Excel 工作簿中创建折线图。它展示了如何添加数据系列、设置图表标题和轴,以及将图表保存到 Excel 文件中。希望本教程对您有所帮助!
原文地址: https://www.cveoy.top/t/topic/f3Rx 著作权归作者所有。请勿转载和采集!