没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2020-01-13 09:47:21.320|阅读 600 次
概述:Excel中的数据透视表被广泛用于数据的汇总和分析。基于Excel数据透视表的重要性,本文旨在向您展示如何在Excel中创建数据透视表并排序或隐藏数据。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
Aspose.Cells for .NET是Excel电子表格编程API,可加快电子表格管理和处理任务,同时支持构建具有生成,修改,转换,呈现和打印电子表格功能的跨平台应用程序。
用于生成和操作Excel电子表格的自动化解决方案如今已被广泛使用。Excel中的数据透视表被广泛用于数据的汇总和分析。而对数据透视表中的数据进行排序对于检查Excel电子表格中的大量数据非常有用。
数据透视表中的数据排序可用于按字母顺序(AZ或ZA)排列文本值的项目,或在数字的情况下从最高值到最低值或从最低值到最高值。基于Excel数据透视表的重要性,本文旨在向您展示如何:
如果你还没有使用过Aspose.Cells,可以点击此处下载最新版体验。
为了进行演示,下面图示的Excel电子表格将在整个示例中使用。
首先让我们看看如何使用Aspose.Cells for .NET在C#中创建Excel数据透视表。创建数据透视表后,我们将隐藏行并根据其列或行字段对数据进行排序。下面的代码示例演示如何创建Excel数据透视表。
Workbook wb = new Workbook("SampleExcel.xlsx"); // Obtaining the reference of the newly added worksheet Worksheet sheet = wb.Worksheets[0]; PivotTableCollection pivotTables = sheet.PivotTables; // source PivotTable // Adding a PivotTable to the worksheet int index = pivotTables.Add("=Sheet1!A1:C10", "E3", "PivotTable2"); //Accessing the instance of the newly added PivotTable PivotTable pivotTable = pivotTables[index]; // Unshowing grand totals for rows. pivotTable.RowGrand = false; pivotTable.ColumnGrand = false; // Dragging the first field to the row area. pivotTable.AddFieldToArea(PivotFieldType.Row, 1); PivotField rowField = pivotTable.RowFields[0]; rowField.IsAutoSort = true; rowField.IsAscendSort = true; // Dragging the second field to the column area. pivotTable.AddFieldToArea(PivotFieldType.Column, 0); PivotField colField = pivotTable.ColumnFields[0]; colField.NumberFormat = "dd/mm/yyyy"; colField.IsAutoSort = true; colField.IsAscendSort = true; // Dragging the third field to the data area. pivotTable.AddFieldToArea(PivotFieldType.Data, 2); pivotTable.RefreshData(); pivotTable.CalculateData(); // end of source PivotTable //Saving the Excel file wb.Save("output.xlsx");
输出结果
现在,我们将创建另一个数据透视表,并对数据进行排序。下面的代码示例创建并按“ SeaFood”行字段值对数据透视表进行排序。
Workbook wb = new Workbook("SampleExcel.xlsx"); // Obtaining the reference of the Excel worksheet. Worksheet sheet = wb.Worksheets[0]; PivotTableCollection pivotTables = sheet.PivotTables; // Adding a PivotTable to the Excel worksheet. int index = pivotTables.Add("=Sheet1!A1:C10", "E3", "PivotTable2"); // Accessing the instance of the newly added PivotTable. PivotTable pivotTable = pivotTables[index]; // Unshowing grand totals for rows. pivotTable.RowGrand = false; pivotTable.ColumnGrand = false; // Dragging the first field to the row area. pivotTable.AddFieldToArea(PivotFieldType.Row, 1); PivotField rowField = pivotTable.RowFields[0]; rowField.IsAutoSort = true; rowField.IsAscendSort = true; // Dragging the second field to the column area. pivotTable.AddFieldToArea(PivotFieldType.Column, 0); PivotField colField = pivotTable.ColumnFields[0]; colField.NumberFormat = "dd/mm/yyyy"; colField.IsAutoSort = true; colField.IsAscendSort = true; colField.AutoSortField = 0; // Dragging the third field to the data area. pivotTable.AddFieldToArea(PivotFieldType.Data, 2); pivotTable.RefreshData(); pivotTable.CalculateData(); // Saving the Excel file. wb.Save("output.xlsx");
输出结果
以下C#代码示例对“ 28/07/2000”列的字段值进行排序。
Workbook wb = new Workbook("SampleExcel.xlsx"); // Obtaining the reference of the Excel worksheet. Worksheet sheet = wb.Worksheets[0]; PivotTableCollection pivotTables = sheet.PivotTables; // Adding a PivotTable to the Excel worksheet. int index = pivotTables.Add("=Sheet1!A1:C10", "E3", "PivotTable2"); // Accessing the instance of the newly added PivotTable. PivotTable pivotTable = pivotTables[index]; // Unshowing grand totals for rows. pivotTable.RowGrand = false; pivotTable.ColumnGrand = false; // Dragging the first field to the row area. pivotTable.AddFieldToArea(PivotFieldType.Row, 1); PivotField rowField = pivotTable.RowFields[0]; rowField.IsAutoSort = true; rowField.IsAscendSort = true; colField.AutoSortField = 0; // Dragging the second field to the column area. pivotTable.AddFieldToArea(PivotFieldType.Column, 0); PivotField colField = pivotTable.ColumnFields[0]; colField.NumberFormat = "dd/mm/yyyy"; colField.IsAutoSort = true; colField.IsAscendSort = true; // Dragging the third field to the data area. pivotTable.AddFieldToArea(PivotFieldType.Data, 2); pivotTable.RefreshData(); pivotTable.CalculateData(); // Saving the Excel file. wb.Save("output.xlsx");
输出结果
根据希望应用的某些条件隐藏Excel数据透视表中的行。下面的代码示例展示了如何使用c#隐藏数据透视表中的特定行。
Workbook workbook = new Workbook("output.xlsx"); Worksheet worksheet = workbook.Worksheets[0]; var pivotTable = worksheet.PivotTables[0]; var dataBodyRange = pivotTable.DataBodyRange; int currentRow = 1; int rowsUsed = dataBodyRange.EndRow; // Sorting values in descending PivotField field = pivotTable.RowFields[0]; field.IsAutoSort = true; field.IsAscendSort = false; field.AutoSortField = 0; pivotTable.RefreshData(); pivotTable.CalculateData(); // Hiding rows with value less than 15 while (currentRow < rowsUsed) { Cell cell = worksheet.Cells[currentRow, 2]; double score = Convert.ToDouble(cell.Value); if (score < 15) { worksheet.Cells.HideRow(currentRow); } currentRow++; } pivotTable.RefreshData(); pivotTable.CalculateData(); // Saving the Excel file workbook.Save("PivotTableHideAndSort.xlsx");
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@wqylolg.cn
在线协同文档编辑器ONLYOFFICE现已支持阿里通义千问( Qwen),带来先进的 AI 功能,实现更智能的文档编辑。本指南将向您展示如何将 Qwen 连接到 ONLYOFFICE,并充分利用其功能。
需要从 PDF 文档中删除特定页面?本快速指南将向您展示如何仅用几行代码删除不需要的页面。
本教程将向您展示如何用MyEclipse开发EJB 3无状态会话Bean,欢迎下载最新版IDE体验!
DHTMLX 产品支持构建功能丰富的预订系统,从而简化针对不同行业的预约安排。Scheduler 组件丰富的功能(可自定义的日历视图、重复事件等)与 Booking 小部件现成的预订管理 UI 相结合,满足您构建现代化 Web 预订解决方案所需。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@wqylolg.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢