使用Apache POI创建Excel折线图 - Java示例代码
使用Apache POI创建Excel折线图
本教程演示如何使用Apache POI库在Java应用程序中创建Excel折线图。
1. 添加Maven依赖
在您的pom.xml文件中添加以下Apache POI依赖项:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
2. Java代码
以下代码展示了如何创建一个包含两个数据系列、自定义轴和图例的简单折线图:
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 ExcelChartExample {
public static void main(String[] args) {
try {
// 读取Excel文件
FileInputStream fileIn = new FileInputStream('input.xlsx');
XSSFWorkbook workbook = new XSSFWorkbook(fileIn);
XSSFSheet sheet = workbook.getSheet('Sheet1');
// 创建折线图
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 12, 10, 22);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText('数据折线图');
chart.setTitleOverlay(false);
// 设置图例位置
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
// 设置横坐标轴为日期坐标轴
XDDFDataSource<?> dateSource = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 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 = sheet.getRow(i + 1);
if (row != null) {
Cell cell = row.getCell(1);
if (cell != null && cell.getCellType() == CellType.NUMERIC) {
Date date = cell.getDateCellValue();
String formattedDate = dateFormat.format(date);
// 设置日期标签
}
}
}
// 设置左侧坐标轴
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setTitle('温度');
// 设置右侧坐标轴
XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setCrosses(AxisCrosses.MAX);
rightAxis.setTitle('数据值');
// 设置数据源
XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 127, 0, 0));
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 127, 1, 1));
XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 127, 0, 0));
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 127, 2, 2));
// 添加第一个数据系列
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1);
series1.setTitle('折线图1', null);
// 添加第二个数据系列
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs2, ys2);
series2.setTitle('折线图2', null);
series2.setSmooth(false);
// 将第二个数据系列关联到右侧坐标轴
chart.plot(data);
// 使用替代方法设置坐标轴关联
// ...
// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream('output.xlsx');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意:
- 将
input.xlsx替换为您的输入Excel文件路径,并将output.xlsx替换为所需的输出文件路径。 - 根据您的Apache POI版本,您可能需要调整代码中与坐标轴关联相关的部分。参考官方文档或示例代码以获取特定版本的信息。
通过以上步骤,您就可以使用Apache POI在Java中创建Excel折线图了。
原文地址: https://www.cveoy.top/t/topic/fRdA 著作权归作者所有。请勿转载和采集!