Excel 折线图教程:使用 Apache POI 创建动态图表
使用 Apache POI 在 Excel 中创建动态折线图
本教程将带您一步步学习如何使用 Apache POI 库在 Excel 中创建动态折线图。我们将使用 Java 代码来读取数据、设置坐标轴和添加数据系列,并最终将图表保存到 Excel 文件中。
1. 添加依赖
首先,您需要在项目中添加 Apache POI 库的依赖。您可以在 Maven 或 Gradle 项目中添加以下依赖项:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
2. 代码示例
以下代码示例展示了如何创建一个简单的 Excel 折线图:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelChartExample {
public static void main(String[] args) {
try {
// 创建Excel工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet('折线图');
// 创建数据
Object[][] data = {
{'日期', '数值1', '数值2'},
{1, 10, 20},
{2, 15, 25},
{3, 20, 30},
{4, 25, 35},
{5, 30, 40},
};
// 写入数据到单元格
for (int rowNum = 0; rowNum < data.length; rowNum++) {
XSSFRow row = sheet.createRow(rowNum);
for (int colNum = 0; colNum < data[rowNum].length; colNum++) {
XSSFCell cell = row.createCell(colNum);
if (data[rowNum][colNum] instanceof String) {
cell.setCellValue((String) data[rowNum][colNum]);
} else if (data[rowNum][colNum] instanceof Integer) {
cell.setCellValue((Integer) data[rowNum][colNum]);
}
}
}
// 创建折线图
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 1, 15, 15);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText('折线图');
chart.setTitleOverlay(false);
// 设置图例位置
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
// 设置横坐标轴
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle('日期');
// 设置纵坐标轴
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle('数值');
// 设置数据源
XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, data.length, 0, 0));
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, data.length, 1, 1));
XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, data.length, 0, 0));
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, data.length, 2, 2));
// 添加数据系列
XDDFLineChartData data1 = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data1.addSeries(xs1, ys1);
series1.setTitle('数值1', null);
// 添加第二个数据系列
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data1.addSeries(xs2, ys2);
series2.setTitle('数值2', null);
// 将数据系列关联到图表
chart.plot(data1);
// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream('折线图.xlsx');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 代码解释
-
创建 Excel 工作簿和工作表:
XSSFWorkbook workbook = new XSSFWorkbook();:创建一个新的 Excel 工作簿。XSSFSheet sheet = workbook.createSheet('折线图');:创建一个名为 '折线图' 的工作表。
-
创建数据:
Object[][] data = { ... };:定义一个二维数组,用于存储图表的数据。
-
写入数据到单元格:
- 使用两个循环遍历数据数组,将数据写入到对应单元格。
-
创建折线图:
XSSFDrawing drawing = sheet.createDrawingPatriarch();:获取工作表的绘图对象。XSSFClientAnchor anchor = drawing.createAnchor(...);:定义图表在工作表中的位置和大小。XSSFChart chart = drawing.createChart(anchor);:创建折线图对象。chart.setTitleText('折线图');:设置图表标题。
-
设置图例位置:
XDDFChartLegend legend = chart.getOrAddLegend();:获取或创建一个图例对象。legend.setPosition(LegendPosition.BOTTOM);:将图例位置设置为底部。
-
设置坐标轴:
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);:创建横坐标轴对象。bottomAxis.setTitle('日期');:设置横坐标轴标题。XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);:创建纵坐标轴对象。leftAxis.setTitle('数值');:设置纵坐标轴标题。
-
设置数据源:
XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(...);:设置数据系列 1 的横坐标数据源。XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(...);:设置数据系列 1 的纵坐标数据源。- 同样方式设置数据系列 2 的数据源。
-
添加数据系列:
XDDFLineChartData data1 = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);:创建折线图数据对象。XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data1.addSeries(xs1, ys1);:添加第一个数据系列。series1.setTitle('数值1', null);:设置第一个数据系列的标题。- 同样方式添加第二个数据系列。
-
将数据系列关联到图表:
chart.plot(data1);:将数据系列添加到图表中。
-
保存 Excel 文件:
FileOutputStream fileOut = new FileOutputStream('折线图.xlsx');:创建文件输出流。workbook.write(fileOut);:将工作簿写入到文件中。fileOut.close();:关闭文件输出流。
4. 总结
通过以上步骤,您就可以使用 Apache POI 库在 Excel 中创建动态折线图。您可以根据需要修改数据和图表的样式,以满足您的需求。
希望本教程对您有所帮助!如果您有任何问题,请随时在评论区留言。
原文地址: http://www.cveoy.top/t/topic/fSlN 著作权归作者所有。请勿转载和采集!