示例#1
0
        /// <summary>
        /// Creates a <see cref="ExcelInterop.PivotTable"/> in a new <see cref="ExcelInterop.Worksheet"/> that contains all <see cref="ExcelInterop.ModelTable"/>s in the active <see cref="ExcelInterop.Workbook"/>.
        /// </summary>
        private void CreatePivotTableForAllRelatedModelTables()
        {
            if (!CreatePivotTableCheckBox.Checked || PivotTablesComboBox.SelectedIndex != 0)
            {
                return;
            }

            var activeWorkbook = Globals.ThisAddIn.ActiveWorkbook;
            var workbookDataModelConnection = activeWorkbook.Connections.Cast <ExcelInterop.WorkbookConnection>().FirstOrDefault(wbConn => wbConn.Name == ExcelUtilities.WORKBOOK_DATA_MODEL_CONNECTION_NAME);

            if (workbookDataModelConnection == null)
            {
                return;
            }

            // Create a new Excel Worksheet for the PivotTable since it will contain all related tables in the Workbook's data model
            var currentWorksheet = activeWorkbook.CreateWorksheet(DATA_MODEL_PIVOT_TABLE_PROPOSED_NAME, true);

            if (currentWorksheet == null)
            {
                return;
            }

            ExcelUtilities.CreatePivotTable(workbookDataModelConnection, Globals.ThisAddIn.Application.ActiveCell, DATA_MODEL_PIVOT_TABLE_PROPOSED_NAME);
        }