-
Notifications
You must be signed in to change notification settings - Fork 63
/
Program.vb
55 lines (41 loc) · 2.04 KB
/
Program.vb
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
' <Snippet0>
Imports System.IO
Imports System.IO.Packaging
Imports DocumentFormat.OpenXml.Packaging
Module Program
Sub Main(args As String())
End Sub
Public Sub OpenSpreadsheetDocumentReadOnly(ByVal filePath As String)
' <Snippet1>
' Open a SpreadsheetDocument based on a file path.
Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Open(filePath, False)
' </Snippet1>
' Attempt to add a new WorksheetPart.
' The call to AddNewPart generates an exception because the file is read-only.
Dim newWorksheetPart As WorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)()
' The rest of the code will not be called.
End Using
' <Snippet2>
' Open a SpreadsheetDocument based on a stream.
Dim stream = File.Open(filePath, FileMode.Open)
Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Open(filePath, False)
' </Snippet2>
' Attempt to add a new WorksheetPart.
' The call to AddNewPart generates an exception because the file is read-only.
Dim newWorksheetPart As WorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)()
' The rest of the code will not be called.
End Using
' <Snippet3>
' Open System.IO.Packaging.Package.
Dim spreadsheetPackage As Package = Package.Open(filePath, FileMode.Open, FileAccess.Read)
' Open a SpreadsheetDocument based on a package.
Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Open(spreadsheetPackage)
' </Snippet3>
' Attempt to add a new WorksheetPart.
' The call to AddNewPart generates an exception because the file is read-only.
Dim newWorksheetPart As WorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)()
' The rest of the code will not be called.
End Using
End Sub
End Module
' </Snippet0>