欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實戰(zhàn)之利用POI生成Excel圖表

 更新時間:2025年02月27日 14:44:29   作者:小洋酸q糖  
Apache POI是Java生態(tài)中處理Office文檔的核心工具,這篇文章主要為大家詳細介紹了如何在Excel中創(chuàng)建折線圖,柱狀圖,餅圖等常見圖表,需要的可以參考下

Apache POI是Java生態(tài)中處理Office文檔的核心工具,支持Excel圖表的動態(tài)生成與數(shù)據(jù)綁定。本文以POI 5.x版本為例,詳解如何在Excel中創(chuàng)建折線圖、柱狀圖、餅圖等常見圖表,并提供代碼示例與最佳實踐。

一、環(huán)境配置與依賴管理

使用POI生成圖表需引入以下核心依賴(以Maven為例):

<dependency>  
    <groupId>org.apache.poi</groupId>  
    <artifactId>poi-ooxml</artifactId>  
    <version>5.2.3</version>  
</dependency>  

注意:POI 5.x版本與舊版本(如3.x)API差異較大,需避免依賴沖突

二、數(shù)據(jù)源準(zhǔn)備與工作表構(gòu)建

創(chuàng)建工作簿與工作表

XSSFWorkbook workbook = new XSSFWorkbook();  
XSSFSheet sheet = workbook.createSheet("數(shù)據(jù)表");  

填充數(shù)據(jù)

以國家GDP數(shù)據(jù)為例,首行寫入國家名稱,后續(xù)行填充數(shù)值:

// 創(chuàng)建標(biāo)題行  
Row headerRow = sheet.createRow(0);  
headerRow.createCell(0).setCellValue("國家");  
headerRow.createCell(1).setCellValue("俄羅斯");  
headerRow.createCell(2).setCellValue("中國");  
// 填充數(shù)據(jù)行  
Row dataRow = sheet.createRow(1);  
dataRow.createCell(0).setCellValue("GDP(萬億美元)");  
dataRow.createCell(1).setCellValue(1.78);  
dataRow.createCell(2).setCellValue(17.96);  

三、圖表生成核心步驟

1. 折線圖(Line Chart)

// 定義圖表位置與尺寸  
XSSFDrawing drawing = sheet.createDrawingPatriarch();  
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 20);  

// 創(chuàng)建圖表對象  
XSSFChart chart = drawing.createChart(anchor);  
chart.setTitleText("國家GDP趨勢分析");  

// 綁定數(shù)據(jù)源  
XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 1, 2));  
XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 1, 2));  

// 配置坐標(biāo)軸與樣式  
XDDFCategoryAxis xAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);  
XDDFValueAxis yAxis = chart.createValueAxis(AxisPosition.LEFT);  
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, xAxis, yAxis);  

// 添加數(shù)據(jù)系列并渲染  
XDDFLineChartData.Series series = data.addSeries(countries, values);  
series.setTitle("GDP", null);  
chart.plot(data);  

關(guān)鍵點:通過CellRangeAddress綁定數(shù)據(jù)區(qū)域,支持動態(tài)擴展

2. 柱狀圖(Bar Chart)

柱狀圖與折線圖代碼結(jié)構(gòu)類似,僅需修改圖表類型與樣式:

XDDFBarChartData data = (XDDFBarChartData) chart.createData(ChartTypes.BAR, xAxis, yAxis);  

可通過XDDFShapeProperties自定義柱體顏色與間距

3. 餅圖(Pie Chart)

餅圖需單獨設(shè)置數(shù)據(jù)標(biāo)簽與百分比顯示:

XDDFPieChartData data = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null);  
XDDFPieChartData.Series series = data.addSeries(countries, values);  
chart.plot(data);  
// 啟用百分比標(biāo)簽  
chart.getCTChart().getPlotArea().getPieChartArray(0).addNewDLbls().addNewShowPercent().setVal(true);  

四、常見問題與優(yōu)化

1.圖表位置偏移

通過XSSFClientAnchor參數(shù)調(diào)整坐標(biāo)(如col1, row1, col2, row2)控制圖表位置

2.數(shù)據(jù)動態(tài)更新

使用CellRangeAddress動態(tài)擴展數(shù)據(jù)范圍,避免硬編碼。例如:

CellRangeAddress valuesRange = new CellRangeAddress(1, 1, 1, sheet.getRow(1).getLastCellNum());  

3.樣式自定義

  • 坐標(biāo)軸標(biāo)題xAxis.setTitle("國家")
  • 圖例位置chart.getOrAddLegend().setPosition(LegendPosition.TOP_RIGHT)
  • 顏色設(shè)置:通過XDDFSolidFillProperties指定RGB值 

五、總結(jié)

通過POI生成Excel圖表的核心在于數(shù)據(jù)綁定API靈活調(diào)用。開發(fā)者需注意:

  • 數(shù)據(jù)區(qū)域需與圖表類型匹配(如分類軸與數(shù)值軸);
  • 高版本POI(≥5.0)推薦使用XDDF系列API,兼容性更強;
  • 復(fù)雜圖表建議封裝工具類(如ChartUtils)提升代碼復(fù)用性

到此這篇關(guān)于Java實戰(zhàn)之利用POI生成Excel圖表的文章就介紹到這了,更多相關(guān)Java POI生成Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論