概述
透視表是依據(jù)已有數(shù)據(jù)源來(lái)創(chuàng)建的交互式表格,我們可在excel中創(chuàng)建透視表,也可編輯已有透視表。
所需工具:Free Spire.XLS for Java免費(fèi)版,編輯代碼前,先下載導(dǎo)入jar到Java程序(可手動(dòng)下載導(dǎo)入,或通過(guò)Maven倉(cāng)庫(kù)下載導(dǎo)入)。
示例代碼
1. 創(chuàng)建透視表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import com.spire.xls.*; public class CreatePivotTable { public static void main(String[] args) { //加載Excel測(cè)試文檔 Workbook wb = new Workbook(); wb.loadFromFile( "test.xlsx" ); //獲取第一個(gè)的工作表 Worksheet sheet = wb.getWorksheets().get( 0 ); //為需要匯總和分析的數(shù)據(jù)創(chuàng)建緩存 CellRange dataRange = sheet.getCellRange( "A1:D10" ); PivotCache cache = wb.getPivotCaches().add(dataRange); //使用緩存創(chuàng)建數(shù)據(jù)透視表,并指定透視表的名稱以及在工作表中的位置 PivotTable pt = sheet.getPivotTables().add( "PivotTable" ,sheet.getCellRange( "A12" ),cache); //添加行字段1 PivotField pf1 = null ; if (pt.getPivotFields().get( "月份" ) instanceof PivotField){ pf1 = (PivotField) pt.getPivotFields().get( "月份" ); } pf1.setAxis(AxisTypes.Row); //添加行字段2 PivotField pf2 = null ; if (pt.getPivotFields().get( "廠商" ) instanceof PivotField){ pf2 = (PivotField) pt.getPivotFields().get( "廠商" ); } pf2.setAxis(AxisTypes.Row); //設(shè)置行字段的標(biāo)題 pt.getOptions().setRowHeaderCaption( "月份" ); //添加列字段 PivotField pf3 = null ; if (pt.getPivotFields().get( "產(chǎn)品" ) instanceof PivotField){ pf3 = (PivotField) pt.getPivotFields().get( "產(chǎn)品" ); } pf3.setAxis(AxisTypes.Column); //設(shè)置列字段標(biāo)題 pt.getOptions().setColumnHeaderCaption( "產(chǎn)品" ); //添加值字段 pt.getDataFields().add(pt.getPivotFields().get( "總產(chǎn)量" ), "求和項(xiàng):總產(chǎn)量" ,SubtotalTypes.Sum); //設(shè)置透視表樣式 pt.setBuiltInStyle(PivotBuiltInStyles.PivotStyleDark12); //保存文檔 wb.saveToFile( "數(shù)據(jù)透視表.xlsx" , ExcelVersion.Version2013); wb.dispose(); } } |
透視創(chuàng)建結(jié)果:
2.刷新Excel透視表
默認(rèn)情況下,源數(shù)據(jù)的更改變動(dòng)不會(huì)自動(dòng)更新到透視表,需要在透視表上進(jìn)行刷新。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import com.spire.xls.*; public class RefreshPivotTable { public static void main(String[] args) { //創(chuàng)建實(shí)例,加載Excel Workbook wb = new Workbook(); wb.loadFromFile( "數(shù)據(jù)透視表.xlsx" ); //獲取第一個(gè)工作表 Worksheet sheet = wb.getWorksheets().get( 0 ); //更改透視表的數(shù)據(jù)源數(shù)據(jù) sheet.getCellRange( "C2:C4" ).setText( "產(chǎn)品A" ); sheet.getCellRange( "C5:C7" ).setText( "產(chǎn)品B" ); sheet.getCellRange( "C8:C10" ).setText( "產(chǎn)品C" ); //獲取透視表,刷新數(shù)據(jù) PivotTable pivotTable = (PivotTable) sheet.getPivotTables().get( 0 ); pivotTable.getCache().isRefreshOnLoad(); //保存文檔 wb.saveToFile( "刷新透視表.xlsx" ,FileFormat.Version2013); } } |
透視表更新前后效果:
3.折疊、展開(kāi)透視表中的行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import com.spire.xls.*; import com.spire.xls.core.spreadsheet.pivottables.XlsPivotTable; public class ExpandRows { public static void main(String[] args) { //加載包含透視表的Excel Workbook wb = new Workbook(); wb.loadFromFile( "數(shù)據(jù)透視表.xlsx" ); //獲取數(shù)據(jù)透視表 XlsPivotTable pivotTable = (XlsPivotTable) wb.getWorksheets().get( 0 ).getPivotTables().get( 0 ); //計(jì)算數(shù)據(jù) pivotTable.calculateData(); //展開(kāi)”月份”字段下“2”的詳細(xì)信息 PivotField field = (PivotField) pivotTable.getPivotFields().get( "月份" ); field.hideItemDetail( "2" , false ); //折疊”月份”字段下“3”的詳細(xì)信息 PivotField field1 = (PivotField) pivotTable.getPivotFields().get( "月份" ); field1.hideItemDetail( "3" , true ); //保存并打開(kāi)文檔 wb.saveToFile( "展開(kāi)、折疊行.xlsx" , ExcelVersion.Version2013); wb.dispose(); } } |
折疊、展開(kāi)效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.51cto.com/eiceblue/2518699