Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
.xls 文件)和现代的命名或自定义数据透视表样式(用于 .xlsx、.xlsm 和 .xlsb 文件)。应调用的 API 取决于工作簿保存到的文件格式,而非加载时的格式。
Aspose.Cells 为数据透视表提供两套并行的样式 API。两者之间的选择由工作簿保存到的文件格式决定,而非读取时的格式。从 .xls 文件加载的工作簿可以重新保存为 .xlsx,此时将应用现代样式 API,而非传统 API。
PivotTable.pivotTableStyleType 用于选择内置的命名样式之一(浅色和深色主题,包括 Excel 2017 中新增的样式)。这些预设为只读。PivotTable.pivotTableStyleName 用于选择您通过 Worksheets.getTableStyles().addPivotTableStyle(...) 自行定义的自定义样式。当您希望修改预置样式之外的颜色、边框或字体时,必须使用自定义样式。
此外,PivotTable.formatAll(Style) 是一个快捷方式,可将单个 Style 对象应用于数据透视表的每个单元格,从而覆盖通过上述任一样式名称 API 所设置的内容。当需要统一外观而不考虑底层主题时,此方法非常有用。PivotTable.autoFormatType 接受来自 Aspose.Cells.Pivot.PivotTableAutoFormatType 枚举的值。可用值包括 Report1 至 Report10、Classic 以及 Table1 至 Table10。
以下示例加载一个新工作簿,填入 Fruit/Year/Amount 示例数据,添加一个数据透视表,应用 PivotTableAutoFormatType.Report5,并将结果保存为 .xls。
Report1 至 Report10、Table1 至 Table10)是经典 Excel 中为单维数据透视表设计的,仅包含行字段和值——它们没有为列字段标题提供内置样式。如果您的数据透视表需要列字段,请改用场景 2 中的现代 PivotTableStyleType 预设,这些预设专为现代 Excel 使用的二维布局而设计。
let workbook = new AsposeCells.Workbook();
// 获取第一个工作表
let sheet = workbook.getWorksheets().get(0);
// 填充源数据,包括表头行(Fruit、Year、Amount)
// 以及 9 行数据,涵盖 2020 和 2021 年的葡萄、蓝莓、猕猴桃、樱桃
sheet.getCells().get(0, 0).putValue("Fruit");
sheet.getCells().get(0, 1).putValue("Year");
sheet.getCells().get(0, 2).putValue("Amount");
sheet.getCells().get(1, 0).putValue("grape");
sheet.getCells().get(1, 1).putValue(2020);
sheet.getCells().get(1, 2).putValue(50);
sheet.getCells().get(2, 0).putValue("blueberry");
sheet.getCells().get(2, 1).putValue(2020);
sheet.getCells().get(2, 2).putValue(30);
sheet.getCells().get(3, 0).putValue("kiwi");
sheet.getCells().get(3, 1).putValue(2020);
sheet.getCells().get(3, 2).putValue(25);
sheet.getCells().get(4, 0).putValue("cherry");
sheet.getCells().get(4, 1).putValue(2020);
sheet.getCells().get(4, 2).putValue(40);
sheet.getCells().get(5, 0).putValue("grape");
sheet.getCells().get(5, 1).putValue(2021);
sheet.getCells().get(5, 2).putValue(60);
sheet.getCells().get(6, 0).putValue("blueberry");
sheet.getCells().get(6, 1).putValue(2021);
sheet.getCells().get(6, 2).putValue(35);
sheet.getCells().get(7, 0).putValue("kiwi");
sheet.getCells().get(7, 1).putValue(2021);
sheet.getCells().get(7, 2).putValue(28);
sheet.getCells().get(8, 0).putValue("cherry");
sheet.getCells().get(8, 1).putValue(2021);
sheet.getCells().get(8, 2).putValue(45);
sheet.getCells().get(9, 0).putValue("grape");
sheet.getCells().get(9, 1).putValue(2020);
sheet.getCells().get(9, 2).putValue(45);
// 在目标单元格 E3 添加一个名为 "Pivot1" 的数据透视表,源数据范围为 A1:C10
let pivotIndex = sheet.getPivotTables().add("A1:C10", "E3", "Pivot1");
let pivotTable = sheet.getPivotTables().get(pivotIndex);
// 分配字段:Fruit -> 行,Amount -> 数据
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.ROW, "Fruit");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.DATA, "Amount");
// 应用旧版 XLS 预设自动格式 "Report5"
// 注意:此属性仅在保存为 .xls 格式时有效。
// 当保存为 .xlsx/.xlsm/.xlsb 格式时,Excel 会忽略 AutoFormatType,
// 而使用 PivotTableStyleType / PivotTableStyleName 所指定的样式。
pivotTable.setAutoFormatType(AsposeCells.PivotTableAutoFormatType.REPORT_5);
// 将工作簿保存为旧版 .xls 格式
workbook.save("output.xls");
无法修改内置预设。当您需要覆盖颜色、边框或字体时,必须定义自定义数据透视表样式。该工作流包含三个步骤:
Worksheets.getTableStyles().addPivotTableStyle(String name) 向工作簿的 TableStyles 集合添加自定义样式。该方法返回新创建样式的索引。TableStyle.tableStyleElements.add(TableStyleElementType) 添加元素(例如 WholeTable 或 GrandTotalRow)来配置样式,然后通过 TableStyleElement.setElementStyle(Style) 为每个元素分配一个 Style。PivotTable.pivotTableStyleName 设置为样式名称,将自定义样式应用于数据透视表。此处不要使用 pivotTableStyleType,因为该属性用于选择内置预设。pivotTableStyleName 和 pivotTableStyleType 不能互换使用。使用 pivotTableStyleType 选择内置预设,使用 pivotTableStyleName 选择通过 addPivotTableStyle 定义的自定义样式。同时设置两者是无害的,但只有与预期来源匹配的那一个会被渲染。
可用的 TableStyleElementType 值包括 WholeTable、FirstRow、LastRow、FirstColumn、LastColumn、GrandTotalRow、GrandTotalColumn、PageFieldLabels 和 PageFieldValues。
以下示例定义了一个自定义数据透视表样式,在 WholeTable 上使用细黑边框,在 GrandTotalRow 上使用粗体红色字体,然后通过 pivotTableStyleName 应用该样式并保存为 .xlsx。
let workbook = new AsposeCells.Workbook();
let worksheet = workbook.getWorksheets().get(0);
// 填充源数据:标题行 + 9 行数据 (A1:C10)
worksheet.getCells().get("A1").putValue("Fruit");
worksheet.getCells().get("B1").putValue("Year");
worksheet.getCells().get("C1").putValue("Amount");
worksheet.getCells().get("A2").putValue("Grape");
worksheet.getCells().get("B2").putValue(2020);
worksheet.getCells().get("C2").putValue(100);
worksheet.getCells().get("A3").putValue("Blueberry");
worksheet.getCells().get("B3").putValue(2020);
worksheet.getCells().get("C3").putValue(200);
worksheet.getCells().get("A4").putValue("Kiwi");
worksheet.getCells().get("B4").putValue(2020);
worksheet.getCells().get("C4").putValue(300);
worksheet.getCells().get("A5").putValue("Cherry");
worksheet.getCells().get("B5").putValue(2020);
worksheet.getCells().get("C5").putValue(400);
worksheet.getCells().get("A6").putValue("Grape");
worksheet.getCells().get("B6").putValue(2021);
worksheet.getCells().get("C6").putValue(500);
worksheet.getCells().get("A7").putValue("Blueberry");
worksheet.getCells().get("B7").putValue(2021);
worksheet.getCells().get("C7").putValue(600);
worksheet.getCells().get("A8").putValue("Kiwi");
worksheet.getCells().get("B8").putValue(2021);
worksheet.getCells().get("C8").putValue(700);
worksheet.getCells().get("A9").putValue("Cherry");
worksheet.getCells().get("B9").putValue(2021);
worksheet.getCells().get("C9").putValue(800);
worksheet.getCells().get("A10").putValue("Grape");
worksheet.getCells().get("B10").putValue(2021);
worksheet.getCells().get("C10").putValue(900);
// 添加数据透视表,源数据为 A1:C10,锚定在 E3,名称为 "Pivot1"
let pivotIndex = worksheet.getPivotTables().add("A1:C10", "E3", "Pivot1");
let pivotTable = worksheet.getPivotTables().get(pivotIndex);
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.ROW, "Fruit");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.COLUMN, "Year");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.DATA, "Amount");
// 步骤 1:注册一个新的自定义数据透视表样式并捕获其索引
let styleIndex = workbook.getWorksheets().getTableStyles().addPivotTableStyle("CustomPivotStyle");
let tableStyle = workbook.getWorksheets().getTableStyles().get(styleIndex);
// 步骤 2:添加一个 WholeTable 元素并在四边应用细黑色边框
let wholeTableElementIndex = tableStyle.getTableStyleElements().add(AsposeCells.TableStyleElementType.WHOLE_TABLE);
let wholeTableElement = tableStyle.getTableStyleElements().get(wholeTableElementIndex);
let wholeTableStyle = workbook.createStyle();
let topBorder = wholeTableStyle.getBorders().get(AsposeCells.BorderType.TOP_BORDER);
topBorder.setLineStyle(AsposeCells.CellBorderType.THIN);
topBorder.setColor(AsposeCells.Color.BLACK);
let bottomBorder = wholeTableStyle.getBorders().get(AsposeCells.BorderType.BOTTOM_BORDER);
bottomBorder.setLineStyle(AsposeCells.CellBorderType.THIN);
bottomBorder.setColor(AsposeCells.Color.BLACK);
let leftBorder = wholeTableStyle.getBorders().get(AsposeCells.BorderType.LEFT_BORDER);
leftBorder.setLineStyle(AsposeCells.CellBorderType.THIN);
leftBorder.setColor(AsposeCells.Color.BLACK);
let rightBorder = wholeTableStyle.getBorders().get(AsposeCells.BorderType.RIGHT_BORDER);
rightBorder.setLineStyle(AsposeCells.CellBorderType.THIN);
rightBorder.setColor(AsposeCells.Color.BLACK);
wholeTableElement.setElementStyle(wholeTableStyle);
// 步骤 3:添加一个 GrandTotalRow 元素并应用粗体红色字体
let grandTotalElementIndex = tableStyle.getTableStyleElements().add(AsposeCells.TableStyleElementType.GRAND_TOTAL_ROW);
let grandTotalElement = tableStyle.getTableStyleElements().get(grandTotalElementIndex);
let grandTotalStyle = workbook.createStyle();
grandTotalStyle.getFont().setBold(true);
grandTotalStyle.getFont().setColor(AsposeCells.Color.RED);
grandTotalElement.setElementStyle(grandTotalStyle);
// 步骤 4:按名称应用自定义样式(不要使用 PivotTableStyleType,那是用于内置预设样式的)
pivotTable.setPivotTableStyleName("CustomPivotStyle");
workbook.save("output.xlsx");
PivotTable.formatAll(Style) 是一个快捷方式,可将单个 Style 对象应用于数据透视表的每个单元格,包括数据区域、行和列标题以及总计。先前通过 pivotTableStyleType 或 pivotTableStyleName 设置的内容都将被覆盖。
formatAll 会覆盖 pivotTableStyleType 和 pivotTableStyleName。仅当需要在整个数据透视表中获得统一且不依赖主题的外观时才使用它。
以下示例创建了一个具有黄色纯色填充、粗体深蓝色字体以及四周细黑边框的 Style,然后使用 formatAll 应用该样式并保存为 .xlsx。
let workbook = new AsposeCells.Workbook();
let worksheet = workbook.getWorksheets().get(0);
// 填充源数据:表头行(第1行)+ 9行数据(第2-10行)
worksheet.getCells().get("A1").putValue("Fruit");
worksheet.getCells().get("B1").putValue("Year");
worksheet.getCells().get("C1").putValue("Amount");
worksheet.getCells().get("A2").putValue("Grape");
worksheet.getCells().get("B2").putValue(2020);
worksheet.getCells().get("C2").putValue(5000);
worksheet.getCells().get("A3").putValue("Blueberry");
worksheet.getCells().get("B3").putValue(2020);
worksheet.getCells().get("C3").putValue(3000);
worksheet.getCells().get("A4").putValue("Kiwi");
worksheet.getCells().get("B4").putValue(2020);
worksheet.getCells().get("C4").putValue(4000);
worksheet.getCells().get("A5").putValue("Cherry");
worksheet.getCells().get("B5").putValue(2020);
worksheet.getCells().get("C5").putValue(2000);
worksheet.getCells().get("A6").putValue("Grape");
worksheet.getCells().get("B6").putValue(2021);
worksheet.getCells().get("C6").putValue(6000);
worksheet.getCells().get("A7").putValue("Blueberry");
worksheet.getCells().get("B7").putValue(2021);
worksheet.getCells().get("C7").putValue(3500);
worksheet.getCells().get("A8").putValue("Kiwi");
worksheet.getCells().get("B8").putValue(2021);
worksheet.getCells().get("C8").putValue(4500);
worksheet.getCells().get("A9").putValue("Cherry");
worksheet.getCells().get("B9").putValue(2021);
worksheet.getCells().get("C9").putValue(2500);
worksheet.getCells().get("A10").putValue("Grape");
worksheet.getCells().get("B10").putValue(2021);
worksheet.getCells().get("C10").putValue(5500);
// 添加数据透视表:源数据区域 A1:C10,目标单元格 E3,名称 "Pivot1"
let pivotIndex = worksheet.getPivotTables().add("A1:C10", "E3", "Pivot1");
let pivotTable = worksheet.getPivotTables().get(pivotIndex);
// 分配透视字段:Fruit -> 行区域,Year -> 列区域,Amount -> 数据区域
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Fruit");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Column, "Year");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Amount");
// 创建一个将强制应用于数据透视表每个单元格的样式
let style = workbook.createStyle();
style.setForegroundColor(AsposeCells.Color.Yellow);
style.setPattern(AsposeCells.BackgroundType.Solid);
style.getFont().setIsBold(true);
style.getFont().setColor(AsposeCells.Color.DarkBlue);
style.getBorders().get(AsposeCells.BorderType.TopBorder).setLineStyle(AsposeCells.CellBorderType.Thin);
style.getBorders().get(AsposeCells.BorderType.TopBorder).setColor(AsposeCells.Color.Black);
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Thin);
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Black);
style.getBorders().get(AsposeCells.BorderType.LeftBorder).setLineStyle(AsposeCells.CellBorderType.Thin);
style.getBorders().get(AsposeCells.BorderType.LeftBorder).setColor(AsposeCells.Color.Black);
style.getBorders().get(AsposeCells.BorderType.RightBorder).setLineStyle(AsposeCells.CellBorderType.Thin);
style.getBorders().get(AsposeCells.BorderType.RightBorder).setColor(AsposeCells.Color.Black);
// 应用 FormatAll:强制将此单个样式应用于数据透视表的每个单元格,
// 覆盖之前设置的任何 PivotTableStyleType / PivotTableStyleName
pivotTable.formatAll(style);
// 以现代 .xlsx 格式保存工作簿
workbook.save("output.xlsx");
样式 API 的选择取决于您要保存到的文件格式。请使用下表作为快速参考。
| 目标文件格式 | 要使用的 API | 备注 |
|---|---|---|
.xls(传统) |
PivotTable.autoFormatType |
值来自 Aspose.Cells.Pivot.PivotTableAutoFormatType(例如 Report1–Report10、Classic、Table1–Table10)。在保存为现代格式时将被忽略。 |
.xlsx / .xlsm / .xlsb(现代,内置样式) |
PivotTable.pivotTableStyleType |
值来自 Aspose.Cells.PivotTableStyleType(浅色/深色主题,包括 Excel 2017 新增内容)。 |
.xlsx / .xlsm / .xlsb(现代,自定义样式) |
PivotTable.pivotTableStyleName + Worksheets.getTableStyles().addPivotTableStyle(...) |
当内置预设不够用时使用。通过 TableStyleElement.setElementStyle(...) 进行配置。 |
| 任何格式(统一覆盖) | PivotTable.formatAll(Style) |
快捷方式,可覆盖整个数据透视表中的所有其他样式设置。 |
如有疑问,请保存为 .xlsx,并对内置主题使用 pivotTableStyleType,对自定义主题使用 pivotTableStyleName。 |
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.