使用Java在Excel中創(chuàng)建數(shù)據(jù)透視表
引言
在處理銷售數(shù)據(jù)、財(cái)務(wù)報(bào)表或運(yùn)營(yíng)指標(biāo)時(shí),數(shù)據(jù)透視表是快速匯總和分析大量數(shù)據(jù)的強(qiáng)大工具。它能夠自動(dòng)對(duì)數(shù)據(jù)進(jìn)行分類、聚合和重新排列,使復(fù)雜的數(shù)據(jù)集變得易于理解。與其手動(dòng)創(chuàng)建匯總表或使用 Excel 交互式界面逐一操作,通過(guò) Java 自動(dòng)化這個(gè)過(guò)程可以實(shí)現(xiàn)定期報(bào)告的自動(dòng)生成,大幅提高工作效率。
本文將詳細(xì)介紹如何使用 Java 在 Excel 工作簿中創(chuàng)建數(shù)據(jù)透視表,通過(guò)編程方式構(gòu)建靈活的數(shù)據(jù)分析工具,自動(dòng)化將原始數(shù)據(jù)轉(zhuǎn)換為結(jié)構(gòu)化的分析報(bào)告。
環(huán)境準(zhǔn)備
首先需要在 Maven 項(xiàng)目中添加 Free Spire.XLS for Java 依賴。將以下配置添加到 pom.xml 文件中:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>16.3.1</version>
</dependency>配置完成后,Maven 會(huì)自動(dòng)下載必要的庫(kù)文件。也可以手動(dòng)下載 JAR 包并添加到項(xiàng)目的 classpath 中。
準(zhǔn)備源數(shù)據(jù)
創(chuàng)建數(shù)據(jù)透視表的第一步是準(zhǔn)備源數(shù)據(jù)。通常這些數(shù)據(jù)包含多個(gè)維度(如產(chǎn)品、時(shí)間)和度量值(如銷售數(shù)量、收入)。以下代碼展示如何在 Excel 中創(chuàng)建基礎(chǔ)數(shù)據(jù)表:
// 創(chuàng)建新的工作簿
Workbook workbook = new Workbook();
// 獲取第一個(gè)工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// 設(shè)置表頭
sheet.getCellRange("A1").setValue("Product"); // 產(chǎn)品列
sheet.getCellRange("B1").setValue("Month"); // 月份列
sheet.getCellRange("C1").setValue("Count"); // 銷售數(shù)量列
// 填充產(chǎn)品數(shù)據(jù)
sheet.getCellRange("A2").setValue("Word");
sheet.getCellRange("A3").setValue("Word");
sheet.getCellRange("A4").setValue("Excel");
sheet.getCellRange("A5").setValue("Word");
sheet.getCellRange("A6").setValue("Excel");
sheet.getCellRange("A7").setValue("Excel");
// 填充月份數(shù)據(jù)
sheet.getCellRange("B2").setValue("January");
sheet.getCellRange("B3").setValue("February");
sheet.getCellRange("B4").setValue("January");
sheet.getCellRange("B5").setValue("January");
sheet.getCellRange("B6").setValue("February");
sheet.getCellRange("B7").setValue("February");
// 填充銷售數(shù)量數(shù)據(jù)
sheet.getCellRange("C2").setValue("10");
sheet.getCellRange("C3").setValue("15");
sheet.getCellRange("C4").setValue("9");
sheet.getCellRange("C5").setValue("7");
sheet.getCellRange("C6").setValue("8");
sheet.getCellRange("C7").setValue("10");這段代碼創(chuàng)建了一個(gè)包含三列數(shù)據(jù)的表(A1:C7),其中每行代表一條銷售記錄。在實(shí)際應(yīng)用中,這些數(shù)據(jù)可以從數(shù)據(jù)庫(kù)、API 或其他數(shù)據(jù)源動(dòng)態(tài)加載。
創(chuàng)建數(shù)據(jù)透視表
有了源數(shù)據(jù)后,就可以創(chuàng)建數(shù)據(jù)透視表。這個(gè)過(guò)程包括定義數(shù)據(jù)緩存、創(chuàng)建透視表對(duì)象,以及配置各個(gè)字段的顯示方式:
// 定義數(shù)據(jù)范圍(包含表頭)
CellRange dataRange = sheet.getCellRange("A1:C7");
// 創(chuàng)建數(shù)據(jù)緩存
PivotCache cache = workbook.getPivotCaches().add(dataRange);
// 在工作表的 E10 單元格處創(chuàng)建數(shù)據(jù)透視表,名稱為 "Pivot Table"
PivotTable pt = sheet.getPivotTables().add("Pivot Table", sheet.getCellRange("E10"), cache);
// 將 Product 字段添加到行區(qū)域
PivotField pf = (PivotField) pt.getPivotFields().get("Product");
pf.setAxis(AxisTypes.Row);
// 將 Month 字段也添加到行區(qū)域
PivotField pf2 = (PivotField) pt.getPivotFields().get("Month");
pf2.setAxis(AxisTypes.Row);
// 將 Count 字段添加到數(shù)據(jù)區(qū)域,并設(shè)置聚合方式為求和
pt.getDataFields().add(pt.getPivotFields().get("Count"), "SUM of Count", SubtotalTypes.Sum);
// 應(yīng)用內(nèi)置樣式,使表格更加美觀
pt.setBuiltInStyle(PivotBuiltInStyles.PivotStyleMedium12);
// 計(jì)算數(shù)據(jù)透視表的數(shù)據(jù)
pt.calculateData();
// 自動(dòng)調(diào)整列寬以顯示所有內(nèi)容
sheet.autoFitColumn(5);
sheet.autoFitColumn(6);關(guān)鍵概念解析:
- PivotCache(數(shù)據(jù)緩存):是數(shù)據(jù)透視表的基礎(chǔ),存儲(chǔ)源數(shù)據(jù)的快照。一個(gè)緩存可以被多個(gè)透視表共享
- AxisTypes.Row:將字段設(shè)置為行軸,該字段會(huì)以行的形式顯示在透視表的左側(cè)
- SubtotalTypes.Sum:指定數(shù)值字段的聚合方式。其他選項(xiàng)還包括
Count(計(jì)數(shù))、Average(平均值)等 - PivotBuiltInStyles:內(nèi)置樣式庫(kù)提供了多種預(yù)定義的表格外觀,快速美化透視表
完整代碼示例
以下是一個(gè)完整的 Java 類,創(chuàng)建一個(gè)數(shù)據(jù)透視表并設(shè)置樣式:
import com.spire.xls.*;
public class CreatePivotTableExample {
public static void main(String[] args) {
// 創(chuàng)建新的工作簿
Workbook workbook = new Workbook();
// 獲取第一個(gè)工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// 設(shè)置表頭
sheet.getCellRange("A1").setValue("Product");
sheet.getCellRange("B1").setValue("Month");
sheet.getCellRange("C1").setValue("Count");
// 填充產(chǎn)品數(shù)據(jù)
sheet.getCellRange("A2").setValue("Word");
sheet.getCellRange("A3").setValue("Word");
sheet.getCellRange("A4").setValue("Excel");
sheet.getCellRange("A5").setValue("Word");
sheet.getCellRange("A6").setValue("Excel");
sheet.getCellRange("A7").setValue("Excel");
// 填充月份數(shù)據(jù)
sheet.getCellRange("B2").setValue("January");
sheet.getCellRange("B3").setValue("February");
sheet.getCellRange("B4").setValue("January");
sheet.getCellRange("B5").setValue("January");
sheet.getCellRange("B6").setValue("February");
sheet.getCellRange("B7").setValue("February");
// 填充銷售數(shù)量數(shù)據(jù)
sheet.getCellRange("C2").setValue("10");
sheet.getCellRange("C3").setValue("15");
sheet.getCellRange("C4").setValue("9");
sheet.getCellRange("C5").setValue("7");
sheet.getCellRange("C6").setValue("8");
sheet.getCellRange("C7").setValue("10");
// 定義數(shù)據(jù)范圍
CellRange dataRange = sheet.getCellRange("A1:C7");
// 創(chuàng)建數(shù)據(jù)緩存
PivotCache cache = workbook.getPivotCaches().add(dataRange);
// 創(chuàng)建數(shù)據(jù)透視表
PivotTable pt = sheet.getPivotTables().add("Pivot Table", sheet.getCellRange("E10"), cache);
// 設(shè)置行字段
PivotField pf = (PivotField) pt.getPivotFields().get("Product");
pf.setAxis(AxisTypes.Row);
PivotField pf2 = (PivotField) pt.getPivotFields().get("Month");
pf2.setAxis(AxisTypes.Row);
// 設(shè)置數(shù)據(jù)字段(求和)
pt.getDataFields().add(pt.getPivotFields().get("Count"), "SUM of Count", SubtotalTypes.Sum);
// 應(yīng)用樣式
pt.setBuiltInStyle(PivotBuiltInStyles.PivotStyleMedium12);
// 計(jì)算透視表數(shù)據(jù)
pt.calculateData();
// 自動(dòng)調(diào)整列寬
sheet.autoFitColumn(5);
sheet.autoFitColumn(6);
// 保存文件為 Excel 格式
workbook.saveToFile("create_pivot_table_demo.xlsx", ExcelVersion.Version2013);
// 釋放資源
workbook.dispose();
System.out.println("數(shù)據(jù)透視表已成功創(chuàng)建!");
}
}生成結(jié)果預(yù)覽:

實(shí)用技巧
- 自定義透視表位置:通過(guò)修改
getCellRange()中的單元格地址(如 "E10"),可以將透視表放置在工作表的任意位置 - 多字段聚合:可以添加多個(gè)數(shù)據(jù)字段,例如同時(shí)顯示銷售數(shù)量和銷售額的求和結(jié)果
- 不同的聚合函數(shù):除了
Sum外,還支持Average、Count、Min、Max等多種聚合方式 - 列字段配置:使用
setAxis(AxisTypes.Column)可以將字段設(shè)置為列軸,創(chuàng)建復(fù)雜的透視表布局
總結(jié)
通過(guò) Java 編程創(chuàng)建 Excel 數(shù)據(jù)透視表,使我們能夠自動(dòng)化數(shù)據(jù)分析工作流。這種方法特別適合需要定期生成報(bào)告或處理大量數(shù)據(jù)集的場(chǎng)景。Spire.XLS 庫(kù)提供的 API 直觀易用,使開(kāi)發(fā)者可以專注于業(yè)務(wù)邏輯,而不必深入 Excel 的復(fù)雜內(nèi)部結(jié)構(gòu)。
您可以基于本文提供的代碼框架進(jìn)行擴(kuò)展,實(shí)現(xiàn)更復(fù)雜的數(shù)據(jù)分析需求,例如添加數(shù)據(jù)透視圖、實(shí)現(xiàn)多表聯(lián)合分析,或集成到自動(dòng)化報(bào)告系統(tǒng)中。
以上就是使用Java在Excel中創(chuàng)建數(shù)據(jù)透視表的詳細(xì)內(nèi)容,更多關(guān)于Java Excel創(chuàng)建數(shù)據(jù)透視表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring boot中多線程開(kāi)發(fā)的注意事項(xiàng)總結(jié)
spring boot 通過(guò)任務(wù)執(zhí)行器 taskexecutor 來(lái)實(shí)現(xiàn)多線程和并發(fā)編程。下面這篇文章主要給大家介紹了關(guān)于spring boot中多線程開(kāi)發(fā)的注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09
關(guān)于Idea使用git時(shí)commit特別慢的問(wèn)題及解決方法
這篇文章主要介紹了關(guān)于Idea使用git時(shí)commit特別慢的問(wèn)題及解決方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
關(guān)于SSM框架下各層的解釋說(shuō)明(Controller等)
這篇文章主要介紹了關(guān)于SSM框架下各層的解釋說(shuō)明(Controller等),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java編程實(shí)現(xiàn)驗(yàn)證哥德巴赫猜想
這篇文章主要介紹了Java編程實(shí)現(xiàn)驗(yàn)證哥德巴赫猜想,具有一定參考價(jià)值,需要的朋友可以了解下。2017-12-12
Spring Boot 404錯(cuò)誤全面解析與解決方案
在Spring Boot應(yīng)用開(kāi)發(fā)過(guò)程中,HTTP狀態(tài)碼404(Not Found)是最為常見(jiàn)的運(yùn)行時(shí)問(wèn)題之一,該錯(cuò)誤表明客戶端請(qǐng)求的資源無(wú)法在服務(wù)器端找到,下面就來(lái)詳細(xì)的介紹一下該問(wèn)題的解決,感興趣的可以了解一下2025-12-12
關(guān)于為何說(shuō)JAVA中要慎重使用繼承詳解
Java繼承是面向?qū)ο蟮淖铒@著的一個(gè)特征,然而下面這篇文章主要給大家介紹了關(guān)于為何說(shuō)JAVA中要慎重使用繼承的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Java 實(shí)現(xiàn)萬(wàn)年歷總結(jié)
這篇文章主要介紹了Java 萬(wàn)年歷實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09

