簡體   English   中英

使用 c#.net 在 MS Excel 2010 中使用數據透視表格式化數據

[英]Format data using pivot table in MS Excel 2010 using c#.net

我有一個包含太多原始數據的 Excel 工作表。 現在我想使用數據透視表工具更改同一工作簿中另一個 Excel 工作表中特定格式的數據表示。

截至目前,為了以所需格式表示數據,我在 Excel 中執行了一些手動步驟,如下所述,因為這些步驟為我提供了客戶所需的結果格式:

  1. 選擇工作表
  2. 插入選項卡 --> 數據透視表
  3. 出現創建數據透視表對話框:從源表中選擇一個表(選擇要分析的數據= Sheet1!$A:$I)
  4. 選擇我們想要放置數據透視表報告的新工作表。
  5. 選擇要添加到報告的字段 - 字段 1、字段 2、字段 3、字段 4、字段 5、字段 6、字段 7、字段 8、字段 9- 作為行標簽
  6. 功能區中的數據透視表工具--> 選項--> 取消選擇“+/- 按鈕”
  7. 功能區中的數據透視表工具--> 設計--> 報表布局--> 以表格形式顯示
  8. 功能區中的數據透視表工具--> 設計--> SubTotatls--> 不顯示小計
  9. 功能區中的數據透視表工具--> 設計--> 總計--> 行和列關閉

下面提到了excel表的源格式

原始數據

現在我想使用 C#.net 以編程方式完成所有這些手動步驟。 最終結果應采用以下格式:

結果格式 - 數據透視表

請提及源代碼來做同樣的事情。

            Excel.Application excelApp = new Excel.Application();

            Excel.Workbook excelWorkBook = excelApp.Workbooks.Open("c:\\Users\\username\\Desktop\\Test.xlsx");

            Excel.Worksheet excelworksheet = excelWorkBook.ActiveSheet;

            Excel.Worksheet sheet2 = excelWorkBook.Sheets.Add(); // Added new sheet to create Pivot Table
            sheet2.Name = "Pivot Table"; // Assigned sheet Name
            excelworksheet.Activate();

            Excel.Range oRange = excelworksheet.UsedRange;
            Excel.PivotCache oPivotCache = (Excel.PivotCache)excelWorkBook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlDatabase, oRange);  // Set the Source data range from First sheet
            Excel.Range oRange2 = sheet2.Cells[1, 1];
            Excel.PivotCaches pch = excelWorkBook.PivotCaches();
            pch.Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, oRange).CreatePivotTable(sheet2.Cells[1, 1], "PivTbl_1", Type.Missing, Type.Missing);// Create Pivot table
            Excel.PivotTable pvt = sheet2.PivotTables("PivTbl_1") as Excel.PivotTable;

            pvt.ShowDrillIndicators = false;  // Used to remove the Expand/ Collapse Button from each cell

            Excel.PivotField fld = ((Excel.PivotField)pvt.PivotFields("ColumnA")); // Create a Pivot Field in Pivot table
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField; // Add the pivot field as Row Field
            fld.set_Subtotals(1, false); //Remove Subtotals for each row and column 
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnB"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            fld.set_Subtotals(1, false);
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnC"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            fld.set_Subtotals(1, false);
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnD"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            fld.set_Subtotals(1, false);
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnE"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            fld.set_Subtotals(1, false);
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnF"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            fld.set_Subtotals(1, false);
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnG"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            fld.set_Subtotals(1, false);
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnH"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            fld.set_Subtotals(1, false);
            fld = ((Excel.PivotField)pvt.PivotFields("ColumnI"));
            fld.Orientation = Excel.XlPivotFieldOrientation.xlDataField; // Sort column set as datafield to show the Pivot table as per requirement- It will show the total count of data and not needed so later on we will hide this Column

            sheet2.UsedRange.Columns.AutoFit();  // Used to Autoset the column width according to data 

            //Set Conditional Formating for "Access" Column if Cell of the Access Column Contain W then Set Background color Light Green/ If R then Set Misty Rose Cell's Back Ground Color
            Excel.FormatCondition SetBgColorForAccessW = sheet2.get_Range("H:H", Type.Missing).FormatConditions.Add(Excel.XlFormatConditionType.xlTextString, Type.Missing, Type.Missing, Type.Missing, "w", Excel.XlContainsOperator.xlContains, Type.Missing, Type.Missing);
            SetBgColorForAccessW.Interior.Color = Color.LightGreen;
            Excel.FormatCondition SetBgColorForAccessR = sheet2.get_Range("H:H", Type.Missing).FormatConditions.Add(Excel.XlFormatConditionType.xlTextString, Type.Missing, Type.Missing, Type.Missing, "r", Excel.XlContainsOperator.xlContains, Type.Missing, Type.Missing);
            SetBgColorForAccessR.Interior.Color = Color.MistyRose;

            sheet2.get_Range("I:I").EntireColumn.Hidden = true; // Used to hide Sort Column as not needed and not have relavent data

            pvt.ColumnGrand = false;  // Used to hide Grand total for columns
            pvt.RowGrand = false; // Used to hide Grand total for Rows

            excelApp.DisplayAlerts = false;  // Used to hide unappropriate message prompt from Excel
            excelworksheet.Delete(); // Delete the Sheet with Raw data because not needed and we created new sheet which represent data in pivot table format
            sheet2.Activate(); // Set focus on Sheet Containing data in Pivot table format
            sheet2.get_Range("J1", "J1").Select(); // Set focus of column J to hide Pivot Table Field List (Left pane) when we open the file
            excelWorkBook.SaveAs(OutputPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);// Used to Save as and overwrite the Excel file if already exist
            excelApp.DisplayAlerts = true;  // Reset the property of Excel
            excelWorkBook.Close(); // Close the workbook
            excelApp.Quit(); // Quit the Excel application;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM