Skip to content

Commit 90a973c

Browse files
committed
added support for loading from byte array or System.IO.Stream
1 parent 8f5c16c commit 90a973c

10 files changed

Lines changed: 245 additions & 17 deletions

File tree

ExcelOps-EpplusFreeFixCalcsEdition/EpplusFreeExcelDataOperations.vb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ Namespace ExcelOps
2222
MyBase.New(file, mode, False, True, [readOnly], passwordForOpening)
2323
End Sub
2424

25+
Public Sub New(data As Byte(), passwordForOpening As String)
26+
MyBase.New(data, False, True, passwordForOpening)
27+
End Sub
28+
29+
Public Sub New(data As Byte(), passwordForOpening As String, disableInitialCalculation As Boolean)
30+
MyBase.New(data, Not disableInitialCalculation, True, passwordForOpening)
31+
End Sub
32+
33+
Public Sub New(data As System.IO.Stream, passwordForOpening As String)
34+
MyBase.New(data, False, True, passwordForOpening)
35+
End Sub
36+
37+
Public Sub New(data As System.IO.Stream, passwordForOpening As String, disableInitialCalculation As Boolean)
38+
MyBase.New(data, Not disableInitialCalculation, True, passwordForOpening)
39+
End Sub
40+
2541
Public Sub New()
2642
Me.New(Nothing)
2743
End Sub

ExcelOps-EpplusPolyform/EpplusPolyformExcelDataOperations.vb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ Namespace ExcelOps
4949
ValidateLicenseContext(Me)
5050
End Sub
5151

52+
Public Sub New(data As Byte(), passwordForOpening As String)
53+
MyBase.New(data, True, False, passwordForOpening)
54+
ValidateLicenseContext(Me)
55+
End Sub
56+
57+
Public Sub New(data As Byte(), passwordForOpening As String, disableInitialCalculation As Boolean)
58+
MyBase.New(data, Not disableInitialCalculation, False, passwordForOpening)
59+
ValidateLicenseContext(Me)
60+
End Sub
61+
62+
Public Sub New(data As System.IO.Stream, passwordForOpening As String)
63+
MyBase.New(data, True, False, passwordForOpening)
64+
ValidateLicenseContext(Me)
65+
End Sub
66+
67+
Public Sub New(data As System.IO.Stream, passwordForOpening As String, disableInitialCalculation As Boolean)
68+
MyBase.New(data, Not disableInitialCalculation, False, passwordForOpening)
69+
ValidateLicenseContext(Me)
70+
End Sub
71+
5272
Public Sub New()
5373
Me.New(Nothing)
5474
End Sub

ExcelOps-FreeSpireXls/FreeSpireXlsDataOperations.vb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ Namespace ExcelOps
5151
MyBase.New(file, mode, True, False, [readOnly], passwordForOpening)
5252
End Sub
5353

54+
Public Sub New(data As Byte(), passwordForOpening As String)
55+
MyBase.New(data, True, False, passwordForOpening)
56+
End Sub
57+
58+
Public Sub New(data As Byte(), passwordForOpening As String, disableInitialCalculation As Boolean)
59+
MyBase.New(data, Not disableInitialCalculation, False, passwordForOpening)
60+
End Sub
61+
62+
Public Sub New(data As System.IO.Stream, passwordForOpening As String)
63+
MyBase.New(data, True, False, passwordForOpening)
64+
End Sub
65+
66+
Public Sub New(data As System.IO.Stream, passwordForOpening As String, disableInitialCalculation As Boolean)
67+
MyBase.New(data, Not disableInitialCalculation, False, passwordForOpening)
68+
End Sub
69+
5470
''' <summary>
5571
''' Create a new excel engine instance (reminder: set System.Threading.Thread.CurrentThread.CurrentCulture as required BEFORE creating the instance to ensure the engine uses the correct culture later on)
5672
''' </summary>

ExcelOps-SpireXls/SpireXlsDataOperations.vb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ Namespace ExcelOps
4242
If AllowInstancingForNonLicencedContextForTestingPurposesOnly = False AndAlso IsLicensedContext = False Then Throw New LicenseException(GetType(Spire.License.LicenseProvider), Nothing, "Correct licensing required, see Spire.License.LicenseProvider and https://www.e-iceblue.com/")
4343
End Sub
4444

45+
Public Sub New(data As Byte(), passwordForOpening As String)
46+
MyBase.New(data, True, False, passwordForOpening)
47+
End Sub
48+
49+
Public Sub New(data As Byte(), passwordForOpening As String, disableInitialCalculation As Boolean)
50+
MyBase.New(data, Not disableInitialCalculation, False, passwordForOpening)
51+
End Sub
52+
53+
Public Sub New(data As System.IO.Stream, passwordForOpening As String)
54+
MyBase.New(data, True, False, passwordForOpening)
55+
End Sub
56+
57+
Public Sub New(data As System.IO.Stream, passwordForOpening As String, disableInitialCalculation As Boolean)
58+
MyBase.New(data, Not disableInitialCalculation, False, passwordForOpening)
59+
End Sub
60+
4561
''' <summary>
4662
''' Create a new excel engine instance (reminder: set System.Threading.Thread.CurrentThread.CurrentCulture as required BEFORE creating the instance to ensure the engine uses the correct culture later on)
4763
''' </summary>

ExcelOpsTest-FreeSpireXls/ExcelOpsTests.Engines/FreeSpireXlsOpsTest.vb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ Namespace ExcelOpsTests.Engines
2323
MyBase.TestInCultureContext_AssignCurrentThreadCulture()
2424
End Sub
2525

26+
Protected Overrides Function _CreateInstance(data() As Byte, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.FreeSpireXlsDataOperations
27+
Return New ExcelOps.FreeSpireXlsDataOperations(data, passwordForOpening, disableCalculationEngine)
28+
End Function
29+
30+
Protected Overrides Function _CreateInstance(data As IO.Stream, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.FreeSpireXlsDataOperations
31+
Return New ExcelOps.FreeSpireXlsDataOperations(data, passwordForOpening, disableCalculationEngine)
32+
End Function
33+
2634
End Class
2735

2836
End Namespace

ExcelOpsTest-SpireXls/ExcelOpsTests.Engines/SpireXlsOpsTest.vb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ Namespace ExcelOpsTests.Engines
5050
Assert.False(CompuMaster.Excel.ExcelOps.Utils.IsLicensedContext)
5151
End Sub
5252

53+
Protected Overrides Function _CreateInstance(data() As Byte, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.SpireXlsDataOperations
54+
Return New ExcelOps.SpireXlsDataOperations(data, passwordForOpening, disableCalculationEngine)
55+
End Function
56+
57+
Protected Overrides Function _CreateInstance(data As IO.Stream, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.SpireXlsDataOperations
58+
Return New ExcelOps.SpireXlsDataOperations(data, passwordForOpening, disableCalculationEngine)
59+
End Function
60+
5361
End Class
5462

5563
End Namespace

ExcelOpsTest/ExcelOpsTests.Engines/EpplusFreeFixCalcsEditionOpsTest.vb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Namespace ExcelOpsTests.Engines
88
Public Overrides ReadOnly Property ExpectedEngineName As String = "Epplus 4 (LGPL)"
99

1010
Protected Overrides Function _CreateInstance(file As String, mode As ExcelOps.ExcelDataOperationsBase.OpenMode, [readOnly] As Boolean, passwordForOpening As String, disableInitialCalculation As Boolean) As ExcelOps.EpplusFreeExcelDataOperations
11+
'disableCalculationEngine not required since always disabled calc-module by engine
1112
Return New ExcelOps.EpplusFreeExcelDataOperations(file, mode, [readOnly], passwordForOpening)
1213
End Function
1314

@@ -19,6 +20,16 @@ Namespace ExcelOpsTests.Engines
1920
Assert.Throws(Of NotSupportedException)(Sub() MyBase.CopySheetContent())
2021
End Sub
2122

23+
Protected Overrides Function _CreateInstance(data() As Byte, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.EpplusFreeExcelDataOperations
24+
'disableCalculationEngine not required since always disabled calc-module by engine
25+
Return New ExcelOps.EpplusFreeExcelDataOperations(data, passwordForOpening)
26+
End Function
27+
28+
Protected Overrides Function _CreateInstance(data As IO.Stream, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.EpplusFreeExcelDataOperations
29+
'disableCalculationEngine not required since always disabled calc-module by engine
30+
Return New ExcelOps.EpplusFreeExcelDataOperations(data, passwordForOpening)
31+
End Function
32+
2233
End Class
2334

2435
End Namespace

ExcelOpsTest/ExcelOpsTests.Engines/EpplusPolyformEditionOpsTest.vb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ Namespace ExcelOpsTests.Engines
2727
Assert.Throws(Of NotSupportedException)(Sub() MyBase.CopySheetContent())
2828
End Sub
2929

30+
Protected Overrides Function _CreateInstance(data() As Byte, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.EpplusPolyformExcelDataOperations
31+
Return New ExcelOps.EpplusPolyformExcelDataOperations(data, passwordForOpening, disableCalculationEngine)
32+
End Function
33+
34+
Protected Overrides Function _CreateInstance(data As IO.Stream, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.EpplusPolyformExcelDataOperations
35+
Return New ExcelOps.EpplusPolyformExcelDataOperations(data, passwordForOpening, disableCalculationEngine)
36+
End Function
37+
3038
End Class
3139

3240
End Namespace

ExcelOpsTest/ExcelOpsTests.Engines/ExcelOpsTestBase.vb

Lines changed: 135 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Namespace ExcelOpsTests.Engines
2121

2222
#Disable Warning CA1716 ' Bezeichner dürfen nicht mit Schlüsselwörtern übereinstimmen
2323
Protected MustOverride Function _CreateInstance(file As String, mode As ExcelOps.ExcelDataOperationsBase.OpenMode, [readOnly] As Boolean, passwordForOpening As String, disableCalculationEngine As Boolean) As T
24+
Protected MustOverride Function _CreateInstance(data As Byte(), passwordForOpening As String, disableCalculationEngine As Boolean) As T
25+
Protected MustOverride Function _CreateInstance(data As System.IO.Stream, passwordForOpening As String, disableCalculationEngine As Boolean) As T
2426
#Enable Warning CA1716 ' Bezeichner dürfen nicht mit Schlüsselwörtern übereinstimmen
2527

2628
''' <summary>
@@ -63,6 +65,88 @@ Namespace ExcelOpsTests.Engines
6365
Return Me.CreateInstance(file, mode, [readOnly], passwordForOpening, False)
6466
End Function
6567

68+
''' <summary>
69+
''' Create a new excel engine instance (reminder: set System.Threading.Thread.CurrentThread.CurrentCulture as required BEFORE creating the instance to ensure the engine uses the correct culture later on)
70+
''' </summary>
71+
''' <param name="data"></param>
72+
''' <param name="passwordForOpening"></param>
73+
''' <returns></returns>
74+
Protected Function CreateInstance(data As Byte(), passwordForOpening As String) As T
75+
Return Me.CreateInstance(data, passwordForOpening, False)
76+
End Function
77+
78+
''' <summary>
79+
''' Create a new excel engine instance (reminder: set System.Threading.Thread.CurrentThread.CurrentCulture as required BEFORE creating the instance to ensure the engine uses the correct culture later on)
80+
''' </summary>
81+
''' <param name="data"></param>
82+
''' <param name="passwordForOpening"></param>
83+
''' <param name="disableCalculationEngine"></param>
84+
''' <returns></returns>
85+
Protected Function CreateInstance(data As Byte(), passwordForOpening As String, disableCalculationEngine As Boolean) As T
86+
Try
87+
Return _CreateInstance(data, passwordForOpening, disableCalculationEngine)
88+
Catch ex As Exception
89+
If ex.GetType() Is GetType(PlatformNotSupportedException) Then
90+
Throw
91+
ElseIf ex.GetType() Is GetType(CompuMaster.ComInterop.ComApplicationNotAvailableException) Then
92+
Throw
93+
Else
94+
Dim InnerEx As Exception = ex.InnerException
95+
Do While InnerEx IsNot Nothing
96+
If InnerEx.GetType() Is GetType(PlatformNotSupportedException) Then
97+
Throw InnerEx
98+
ElseIf InnerEx.GetType() Is GetType(CompuMaster.ComInterop.ComApplicationNotAvailableException) Then
99+
Throw InnerEx
100+
Else
101+
InnerEx = InnerEx.InnerException
102+
End If
103+
Loop
104+
End If
105+
Throw
106+
End Try
107+
End Function
108+
109+
''' <summary>
110+
''' Create a new excel engine instance (reminder: set System.Threading.Thread.CurrentThread.CurrentCulture as required BEFORE creating the instance to ensure the engine uses the correct culture later on)
111+
''' </summary>
112+
''' <param name="data"></param>
113+
''' <param name="passwordForOpening"></param>
114+
''' <returns></returns>
115+
Protected Function CreateInstance(data As System.IO.Stream, passwordForOpening As String) As T
116+
Return Me.CreateInstance(data, passwordForOpening, False)
117+
End Function
118+
119+
''' <summary>
120+
''' Create a new excel engine instance (reminder: set System.Threading.Thread.CurrentThread.CurrentCulture as required BEFORE creating the instance to ensure the engine uses the correct culture later on)
121+
''' </summary>
122+
''' <param name="data"></param>
123+
''' <param name="passwordForOpening"></param>
124+
''' <param name="disableCalculationEngine"></param>
125+
''' <returns></returns>
126+
Protected Function CreateInstance(data As System.IO.Stream, passwordForOpening As String, disableCalculationEngine As Boolean) As T
127+
Try
128+
Return _CreateInstance(data, passwordForOpening, disableCalculationEngine)
129+
Catch ex As Exception
130+
If ex.GetType() Is GetType(PlatformNotSupportedException) Then
131+
Throw
132+
ElseIf ex.GetType() Is GetType(CompuMaster.ComInterop.ComApplicationNotAvailableException) Then
133+
Throw
134+
Else
135+
Dim InnerEx As Exception = ex.InnerException
136+
Do While InnerEx IsNot Nothing
137+
If InnerEx.GetType() Is GetType(PlatformNotSupportedException) Then
138+
Throw InnerEx
139+
ElseIf InnerEx.GetType() Is GetType(CompuMaster.ComInterop.ComApplicationNotAvailableException) Then
140+
Throw InnerEx
141+
Else
142+
InnerEx = InnerEx.InnerException
143+
End If
144+
Loop
145+
End If
146+
Throw
147+
End Try
148+
End Function
149+
66150
''' <summary>
67151
''' Create a new excel engine instance (reminder: set System.Threading.Thread.CurrentThread.CurrentCulture as required BEFORE creating the instance to ensure the engine uses the correct culture later on)
68152
''' </summary>
@@ -202,6 +286,40 @@ Namespace ExcelOpsTests.Engines
202286

203287
End Sub
204288

289+
<Test> Public Sub LoadFileFromByteArray()
290+
Dim Wb As T
291+
'Testfile without password
292+
Dim TestFile As String = TestEnvironment.FullPathOfExistingTestFile("test_data", "ExcelOpsGrund01.xlsx")
293+
Dim Data As Byte() = System.IO.File.ReadAllBytes(TestFile)
294+
If GetType(T) Is GetType(MsExcelDataOperations) Then
295+
'known to fail because no support for reading files from in-memory
296+
Assert.Throws(Of NotSupportedException)(Sub()
297+
Me.CreateInstance(Data, "")
298+
End Sub)
299+
Else
300+
Wb = Me.CreateInstance(Data, "")
301+
Assert.AreEqual("Grunddaten", Wb.SheetNames(0))
302+
Assert.That(Wb.ReadOnly, [Is].True)
303+
End If
304+
End Sub
305+
306+
<Test> Public Sub LoadFileFromStream()
307+
Dim Wb As T
308+
'Testfile without password
309+
Dim TestFile As String = TestEnvironment.FullPathOfExistingTestFile("test_data", "ExcelOpsGrund01.xlsx")
310+
Dim Data As System.IO.Stream = New System.IO.FileStream(TestFile, System.IO.FileMode.Open)
311+
If GetType(T) Is GetType(MsExcelDataOperations) Then
312+
'known to fail because no support for reading files from in-memory
313+
Assert.Throws(Of NotSupportedException)(Sub()
314+
Me.CreateInstance(Data, "")
315+
End Sub)
316+
Else
317+
Wb = Me.CreateInstance(Data, "")
318+
Assert.AreEqual("Grunddaten", Wb.SheetNames(0))
319+
Assert.That(Wb.ReadOnly, [Is].True)
320+
End If
321+
End Sub
322+
205323
<Test> Public Sub PasswordForOpening()
206324
Dim Wb As T
207325
'Testfile without password
@@ -518,23 +636,23 @@ Namespace ExcelOpsTests.Engines
518636
End If
519637

520638
eppeo.SelectSheet(0)
521-
eppeo.SaveAs(TestEnvironment.FullPathOfDynTestFile(eppeo, "SelectedSheet.Grunddaten.0.xlsx"), ExcelDataOperationsBase.SaveOptionsForDisabledCalculationEngines.NoReset)
522-
System.Console.WriteLine("OUT: " & eppeo.FilePath)
523-
Assert.That(eppeo.SelectedSheetName, [Is].EqualTo("Grunddaten"))
524-
525-
eppeo = Me.CreateInstance(TestFileName, ExcelDataOperationsBase.OpenMode.OpenExistingFile, True, Nothing)
526-
eppeo.SelectSheet(1)
527-
eppeo.SaveAs(TestEnvironment.FullPathOfDynTestFile(eppeo, "SelectedSheet.Ausgewählt.1.xlsx"), ExcelDataOperationsBase.SaveOptionsForDisabledCalculationEngines.NoReset)
528-
System.Console.WriteLine("OUT: " & eppeo.FilePath)
529-
Assert.That(eppeo.SelectedSheetName, [Is].EqualTo("Ausgewählt"))
530-
531-
eppeo = Me.CreateInstance(TestFileName, ExcelDataOperationsBase.OpenMode.OpenExistingFile, True, Nothing)
532-
eppeo.SelectSheet(2)
533-
eppeo.SaveAs(TestEnvironment.FullPathOfDynTestFile(eppeo, "SelectedSheet.Kostenplanung.2.xlsx"), ExcelDataOperationsBase.SaveOptionsForDisabledCalculationEngines.NoReset)
534-
System.Console.WriteLine("OUT: " & eppeo.FilePath)
535-
Assert.That(eppeo.SelectedSheetName, [Is].EqualTo("Kostenplanung"))
536-
537-
eppeo.SelectSheet("Grunddaten")
639+
eppeo.SaveAs(TestEnvironment.FullPathOfDynTestFile(eppeo, "SelectedSheet.Grunddaten.0.xlsx"), ExcelDataOperationsBase.SaveOptionsForDisabledCalculationEngines.NoReset)
640+
System.Console.WriteLine("OUT: " & eppeo.FilePath)
641+
Assert.That(eppeo.SelectedSheetName, [Is].EqualTo("Grunddaten"))
642+
643+
eppeo = Me.CreateInstance(TestFileName, ExcelDataOperationsBase.OpenMode.OpenExistingFile, True, Nothing)
644+
eppeo.SelectSheet(1)
645+
eppeo.SaveAs(TestEnvironment.FullPathOfDynTestFile(eppeo, "SelectedSheet.Ausgewählt.1.xlsx"), ExcelDataOperationsBase.SaveOptionsForDisabledCalculationEngines.NoReset)
646+
System.Console.WriteLine("OUT: " & eppeo.FilePath)
647+
Assert.That(eppeo.SelectedSheetName, [Is].EqualTo("Ausgewählt"))
648+
649+
eppeo = Me.CreateInstance(TestFileName, ExcelDataOperationsBase.OpenMode.OpenExistingFile, True, Nothing)
650+
eppeo.SelectSheet(2)
651+
eppeo.SaveAs(TestEnvironment.FullPathOfDynTestFile(eppeo, "SelectedSheet.Kostenplanung.2.xlsx"), ExcelDataOperationsBase.SaveOptionsForDisabledCalculationEngines.NoReset)
652+
System.Console.WriteLine("OUT: " & eppeo.FilePath)
653+
Assert.That(eppeo.SelectedSheetName, [Is].EqualTo("Kostenplanung"))
654+
655+
eppeo.SelectSheet("Grunddaten")
538656
eppeo.SaveAs(TestEnvironment.FullPathOfDynTestFile(eppeo, "SelectedSheet.Grunddaten.xlsx"), ExcelDataOperationsBase.SaveOptionsForDisabledCalculationEngines.NoReset)
539657
System.Console.WriteLine("OUT: " & eppeo.FilePath)
540658
Assert.That(eppeo.SelectedSheetName, [Is].EqualTo("Grunddaten"))

ExcelOpsTest/ExcelOpsTests.Engines/MsExcelOpsTest.vb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ Namespace ExcelOpsTests.Engines
119119
MsExcelInstance.SetCultureContext(System.Threading.Thread.CurrentThread.CurrentCulture)
120120
End Sub
121121

122+
Protected Overrides Function _CreateInstance(data() As Byte, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.MsExcelDataOperations
123+
Throw New NotSupportedException
124+
End Function
125+
126+
Protected Overrides Function _CreateInstance(data As IO.Stream, passwordForOpening As String, disableCalculationEngine As Boolean) As ExcelOps.MsExcelDataOperations
127+
Throw New NotSupportedException
128+
End Function
122129
End Class
123130

124131
End Namespace

0 commit comments

Comments
 (0)