Java 使用 Apache POI 从 Excel 文件生成折线图
您可以使用 Apache POI 库来读取 Excel 文件并生成折线图。下面是一个示例代码,从名为'input-1.xlsx' 的 Excel 文件中读取'P1'工作表的内容,并将 A1-A127 作为横坐标轴(时间格式),C1-C127 作为纵坐标轴,生成一个折线图,并保存到名为'output.xlsx' 的 Excel 文件中。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFChartLegend;
import org.apache.poi.xssf.usermodel.XDDFChart;
import org.apache.poi.xssf.usermodel.XDDFChartLegend;
import org.apache.poi.xssf.usermodel.XDDFDataSource;
import org.apache.poi.xssf.usermodel.XDDFNumericalDataSource;
import org.apache.poi.xssf.usermodel.XDDFLineChartData;
import org.apache.poi.xssf.usermodel.XDDFLineChartData.Series;
import org.apache.poi.xssf.usermodel.XDDFLineChartData.SeriesMarker;
import org.apache.poi.xssf.usermodel.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XDDFCategoryAxis;
import org.apache.poi.xssf.usermodel.XDDFChartAxis;
import org.apache.poi.xssf.usermodel.XDDFChartData;
import org.apache.poi.xssf.usermodel.XDDFChartDataFactory;
import org.apache.poi.xssf.usermodel.XDDFChartLegendEntry;
import org.apache.poi.xssf.usermodel.XDDFDataSourcesFactory;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
public class ExcelLineChartExample {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream('input-1.xlsx');
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheet('P1');
// 获取数据范围
int startRowNum = 0; // 开始行号
int endRowNum = 127; // 结束行号
int xAxisColumnIndex = 0; // X轴数据所在列的索引
int yAxisColumnIndex = 2; // Y轴数据所在列的索引
// 创建图表
XSSFSheet chartSheet = (XSSFSheet) workbook.createSheet('Chart');
XDDFChart chart = (XDDFChart) chartSheet.createDrawingPatriarch().createChart(chartSheet.createDrawingPatriarch().createAnchor(0, 0, 0, 0, 0, 5, 10, 15));
// 设置图表标题
chart.setTitleText('折线图');
chart.setTitleOverlay(false);
// 创建X轴和Y轴
XDDFCategoryAxis xAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis yAxis = chart.createValueAxis(AxisPosition.LEFT);
// 设置X轴和Y轴的格式
xAxis.setMajorTickMark(AxisTickMark.OUTSIDE);
xAxis.setMinorTickMark(AxisTickMark.NONE);
yAxis.setMajorTickMark(AxisTickMark.OUTSIDE);
yAxis.setMinorTickMark(AxisTickMark.NONE);
// 设置X轴的数据范围
String[] categories = new String[endRowNum - startRowNum + 1];
for (int i = startRowNum; i <= endRowNum; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(xAxisColumnIndex);
categories[i - startRowNum] = cell.getStringCellValue();
}
XDDFDataSource<String> xAxisData = XDDFDataSourcesFactory.fromArray(categories);
xAxis.setCategoryData(xAxisData);
// 设置Y轴的数据
XDDFNumericalDataSource<Double> yAxisData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(startRowNum, endRowNum, yAxisColumnIndex, yAxisColumnIndex));
yAxis.setCrosses(AxisCrosses.AUTO_ZERO);
yAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
// 创建折线图数据
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, xAxis, yAxis);
data.setVaryColors(true);
// 添加数据系列
Series series = (XDDFLineChartData.Series) data.addSeries(xAxisData, yAxisData);
series.setTitle('数据系列', null);
// 设置数据点标记
series.setMarker(SeriesMarker.SQUARE);
// 设置图例
XDDFChartLegend legend = chart.getOrAddLegend();
XDDFChartLegendEntry legendEntry = legend.addEntry(series);
legend.setPosition(LegendPosition.BOTTOM);
// 绘制图表
chart.plot(data);
// 保存文件
FileOutputStream output = new FileOutputStream('output.xlsx');
workbook.write(output);
output.close();
System.out.println('折线图生成成功!');
} catch (IOException e) {
e.printStackTrace();
}
}
}
请确保您已经在项目中引入了 Apache POI 库的依赖。这段代码将会从名为'input-1.xlsx' 的 Excel 文件中读取'P1'工作表的内容,并根据您的要求生成一个折线图。折线图将使用 A1-A127 作为横坐标轴(时间格式),C1-C127 作为纵坐标轴。生成的折线图将保存在名为'output.xlsx' 的 Excel 文件中。
原文地址: https://www.cveoy.top/t/topic/fSyY 著作权归作者所有。请勿转载和采集!