Repository navigation
Expand file tree
/
Copy pathExcelExt.cs
More file actions
137 lines (110 loc) · 4.53 KB
/
Copy pathExcelExt.cs
File metadata and controls
137 lines (110 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Interop.Excel;
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
namespace Selenium.Internal {
class ExcelExt {
static CultureInfo CULTURE_US = new CultureInfo("en-US");
/// <summary>
/// Gets a range object from an existing instance or new one
/// if source is null, then adds a new worksheet and return the used range
/// if source is Range, then return the range
/// if source is Worksheet, then return the used range
/// if source is string, then parse the source and return the range
/// </summary>
/// <param name="source">Reference, range or worksheet</param>
/// <returns>Range</returns>
public static IRange GetRange(object source) {
//Get a running instance of Excel or create one
IExcel excel = ExcelExt.GetInstance();
//Handle null source
IWorksheets worksheets = excel.Worksheets;
if (source == null) {
//Add a new sheet to the current workbook
IWorksheet lastWorksheet = worksheets[worksheets.Count];
IWorksheet newWorksheet = worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing);
return newWorksheet.UsedRange;
}
//Handle source as address
if (source is string) {
//try parse worksheet name
var srctxt = source as string;
if (srctxt != null && srctxt.IndexOf('!') == -1 && srctxt.IndexOf(':') == -1) {
try {
return ((IWorksheet)worksheets[source]).UsedRange;
} catch { }
}
//try parse range name
try {
return excel[source, Type.Missing];
} catch {
throw new ExcelSourceError("Failed to resolve the source:\n{0}", source);
}
}
//Handle source as Worksheet
IWorksheet worksheet = source as IWorksheet;
if (worksheet != null)
return worksheet.UsedRange;
//Handle source as Range
IRange range = source as IRange;
if (range != null)
return range;
throw new ExcelSourceError("Invalid type of source: {0}", source.GetType().Name);
}
/// <summary>
/// Returns an active instance of Excel or a new one.
/// </summary>
/// <returns></returns>
public static IExcel GetInstance() {
//Set a compatible culture
Thread.CurrentThread.CurrentCulture = CULTURE_US;
//Get the running instance of Excel or create one if there isn't.
bool isCreatedApp = false;
Object app = null;
Guid excelguid = typeof(Excel).GUID;
NativeMethods.GetActiveObject(ref excelguid, IntPtr.Zero, out app);
if (app == null) {
try {
app = new Excel();
isCreatedApp = true;
} catch {
throw new ExcelError("Failed to launch Excel.");
}
}
//Cast to interface
IExcel excel = app as IExcel;
if (excel == null)
throw new ExcelError("Failed to interface with Excel.");
//Adds a worksheet if it's an empty instance of Excel
if (isCreatedApp) {
IWorkbook workbook = excel.Workbooks.Add();
workbook.Worksheets.Add();
excel.Visible = true;
}
return excel;
}
class NativeMethods {
const String OLE32 = "oleaut32.dll";
[DllImport(OLE32, PreserveSig = true)]
public static extern void GetActiveObject(ref Guid rclsid, IntPtr reserved
, [MarshalAs(UnmanagedType.Interface)] out Object ppunk);
}
#region Errors
/// <summary>
/// Excel error
/// </summary>
public class ExcelError : SeleniumError {
internal ExcelError(string message, params object[] args)
: base(message, args) { }
}
/// <summary>
/// Error occuring when a source is incorrect.
/// </summary>
public class ExcelSourceError : ExcelError {
internal ExcelSourceError(string message, params object[] args)
: base(message, args) { }
}
#endregion
}
}