Skip to content

Commit 33021f8

Browse files
committed
Added --experiment setting to enable direct load of Excel without ODC file.
1 parent ddbdf21 commit 33021f8

9 files changed

Lines changed: 225 additions & 96 deletions

File tree

AnalyzeInExcel/AnalyzeInExcel.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
<Generator>MSBuild:Compile</Generator>
115115
<SubType>Designer</SubType>
116116
</ApplicationDefinition>
117+
<Compile Include="SplashLoading.xaml.cs">
118+
<DependentUpon>SplashLoading.xaml</DependentUpon>
119+
</Compile>
117120
<Compile Include="TelemetryHelper.cs" />
118121
<Page Include="MainWindow.xaml">
119122
<Generator>MSBuild:Compile</Generator>
@@ -128,6 +131,10 @@
128131
<DependentUpon>MainWindow.xaml</DependentUpon>
129132
<SubType>Code</SubType>
130133
</Compile>
134+
<Page Include="SplashLoading.xaml">
135+
<SubType>Designer</SubType>
136+
<Generator>MSBuild:Compile</Generator>
137+
</Page>
131138
</ItemGroup>
132139
<ItemGroup>
133140
<Compile Include="ModelHelper.cs" />

AnalyzeInExcel/App.xaml.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using AutoUpdaterDotNET;
88
using System.Windows.Input;
9+
using System.Threading.Tasks;
910

1011
namespace AnalyzeInExcel
1112
{
@@ -106,9 +107,29 @@ protected override void OnStartup(StartupEventArgs e)
106107
// TODO request action / configuration to users
107108
}
108109

109-
ExcelHelper.CreateInstanceWithPivotTable(serverName, databaseName, cubeName);
110-
//RunExcelProcess(serverName, databaseName, cubeName);
111-
th.TrackEvent("Run Excel");
110+
var splashScreen = new SplashLoading();
111+
try
112+
{
113+
this.MainWindow = splashScreen;
114+
splashScreen.Show();
115+
116+
bool experiment = (((App)Current).AppOptions != null) ? ((App)Current).AppOptions.Experiment : false;
117+
bool excelStarted = false;
118+
if (experiment)
119+
{
120+
excelStarted = ExcelHelper.CreateInstanceWithPivotTable(serverName, databaseName, cubeName, (ex) => th.TrackException(ex));
121+
if (excelStarted) th.TrackEvent("Run Excel Experiment");
122+
}
123+
if (!excelStarted)
124+
{
125+
RunExcelProcess(serverName, databaseName, cubeName);
126+
th.TrackEvent("Run Excel");
127+
}
128+
}
129+
finally
130+
{
131+
splashScreen.Close();
132+
}
112133
}
113134
else
114135
{
@@ -163,6 +184,7 @@ private void OpenDiagnosticWindow(string serverName, string databaseName)
163184
{
164185
//// start application window
165186
MainWindow mw = new MainWindow();
187+
this.MainWindow = mw;
166188
mw.diagnosticInfo.Content = $@"Current configuration
167189
Server={serverName ?? "(blank)"}
168190
Database={databaseName ?? "(blank)"}

AnalyzeInExcel/ExcelHelper.cs

Lines changed: 97 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
using System;
2+
using System.Threading;
3+
using System.Windows;
4+
using System.Windows.Threading;
25
using System.Runtime.InteropServices;
36

47
namespace 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
}

AnalyzeInExcel/Options.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ public class Options
1212

1313
[Option('n', "telemetry", HelpText = "Enable Telemetry")]
1414
public bool Telemetry { get; set; }
15+
16+
[Option('x', "experiment", HelpText = "Experiment feature")]
17+
public bool Experiment { get; set; }
1518
}
1619
}

AnalyzeInExcel/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@
5151
// You can specify all the values or you can default the Build and Revision Numbers
5252
// by using the '*' as shown below:
5353
// [assembly: AssemblyVersion("1.0.*")]
54-
[assembly: AssemblyVersion("1.0.4.0")]
55-
[assembly: AssemblyFileVersion("1.0.4.0")]
54+
[assembly: AssemblyVersion("1.0.5.0")]
55+
[assembly: AssemblyFileVersion("1.0.5.0")]

AnalyzeInExcel/SplashLoading.xaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Window x:Class="AnalyzeInExcel.SplashLoading"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:AnalyzeInExcel"
7+
mc:Ignorable="d"
8+
WindowStartupLocation="Manual"
9+
WindowStyle="None"
10+
AllowsTransparency="True"
11+
Height="30"
12+
Width="200">
13+
<Grid>
14+
<ProgressBar IsIndeterminate="True" />
15+
<TextBlock HorizontalAlignment="Center"
16+
VerticalAlignment="Center">Starting Excel...</TextBlock>
17+
</Grid>
18+
</Window>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Controls;
9+
using System.Windows.Data;
10+
using System.Windows.Documents;
11+
using System.Windows.Input;
12+
using System.Windows.Media;
13+
using System.Windows.Media.Imaging;
14+
using System.Windows.Shapes;
15+
16+
namespace AnalyzeInExcel
17+
{
18+
/// <summary>
19+
/// Interaction logic for SplashLoading.xaml
20+
/// </summary>
21+
public partial class SplashLoading : Window
22+
{
23+
public struct POINT
24+
{
25+
public int X;
26+
public int Y;
27+
28+
public POINT(int x, int y)
29+
{
30+
this.X = x;
31+
this.Y = y;
32+
}
33+
}
34+
35+
[DllImport("user32.dll")]
36+
[return: MarshalAs(UnmanagedType.Bool)]
37+
public static extern bool GetCursorPos(out POINT pPoint);
38+
39+
public static int SM_CXSCREEN = 0; // GetSystemMetrics index.
40+
[DllImport("USER32.DLL", SetLastError = true)]
41+
public static extern int GetSystemMetrics(int nIndex);
42+
43+
public SplashLoading()
44+
{
45+
InitializeComponent();
46+
47+
Point ptMouse = GetCursorPosition();
48+
49+
Left = ptMouse.X;
50+
Top = ptMouse.Y;
51+
}
52+
53+
private static Point GetCursorPosition()
54+
{
55+
POINT cursorScreenPosition;
56+
GetCursorPos(out cursorScreenPosition);
57+
58+
double widthInDevicePixels = SplashLoading.GetSystemMetrics(SplashLoading.SM_CXSCREEN);
59+
double widthInDIP = SystemParameters.WorkArea.Right; // Device independent pixels.
60+
double scalingFactor = widthInDIP / widthInDevicePixels;
61+
62+
var ptMouse = new System.Windows.Point(cursorScreenPosition.X * scalingFactor, cursorScreenPosition.Y * scalingFactor);
63+
return ptMouse;
64+
}
65+
}
66+
}

ExternalToolsInstaller/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.4.0")]
36-
[assembly: AssemblyFileVersion("1.0.4.0")]
35+
[assembly: AssemblyVersion("1.0.5.0")]
36+
[assembly: AssemblyFileVersion("1.0.5.0")]

0 commit comments

Comments
 (0)