概述
透视表是依据已有数据源来创建的交互式表格,我们可在excel中创建透视表,也可编辑已有透视表。
所需工具:Free Spire.XLS for Java免费版,编辑代码前,先下载导入jar到Java程序(可手动下载导入,或通过Maven仓库下载导入)。
示例代码
1. 创建透视表
?
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测试文档
Workbook wb = new Workbook();
wb.loadFromFile( "test.xlsx" );
//获取第一个的工作表
Worksheet sheet = wb.getWorksheets().get( 0 );
//为需要汇总和分析的数据创建缓存
CellRange dataRange = sheet.getCellRange( "A1:D10" );
PivotCache cache = wb.getPivotCaches().add(dataRange);
//使用缓存创建数据透视表,并指定透视表的名称以及在工作表中的位置
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);
//设置行字段的标题
pt.getOptions().setRowHeaderCaption( "月份" );
//添加列字段
PivotField pf3 = null ;
if (pt.getPivotFields().get( "产品" ) instanceof PivotField){
pf3 = (PivotField) pt.getPivotFields().get( "产品" );
}
pf3.setAxis(AxisTypes.Column);
//设置列字段标题
pt.getOptions().setColumnHeaderCaption( "产品" );
//添加值字段
pt.getDataFields().add(pt.getPivotFields().get( "总产量" ), "求和项:总产量" ,SubtotalTypes.Sum);
//设置透视表样式
pt.setBuiltInStyle(PivotBuiltInStyles.PivotStyleDark12);
//保存文档
wb.saveToFile( "数据透视表.xlsx" , ExcelVersion.Version2013);
wb.dispose();
}
}
|
透视创建结果:
默认情况下,源数据的更改变动不会自动更新到透视表,需要在透视表上进行刷新。
?
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) {
//创建实例,加载Excel
Workbook wb = new Workbook();
wb.loadFromFile( "数据透视表.xlsx" );
//获取第一个工作表
Worksheet sheet = wb.getWorksheets().get( 0 );
//更改透视表的数据源数据
sheet.getCellRange( "C2:C4" ).setText( "产品A" );
sheet.getCellRange( "C5:C7" ).setText( "产品B" );
sheet.getCellRange( "C8:C10" ).setText( "产品C" );
//获取透视表,刷新数据
PivotTable pivotTable = (PivotTable) sheet.getPivotTables().get( 0 );
pivotTable.getCache().isRefreshOnLoad();
//保存文档
wb.saveToFile( "刷新透视表.xlsx" ,FileFormat.Version2013);
}
}
|
透视表更新前后效果:
3.折叠、展开透视表中的行
?
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( "数据透视表.xlsx" );
//获取数据透视表
XlsPivotTable pivotTable = (XlsPivotTable) wb.getWorksheets().get( 0 ).getPivotTables().get( 0 );
//计算数据
pivotTable.calculateData();
//展开”月份”字段下“2”的详细信息
PivotField field = (PivotField) pivotTable.getPivotFields().get( "月份" );
field.hideItemDetail( "2" , false );
//折叠”月份”字段下“3”的详细信息
PivotField field1 = (PivotField) pivotTable.getPivotFields().get( "月份" );
field1.hideItemDetail( "3" , true );
//保存并打开文档
wb.saveToFile( "展开、折叠行.xlsx" , ExcelVersion.Version2013);
wb.dispose();
}
}
|
折叠、展开效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.51cto.com/eiceblue/2518699
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 32
-
Spring Boot Hazelcast Caching 使用和配置详解
2025-05-29 69 -
2025-05-25 51
-
2025-06-04 58
-
2025-05-27 75
热门评论