forked from OfficeDev/open-xml-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.vb
115 lines (96 loc) · 5.07 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
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
' <Snippet0>
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Module MyModule
Sub Main(args As String())
DeleteTextFromCell(args(0), args(1), args(2), UInteger.Parse(args(3)))
End Sub
' <Snippet1>
' Given a document, a worksheet name, a column name, and a one-based row index,
' deletes the text from the cell at the specified column and row on the specified sheet.
Public Sub DeleteTextFromCell(ByVal docName As String, ByVal sheetName As String, ByVal colName As String, ByVal rowIndex As UInteger)
' Open the document for editing.
Dim document As SpreadsheetDocument = SpreadsheetDocument.Open(docName, True)
Using (document)
Dim sheets As IEnumerable(Of Sheet) = document.WorkbookPart.Workbook.GetFirstChild(Of Sheets)().Elements(Of Sheet)().Where(Function(s) s.Name = sheetName.ToString())
If (sheets.Count = 0) Then
' The specified worksheet does not exist.
Return
End If
Dim relationshipId As String = sheets.First.Id.Value
Dim worksheetPart As WorksheetPart = CType(document.WorkbookPart.GetPartById(relationshipId), WorksheetPart)
' Get the cell at the specified column and row.
Dim cell As Cell = GetSpreadsheetCell(worksheetPart.Worksheet, colName, rowIndex)
If (cell Is Nothing) Then
' The specified cell does not exist.
Return
End If
cell.Remove()
worksheetPart.Worksheet.Save()
End Using
End Sub
' </Snippet1>
' <Snippet2>
' Given a worksheet, a column name, and a row index, gets the cell at the specified column and row.
Private Function GetSpreadsheetCell(ByVal worksheet As Worksheet, ByVal columnName As String, ByVal rowIndex As UInteger) As Cell
Dim rows As IEnumerable(Of Row) = worksheet.GetFirstChild(Of SheetData)().Elements(Of Row)().Where(Function(r) r.RowIndex = rowIndex.ToString())
If (rows.Count = 0) Then
' A cell does not exist at the specified row.
Return Nothing
End If
Dim cells As IEnumerable(Of Cell) = rows.First().Elements(Of Cell)().Where(Function(c) String.Compare(c.CellReference.Value, columnName + rowIndex.ToString(), True) = 0)
If (cells.Count = 0) Then
' A cell does not exist at the specified column, in the specified row.
Return Nothing
End If
Return cells.First
End Function
' </Snippet2>
' <Snippet3>
' Given a shared string ID and a SpreadsheetDocument, verifies that other cells in the document no longer
' reference the specified SharedStringItem and removes the item.
Private Sub RemoveSharedStringItem(ByVal shareStringId As Integer, ByVal document As SpreadsheetDocument)
Dim remove As Boolean = True
For Each part In document.WorkbookPart.GetPartsOfType(Of WorksheetPart)()
Dim worksheet As Worksheet = part.Worksheet
For Each cell In worksheet.GetFirstChild(Of SheetData)().Descendants(Of Cell)()
' Verify if other cells in the document reference the item.
If cell.DataType IsNot Nothing AndAlso cell.DataType.Value = CellValues.SharedString AndAlso cell.CellValue.Text = shareStringId.ToString() Then
' Other cells in the document still reference the item. Do not remove the item.
remove = False
Exit For
End If
Next
If Not remove Then
Exit For
End If
Next
' Other cells in the document do not reference the item. Remove the item.
If remove Then
Dim shareStringTablePart As SharedStringTablePart = document.WorkbookPart.SharedStringTablePart
If shareStringTablePart Is Nothing Then
Exit Sub
End If
Dim item As SharedStringItem = shareStringTablePart.SharedStringTable.Elements(Of SharedStringItem)().ElementAt(shareStringId)
If item IsNot Nothing Then
item.Remove()
' Refresh all the shared string references.
For Each part In document.WorkbookPart.GetPartsOfType(Of WorksheetPart)()
Dim worksheet As Worksheet = part.Worksheet
For Each cell In worksheet.GetFirstChild(Of SheetData)().Descendants(Of Cell)()
If cell.DataType IsNot Nothing AndAlso cell.DataType.Value = CellValues.SharedString Then
Dim itemIndex As Integer = Integer.Parse(cell.CellValue.Text)
If itemIndex > shareStringId Then
cell.CellValue.Text = (itemIndex - 1).ToString()
End If
End If
Next
worksheet.Save()
Next
document.WorkbookPart.SharedStringTablePart.SharedStringTable.Save()
End If
End If
End Sub
' </Snippet3>
End Module
' </Snippet0>