11using System ;
2+ using System . Threading ;
3+ using System . Windows ;
4+ using System . Windows . Threading ;
25using System . Runtime . InteropServices ;
36
47namespace AnalyzeInExcel
58{
69 public class ExcelHelper
710 {
11+ [ DllImport ( "user32.dll" ) ]
12+ [ return : MarshalAs ( UnmanagedType . Bool ) ]
13+ static extern bool SetForegroundWindow ( IntPtr hWnd ) ;
14+
815 /// <summary>
916 /// Check whether Excel is installed
1017 /// </summary>
@@ -15,107 +22,113 @@ public static bool IsExcelAvailable()
1522 return ( type != null ) ;
1623 }
1724
18- public static void CreateInstanceWithPivotTable ( string serverName , string databaseName , string cubeName )
25+ public static bool CreateInstanceWithPivotTable ( string serverName , string databaseName , string cubeName , Action < Exception > exceptionAction )
1926 {
20- //const int XlSheetType_xlWorksheet = -4167; // Excel.XlSheetType.xlWorksheet
2127 const int XlLayoutRowType_xlCompactRow = 0 ; // Excel.XlLayoutRowType.xlCompactRow
2228 const int XlPivotTableSourceType_xlExternal = 2 ; // Excel.XlPivotTableSourceType.xlExternal
2329 const int XlPivotFieldRepeatLabels_xlRepeatLabels = 2 ; // Excel.XlPivotFieldRepeatLabels.xlRepeatLabels
24- //const int XlPivotFieldOrientation_xlRowField = 1; // Excel.XlPivotFieldOrientation.xlRowField
25-
30+
2631 var connectionString = ModelHelper . GetOleDbConnectionString ( serverName , databaseName ) ;
2732 var connectionName = $ "AnalyzeInExcel [{ serverName } ].[{ databaseName } ].[{ cubeName } ]";
2833 var commandText = cubeName ;
2934 var pivotTableName = $ "AnalyzeInExcelPivotTable";
3035
3136 var type = Type . GetTypeFromProgID ( "Excel.Application" ) ;
32- // if (type == null)
33- // return;
37+ if ( type == null )
38+ return false ;
3439
3540 dynamic app = Activator . CreateInstance ( type ) ;
3641 try
3742 {
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;
43+ try
44+ {
45+ var workbook = app . Workbooks . Add ( ) ;
46+
47+ var workbookConnection = workbook . Connections . Add (
48+ Name : connectionName ,
49+ Description : "" ,
50+ ConnectionString : $ "OLEDB;{ connectionString } ",
51+ CommandText : commandText ,
52+ lCmdtype : 1
53+ ) ;
54+
55+ var pivotCache = workbook . PivotCaches ( ) . Create (
56+ SourceType : XlPivotTableSourceType_xlExternal ,
57+ SourceData : workbookConnection
58+ ) ;
59+
60+ #region Configure PivotCache
61+
62+ pivotCache . RefreshOnFileOpen = false ;
63+
64+ #endregion
65+
66+ var worksheet = workbook . ActiveSheet ;
67+
68+ var pivotTable = pivotCache . CreatePivotTable (
69+ TableDestination : worksheet . Range [ "A1" ] ,
70+ TableName : pivotTableName ,
71+ ReadData : false
72+ ) ;
73+
74+ #region Configure PivotTable
75+
76+ pivotTable . ColumnGrand = true ;
77+ pivotTable . HasAutoFormat = true ;
78+ pivotTable . DisplayErrorString = true ;
79+ pivotTable . DisplayNullString = true ;
80+ pivotTable . EnableDrilldown = true ;
81+ pivotTable . ErrorString = "" ;
82+ pivotTable . MergeLabels = false ;
83+ pivotTable . NullString = "" ;
84+ pivotTable . PageFieldOrder = 2 ;
85+ pivotTable . PageFieldWrapCount = 0 ;
86+ pivotTable . PreserveFormatting = true ;
87+ pivotTable . RowGrand = true ;
88+ pivotTable . PrintTitles = false ;
89+ pivotTable . RepeatItemsOnEachPrintedPage = true ;
90+ pivotTable . TotalsAnnotation = true ;
91+ pivotTable . CompactRowIndent = 1 ;
92+ pivotTable . VisualTotals = false ;
93+ pivotTable . InGridDropZones = false ;
94+ pivotTable . DisplayFieldCaptions = true ;
95+ pivotTable . DisplayMemberPropertyTooltips = true ;
96+ pivotTable . DisplayContextTooltips = true ;
97+ pivotTable . ShowDrillIndicators = true ;
98+ pivotTable . PrintDrillIndicators = false ;
99+ pivotTable . DisplayEmptyRow = false ;
100+ pivotTable . DisplayEmptyColumn = false ;
101+ pivotTable . AllowMultipleFilters = false ;
102+ pivotTable . SortUsingCustomLists = true ;
103+ pivotTable . DisplayImmediateItems = true ;
104+ pivotTable . ViewCalculatedMembers = true ;
105+ pivotTable . EnableWriteback = false ;
106+ pivotTable . ShowValuesRow = false ;
107+ pivotTable . CalculatedMembersInFilters = true ;
108+ pivotTable . RowAxisLayout ( XlLayoutRowType_xlCompactRow ) ;
109+ pivotTable . RepeatAllLabels ( XlPivotFieldRepeatLabels_xlRepeatLabels ) ;
110+
111+ #endregion
112+
113+ // Show Excel
114+ app . Visible = true ;
115+
116+ // Set Excel window as foreground window
117+ var hwnd = app . Hwnd ;
118+ SetForegroundWindow ( ( IntPtr ) hwnd ) ; // Note Hwnd is declared as int
119+ }
120+ finally
121+ {
122+ Marshal . ReleaseComObject ( app ) ;
123+ }
114124 }
115- finally
125+ catch ( Exception ex )
116126 {
117- Marshal . ReleaseComObject ( app ) ;
127+ // In case of error simply fails the request and forward the exception
128+ exceptionAction ? . Invoke ( ex ) ;
129+ return false ;
118130 }
131+ return true ;
119132 }
120133 }
121134}
0 commit comments