11using System ;
2+ using System . Runtime . InteropServices ;
23
34namespace AnalyzeInExcel
45{
@@ -13,5 +14,108 @@ public static bool IsExcelAvailable()
1314 var type = Type . GetTypeFromProgID ( "Excel.Application" ) ;
1415 return ( type != null ) ;
1516 }
17+
18+ public static void CreateInstanceWithPivotTable ( string serverName , string databaseName , string cubeName )
19+ {
20+ //const int XlSheetType_xlWorksheet = -4167; // Excel.XlSheetType.xlWorksheet
21+ const int XlLayoutRowType_xlCompactRow = 0 ; // Excel.XlLayoutRowType.xlCompactRow
22+ const int XlPivotTableSourceType_xlExternal = 2 ; // Excel.XlPivotTableSourceType.xlExternal
23+ const int XlPivotFieldRepeatLabels_xlRepeatLabels = 2 ; // Excel.XlPivotFieldRepeatLabels.xlRepeatLabels
24+ //const int XlPivotFieldOrientation_xlRowField = 1; // Excel.XlPivotFieldOrientation.xlRowField
25+
26+ var connectionString = ModelHelper . GetOleDbConnectionString ( serverName , databaseName ) ;
27+ var connectionName = $ "AnalyzeInExcel [{ serverName } ].[{ databaseName } ].[{ cubeName } ]";
28+ var commandText = cubeName ;
29+ var pivotTableName = $ "AnalyzeInExcelPivotTable";
30+
31+ var type = Type . GetTypeFromProgID ( "Excel.Application" ) ;
32+ //if (type == null)
33+ // return;
34+
35+ dynamic app = Activator . CreateInstance ( type ) ;
36+ try
37+ {
38+ app . Visible = true ;
39+
40+ var workbook = app . Workbooks . Add ( ) ;
41+
42+ var workbookConnection = workbook . Connections . Add (
43+ Name : connectionName ,
44+ Description : "" ,
45+ ConnectionString : $ "OLEDB;{ connectionString } ",
46+ CommandText : commandText ,
47+ lCmdtype : 1
48+ ) ;
49+
50+ var pivotCache = workbook . PivotCaches ( ) . Create (
51+ SourceType : XlPivotTableSourceType_xlExternal ,
52+ SourceData : workbookConnection
53+ ) ;
54+
55+ #region Configure PivotCache
56+
57+ pivotCache . RefreshOnFileOpen = false ;
58+
59+ #endregion
60+
61+ var worksheet = workbook . ActiveSheet ;
62+ //var worksheet = workbook.Worksheets.Add(
63+ // Type: XlSheetType_xlWorksheet
64+ // );
65+
66+ var pivotTable = pivotCache . CreatePivotTable (
67+ TableDestination : worksheet . Range [ "A1" ] ,
68+ TableName : pivotTableName ,
69+ ReadData : false
70+ ) ;
71+
72+ #region Configure PivotTable
73+
74+ pivotTable . ColumnGrand = true ;
75+ pivotTable . HasAutoFormat = true ;
76+ pivotTable . DisplayErrorString = true ;
77+ pivotTable . DisplayNullString = true ;
78+ pivotTable . EnableDrilldown = true ;
79+ pivotTable . ErrorString = "" ;
80+ pivotTable . MergeLabels = false ;
81+ pivotTable . NullString = "" ;
82+ pivotTable . PageFieldOrder = 2 ;
83+ pivotTable . PageFieldWrapCount = 0 ;
84+ pivotTable . PreserveFormatting = true ;
85+ pivotTable . RowGrand = true ;
86+ pivotTable . PrintTitles = false ;
87+ pivotTable . RepeatItemsOnEachPrintedPage = true ;
88+ pivotTable . TotalsAnnotation = true ;
89+ pivotTable . CompactRowIndent = 1 ;
90+ pivotTable . VisualTotals = false ;
91+ pivotTable . InGridDropZones = false ;
92+ pivotTable . DisplayFieldCaptions = true ;
93+ pivotTable . DisplayMemberPropertyTooltips = true ;
94+ pivotTable . DisplayContextTooltips = true ;
95+ pivotTable . ShowDrillIndicators = true ;
96+ pivotTable . PrintDrillIndicators = false ;
97+ pivotTable . DisplayEmptyRow = false ;
98+ pivotTable . DisplayEmptyColumn = false ;
99+ pivotTable . AllowMultipleFilters = false ;
100+ pivotTable . SortUsingCustomLists = true ;
101+ pivotTable . DisplayImmediateItems = true ;
102+ pivotTable . ViewCalculatedMembers = true ;
103+ pivotTable . EnableWriteback = false ;
104+ pivotTable . ShowValuesRow = false ;
105+ pivotTable . CalculatedMembersInFilters = true ;
106+ pivotTable . RowAxisLayout ( XlLayoutRowType_xlCompactRow ) ;
107+ pivotTable . RepeatAllLabels ( XlPivotFieldRepeatLabels_xlRepeatLabels ) ;
108+
109+ #endregion
110+
111+ //var field1 = pivotTable.CubeFields.Item[1];
112+ //field1.Orientation = XlPivotFieldOrientation_xlRowField;
113+ //field1.Position = 1;
114+ }
115+ finally
116+ {
117+ Marshal . ReleaseComObject ( app ) ;
118+ }
119+ }
16120 }
17121}
0 commit comments