使用Apache POI创建Excel折线图
使用Apache POI创建Excel折线图
本文介绍如何使用Apache POI库在Java中创建Excel折线图。
代码示例:
package 折线图;
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 ExcelChartExample04 {
public static void main(String[] args) {
try {
// 读取Excel文件
FileInputStream fileIn = new FileInputStream('input-1.xlsx');
XSSFWorkbook workbook = new XSSFWorkbook(fileIn);
XSSFSheet sheet1 = workbook.getSheet('P1');
// 创建折线图
XSSFDrawing drawing = sheet1.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 129, 10, 149);
XDDFChart chart = drawing.createChart(anchor);
chart.setTitleText('数据折线图');
chart.setTitleOverlay(false);
// 设置图例位置
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
// 设置横坐标轴为日期坐标轴
XDDFDataSource<?> dateSource = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(0, 0, 0, 126));
XDDFDateAxis bottomAxis = chart.createDateAxis(AxisPosition.BOTTOM);
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 + 1);
if (row != null) {
Cell cell = row.getCell(0);
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('温度');
// 设置右侧坐标轴为数值坐标轴
XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setCrosses(AxisCrosses.MAX);
rightAxis.setTitle('数据值');
// 设置数据源
XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 127, 0, 0));
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 127, 1, 1));
XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(1, 127, 0, 0));
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, 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);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).getIdx().setVal(1);
// 保存Excel文件
FileOutputStream fileOut = new FileOutputStream('input-1.xlsx');
workbook.write(fileOut);
fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决运行后出现空白窗格的问题:
运行完程序后出现折线图位置只有一个空白窗格的原因可能是数据源或者图表设置有误。以下是可能的解决办法:
-
检查数据源: 确保数据源的范围和位置正确。查看
XDDFDataSourcesFactory.fromNumericCellRange()方法的参数是否正确,包括sheet名称、行列范围等。 -
检查图表设置: 确保图表的坐标轴、数据系列等设置正确。查看
createDateAxis()、createValueAxis()和createData()等方法的参数是否正确,包括坐标轴位置、标题等。 -
检查保存文件: 确保保存文件的路径和名称正确。查看
FileOutputStream的参数是否正确。
如果以上解决办法仍然无法解决问题,建议检查程序是否存在其他错误或异常,并尝试使用调试工具进行调试,以找出问题所在。
原文地址: https://www.cveoy.top/t/topic/fSwm 著作权归作者所有。请勿转载和采集!