使用Apache POI创建Excel折线图教程
使用Apache POI创建Excel折线图教程
本教程将指导您使用Apache POI库在Excel工作簿中创建折线图。我们将逐步介绍整个过程,包括读取Excel文件、创建图表对象、添加数据系列、设置坐标轴格式以及自定义图表外观。
1. 项目设置
- 确保您的项目中包含以下POI库依赖项: * poi * poi-ooxml * poi-ooxml-schemas
2. 代码示例javaimport org.apache.poi.ss.usermodel.Cell;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 ExcelChartExample02 {
public static void main(String[] args) { try { // 读取Excel文件 FileInputStream fileIn = new FileInputStream('巴新EDEVU水库数据处理表.xlsx'); XSSFWorkbook workbook = new XSSFWorkbook(fileIn); XSSFSheet sheet1 = workbook.getSheet('P9'); XSSFSheet sheet2 = workbook.getSheet('P10');
// 创建折线图 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);
// 设置横坐标轴 XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); bottomAxis.setTitle('时间');
// 设置纵坐标轴 XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); leftAxis.setTitle('数据值');
// 设置数据源 XDDFNumericalDataSource<Double> xs1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 0, 0)); XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 2, 2)); XDDFNumericalDataSource<Double> xs2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 0, 0)); XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet2, new CellRangeAddress(11, 139, 2, 2));
// 添加数据系列 XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis); XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs1, ys1); series1.setTitle('P9数据折线图', null); XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs2, ys2); series2.setTitle('P10数据折线图', null);
// 设置横坐标轴为日期坐标轴 XDDFDataSource<?> dateSource = XDDFDataSourcesFactory.fromNumericCellRange(sheet1, new CellRangeAddress(11, 85, 1, 1)); XDDFDateAxis bottomDateAxis = chart.createDateAxis(AxisPosition.BOTTOM); bottomDateAxis.setMajorTickMark(AxisTickMark.CROSS); bottomDateAxis.setMinorTickMark(AxisTickMark.NONE); bottomDateAxis.setCrosses(AxisCrosses.AUTO_ZERO); bottomDateAxis.setTitle('时间'); bottomDateAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO);
// 设置时间格式 SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy-MM-dd'); for (int i = 0; i < dateSource.getPointCount(); i++) { Cell cell = sheet1.getRow(i + 11).getCell(1); Date date = cell.getDateCellValue(); if (date != null) { String formattedDate = dateFormat.format(date); bottomDateAxis.setTickLabelPosition(AxisTickLabelPosition.NEXT_TO); } }
// 绘制图表 chart.plot(data);
// 创建'温度'工作表 XSSFSheet 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); XDDFCategoryAxis bottomAxis2 = chart2.createCategoryAxis(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('巴新EDEVU水库数据处理表.xlsx'); workbook.write(fileOut); fileOut.close();
System.out.println('折线图已创建并保存到Excel文件中。');
} catch (IOException e) { e.printStackTrace(); }
原文地址: https://www.cveoy.top/t/topic/fzHd 著作权归作者所有。请勿转载和采集!