翻譯|使用教程|編輯:李顯亮|2020-01-13 09:47:21.320|閱讀 652 次
概述:Excel中的數(shù)據(jù)透視表被廣泛用于數(shù)據(jù)的匯總和分析?;贓xcel數(shù)據(jù)透視表的重要性,本文旨在向您展示如何在Excel中創(chuàng)建數(shù)據(jù)透視表并排序或隱藏數(shù)據(jù)。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務(wù),同時支持構(gòu)建具有生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印電子表格功能的跨平臺應(yīng)用程序。
用于生成和操作Excel電子表格的自動化解決方案如今已被廣泛使用。Excel中的數(shù)據(jù)透視表被廣泛用于數(shù)據(jù)的匯總和分析。而對數(shù)據(jù)透視表中的數(shù)據(jù)進行排序?qū)τ跈z查Excel電子表格中的大量數(shù)據(jù)非常有用。
數(shù)據(jù)透視表中的數(shù)據(jù)排序可用于按字母順序(AZ或ZA)排列文本值的項目,或在數(shù)字的情況下從最高值到最低值或從最低值到最高值?;贓xcel數(shù)據(jù)透視表的重要性,本文旨在向您展示如何:
如果你還沒有使用過Aspose.Cells,可以點擊此處下載最新版體驗。
為了進行演示,下面圖示的Excel電子表格將在整個示例中使用。
首先讓我們看看如何使用Aspose.Cells for .NET在C#中創(chuàng)建Excel數(shù)據(jù)透視表。創(chuàng)建數(shù)據(jù)透視表后,我們將隱藏行并根據(jù)其列或行字段對數(shù)據(jù)進行排序。下面的代碼示例演示如何創(chuàng)建Excel數(shù)據(jù)透視表。
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");
輸出結(jié)果
現(xiàn)在,我們將創(chuàng)建另一個數(shù)據(jù)透視表,并對數(shù)據(jù)進行排序。下面的代碼示例創(chuàng)建并按“ SeaFood”行字段值對數(shù)據(jù)透視表進行排序。
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");
輸出結(jié)果
以下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");
輸出結(jié)果
根據(jù)希望應(yīng)用的某些條件隱藏Excel數(shù)據(jù)透視表中的行。下面的代碼示例展示了如何使用c#隱藏數(shù)據(jù)透視表中的特定行。
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");
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn