44using System . Windows . Threading ;
55using System . Runtime . InteropServices ;
66using Excel = Microsoft . Office . Interop . Excel ;
7+ using System . Linq ;
78
89namespace AnalyzeInExcel
910{
@@ -23,6 +24,13 @@ public static bool IsExcelAvailable()
2324 return ( type != null ) ;
2425 }
2526
27+ public static string GetSafeConnectionName ( string serverName , string databaseName , string cubeName )
28+ {
29+ string connectionName = $ "AnalyzeInExcel [{ serverName } ].[{ databaseName } ].[{ cubeName } ]";
30+ connectionName = System . IO . Path . GetInvalidFileNameChars ( ) . Aggregate ( connectionName , ( current , c ) => current . Replace ( c . ToString ( ) , "_" ) ) ;
31+ return connectionName ;
32+ }
33+
2634 /// <summary>
2735 /// Create a new Excel file with a PivotTable connected to the server/database/cube provided
2836 /// </summary>
@@ -34,7 +42,7 @@ public static bool IsExcelAvailable()
3442 public static bool CreateInstanceWithPivotTableViaInterop ( string serverName , string databaseName , string cubeName , out string excelVersion , Action < Exception > exceptionAction )
3543 {
3644 var connectionString = ModelHelper . GetOleDbConnectionString ( serverName , databaseName ) ;
37- var connectionName = $ "AnalyzeInExcel [ { serverName } ].[ { databaseName } ].[ { cubeName } ]" ;
45+ var connectionName = GetSafeConnectionName ( serverName , databaseName , cubeName ) ;
3846 var commandText = cubeName ;
3947 var pivotTableName = $ "AnalyzeInExcelPivotTable";
4048
@@ -93,7 +101,7 @@ public static bool CreateInstanceWithPivotTableViaInterop(string serverName, str
93101 public static bool CreateInstanceWithPivotTableViaDispatch ( string serverName , string databaseName , string cubeName , out string excelVersion , Action < Exception > exceptionAction )
94102 {
95103 string connectionString = ModelHelper . GetOleDbConnectionString ( serverName , databaseName ) ;
96- string connectionName = $ "AnalyzeInExcel [ { serverName } ].[ { databaseName } ].[ { cubeName } ]" ;
104+ string connectionName = GetSafeConnectionName ( serverName , databaseName , cubeName ) ;
97105 string commandText = cubeName ;
98106 string pivotTableName = $ "AnalyzeInExcelPivotTable";
99107 string foundExcelVersion = null ;
0 commit comments