数据透视图是 Excel 中强大的数据分析工具,可以帮助用户迅速总结、分析和展示大量数据。通过数据透视图,用户可以灵活地组织和汇总数据,识别趋势和模式,做出更有效的决策。本文将介绍如何使用 Spire.XLS for Java 和 Java 在 Excel 中创建数据透视图。
安装 Spire.XLS for Java
首先,您需要在 Java 程序中添加 Spire.XLS for Java 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>15.1.3</version>
</dependency>
</dependencies>
Java 在 Excel 中创建数据透视图
Spire.XLS for Java 提供了 Worksheet.getCharts().add(ExcelChartType pivotChartType, IPivotTable pivotTable) 方法,支持基于 Excel 中的特定数据透视表创建数据透视图。详细步骤如下:
- 创建 Workbook 类的对象。
- 使用 Workbook.loadFromFile() 方法加载 Excel 文件。
- 使用 Workbook.getWorksheets.get(index) 方法获取 Excel 文件中的特定工作表。
- 使用 Worksheet.getPivotTables().get(index) 方法获取工作表中的特定数据透视表。
- 使用 Worksheet.getCharts().add() 方法向工作表添加数据透视图。
- 设置数据透视图的位置和标题。
- 使用 Workbook.saveToFile() 方法保存生成的文件。
- C#
import com.spire.xls.*;
import com.spire.xls.core.IPivotTable;
public class CreatePivotChart {
public static void main(String[] args) {
// 创建 Workbook 对象
Workbook workbook = new Workbook();
// 加载 Excel 文件
workbook.loadFromFile("示例.xlsx");
// 获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// 获取工作表中的第一个数据透视表
IPivotTable pivotTable = sheet.getPivotTables().get(0);
// 基于数据透视表创建数据透视图
Chart pivotChart = sheet.getCharts().add(ExcelChartType.ColumnClustered, pivotTable);
// 设置图表位置
pivotChart.setTopRow(1);
pivotChart.setLeftColumn(11);
pivotChart.setRightColumn(20);
pivotChart.setBottomRow(15);
// 将图表标题设置为 null
pivotChart.setChartTitle("");
// 保存生成的文件
workbook.saveToFile("创建数据透视图.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}
Java 显示或隐藏 Excel 数据透视图的字段按钮
你可以使用 Spire.XLS for Java 在数据透视图中显示或隐藏以下字段按钮:
- 所有字段按钮
- 报告筛选字段按钮
- 图例字段按钮
- 轴字段按钮
- 数值字段按钮
详细步骤如下:
- 创建 Workbook 类的对象。
- 使用 Workbook.loadFromFile() 方法加载 Excel 文件。
- 使用 Workbook.getWorksheets.get(index) 方法获取 Excel 文件中的特定工作表。
- 使用 Worksheet.getPivotTables().get(index) 方法获取工作表中的特定数据透视表。
- 使用 Worksheet.getCharts().add() 方法向工作表添加数据透视图。
- 设置数据透视图的位置和标题。
- 使用 Chart.setDisplayAxisFieldButtons() 和 Chart.setDisplayValueFieldButtons() 等方法隐藏数据透视图中的轴字段按钮和数值字段按钮等。
- 使用 Workbook.saveToFile() 方法保存生成的文件。
- C#
import com.spire.xls.*;
import com.spire.xls.core.IPivotTable;
public class ShowOrHideFieldButtons {
public static void main(String[] args) {
// 创建 Workbook 对象
Workbook workbook = new Workbook();
// 加载 Excel 文件
workbook.loadFromFile("示例.xlsx");
// 获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// 获取工作表中的第一个数据透视表
IPivotTable pivotTable = sheet.getPivotTables().get(0);
// 基于数据透视表创建数据透视图
Chart pivotChart = sheet.getCharts().add(ExcelChartType.ColumnClustered, pivotTable);
// 设置图表位置
pivotChart.setTopRow(1);
pivotChart.setLeftColumn(11);
pivotChart.setRightColumn(20);
pivotChart.setBottomRow(15);
// 将图表标题设置为 null
pivotChart.setChartTitle("");
// 隐藏特定字段按钮
pivotChart.setDisplayAxisFieldButtons(false);
pivotChart.setDisplayValueFieldButtons(false);
// pivotChart.setDisplayLegendFieldButtons(false);
// pivotChart.setShowReportFilterFieldButtons(false);
// pivotChart.setDisplayEntireFieldButtons(false);
// 保存生成的文件
workbook.saveToFile("隐藏字段按钮.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}
Java 给 Excel 数据透视图系列设置格式
当使用 Spire.XLS for Java 基于数据透视表生成数据透视图时,数据透视图的系列不会自动创建。你需要向数据透视图添加系列,然后再应用所需的格式。详细步骤如下:
- 创建 Workbook 类的对象。
- 使用 Workbook.loadFromFile() 方法加载 Excel 文件。
- 使用 Workbook.getWorksheets.get(index) 方法获取 Excel 文件中的特定工作表。
- 使用 Worksheet.getPivotTables().get(index) 方法获取工作表中的特定数据透视表。
- 使用 Worksheet.getCharts().add() 方法向工作表添加数据透视图。
- 设置数据透视图的位置和标题。
- 使用 Chart.getSeries().Add() 方法向数据透视图添加系列,然后对系列应用所需的格式。
- 使用 Workbook.saveToFile() 方法保存生成的文件。
- C#
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import com.spire.xls.core.IPivotTable;
public class FormatChartSeries {
public static void main(String[] args) {
// 创建 Workbook 对象
Workbook workbook = new Workbook();
// 加载 Excel 文件
workbook.loadFromFile("示例.xlsx");
// 获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// 获取工作表中的第一个数据透视表
IPivotTable pivotTable = sheet.getPivotTables().get(0);
// 基于数据透视表创建数据透视图
Chart pivotChart = sheet.getCharts().add(ExcelChartType.ColumnClustered, pivotTable);
// 设置图表位置
pivotChart.setTopRow(1);
pivotChart.setLeftColumn(11);
pivotChart.setRightColumn(20);
pivotChart.setBottomRow(15);
// 将图表标题设置为 null
pivotChart.setChartTitle("");
// 添加图表系列
ChartSerie series = pivotChart.getSeries().add(ExcelChartType.ColumnClustered);
// 设置数据条的宽度
series.getCommonSerieFormat().setGapWidth(10);
// series.getCommonSerieFormat().setOverlap(100);
// 保存生成的文件
workbook.saveToFile("设置数据透视图系列格式.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。