Java Apache POI 创建 Excel 折线图详解
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExcelChartExample03 {
public static void main(String[] args) {
try {
// 读取Excel文件
FileInputStream fileIn = new FileInputStream('input.xlsx');
XSSFWorkbook workbook = new XSSFWorkbook(fileIn);
XSSFSheet sheet1 = workbook.getSheet('P1');
XSSFSheet sheet2 = workbook.getSheet('P1-1');
// 创建折线图
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);
// 设置横坐标轴为日期坐标轴
XDDFDataSource<?> dateSource = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 1, 1));
XDDFDateAxis bottomAxis = chart.createDateAxis(AxisPosition.BOTTOM);
bottomAxis.setMajorTickMark(AxisTickMark.CROSS);
bottomAxis.setMinorTickMark(AxisTickMark.NONE);
bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
bottomAxis.setTitle('时间');
bottomAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO);
// 设置时间格式
SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy-MM-dd');
for (int i = 0; i < dateSource.getPointCount(); i++) {
XSSFRow row = sheet1.getRow(i + 11);
if (row != null) {
Cell cell = row.getCell(1);
if (cell != null && cell.getCellType() == CellType.NUMERIC) {
Date date = cell.getDateCellValue();
String formattedDate = dateFormat.format(date);
bottomAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO);
}
}
}
// 设置纵坐标轴
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setTitle('数据值');
// 设置数据源
XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 73, 0, 0));
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 73, 1, 1));
XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(1, 73, 0, 0));
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(1, 73, 1, 1));
// 添加数据系列
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1);
series1.setTitle('P1数据折线图', null);
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs2, ys2);
series2.setTitle('P1-1数据折线图', null);
// 绘制图表
chart.plot(data);
// 创建'温度'工作表
XSSFSheet sheet3 = null;
if (workbook.getSheet('温度') != null) {
sheet3 = workbook.getSheet('温度');
} else {
sheet3 = workbook.createSheet('温度');
}
// 将图表复制到'温度'工作表
XSSFDrawing drawing2 = sheet3.createDrawingPatriarch();
XSSFClientAnchor anchor2 = drawing2.createAnchor(0, 0, 0, 0, 0, 0, 10, 20);
XSSFChart chart2 = drawing2.createChart(anchor2);
chart2.setTitleText('数据折线图');
chart2.setTitleOverlay(false);
XDDFDateAxis bottomAxis2 = chart2.createDateAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis2 = chart2.createValueAxis(AxisPosition.LEFT);
leftAxis2.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFLineChartData data2 = (XDDFLineChartData) chart2.createData(ChartTypes.LINE, bottomAxis2, leftAxis2);
data2.addSeries(xs1, ys1);
data2.addSeries(xs2, ys2);
chart2.plot(data2);
// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream('input.xlsx');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
} catch (IOException e) {
e.printStackTrace();
}
}
}
这段代码演示了如何使用 Apache POI 库在 Java 中创建 Excel 折线图。
主要步骤:
- 读取 Excel 文件: 使用
FileInputStream和XSSFWorkbook读取现有的 Excel 文件。 - 创建图表对象: 使用
XSSFDrawing和XSSFChart创建图表对象,并设置图表标题和位置。 - 设置坐标轴: 使用
XDDFDateAxis和XDDFValueAxis设置横坐标轴和纵坐标轴,包括坐标轴类型、标题、刻度等。 - 设置数据源: 使用
XDDFDataSourcesFactory从 Excel 表格中读取数据,并创建数据源对象。 - 添加数据系列: 使用
XDDFLineChartData创建折线图数据对象,并使用addSeries方法添加数据系列,设置数据系列的名称和样式。 - 绘制图表: 使用
chart.plot(data)方法绘制图表。 - 保存 Excel 文件: 使用
FileOutputStream和workbook.write()方法保存修改后的 Excel 文件。
代码中还包含以下细节处理:
- 设置时间格式: 使用
SimpleDateFormat类将日期格式化为指定的格式。 - 处理异常: 使用
try-catch块捕获可能出现的异常,例如文件读取异常和写入异常。 - 避免重复创建工作表:在创建“温度”工作表之前,先判断工作簿中是否已经存在同名工作表,避免报错。
希望这篇博客文章能够帮助你理解如何使用 Java Apache POI 库创建 Excel 折线图。
原文地址: https://www.cveoy.top/t/topic/fAkQ 著作权归作者所有。请勿转载和采集!