Skip to content

Commit 9c78163

Browse files
committed
Sanitize connection name in case the use saves the connection from Excel (default ODC name is copied from connection name).
1 parent 9076806 commit 9c78163

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

AnalyzeInExcel/ExcelHelper.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Windows.Threading;
55
using System.Runtime.InteropServices;
66
using Excel = Microsoft.Office.Interop.Excel;
7+
using System.Linq;
78

89
namespace 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

Comments
 (0)