@@ -8,51 +8,52 @@ Module MyModule
88 End Sub
99
1010 Public Sub CreateSpreadsheetWorkbookWithNumValue( ByVal filepath As String )
11- ' Create a spreadsheet document by supplying the filepath.
12- ' By default, AutoSave = true, Editable = true, and Type = xlsx.
13- Dim spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)
14-
15- ' Add a WorkbookPart to the document.
16- Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart()
17- workbookpart.Workbook = New Workbook()
18-
19- ' Add a WorksheetPart to the WorkbookPart.
20- Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart( Of WorksheetPart)()
21- worksheetPart.Worksheet = New Worksheet( New SheetData())
22-
23- ' Add Sheets to the Workbook.
24- Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild( Of Sheets)( New Sheets())
25-
26- ' Append a new worksheet and associate it with the workbook.
27- Dim sheet As New Sheet() With {.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), .SheetId = 1 , .Name = "mySheet" }
28- sheets.Append(sheet)
29-
30- ' Get the sheetData cell table.
31- Dim sheetData As SheetData = worksheetPart.Worksheet.GetFirstChild( Of SheetData)()
32-
33- ' Add a row to the cell table.
34- Dim row As Row
35- row = New Row() With {.RowIndex = 1 }
36- sheetData.Append(row)
37-
38- ' In the new row, find the column location to insert a cell in A1.
39- Dim refCell As Cell = Nothing
40- For Each cell As Cell In row.Elements( Of Cell)()
41- If String .Compare(cell.CellReference.Value, "A1" , True ) > 0 Then
42- refCell = cell
43- Exit For
44- End If
45- Next
46-
47- ' Add the cell to the cell table at A1.
48- Dim newCell As New Cell() With {.CellReference = "A1" }
49- row.InsertBefore(newCell, refCell)
50-
51- ' Set the cell value to be a numeric value of 100.
52- newCell.CellValue = New CellValue( "100" )
53- newCell.DataType = New EnumValue( Of CellValues)(CellValues.Number)
54-
55- ' Dispose the document.
56- spreadsheetDocument.Dispose()
11+ ' Use 'Using' block to ensure proper disposal of the document
12+ Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)
13+
14+ ' Add a WorkbookPart to the document.
15+ Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart()
16+ workbookpart.Workbook = New Workbook()
17+
18+ ' Add a WorksheetPart to the WorkbookPart.
19+ Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart( Of WorksheetPart)()
20+ worksheetPart.Worksheet = New Worksheet( New SheetData())
21+
22+ ' Add Sheets to the Workbook.
23+ Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild( Of Sheets)( New Sheets())
24+
25+ ' Append a new worksheet and associate it with the workbook.
26+ Dim sheet As New Sheet() With {.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), .SheetId = 1 , .Name = "mySheet" }
27+ sheets.Append(sheet)
28+
29+ ' Get the sheetData cell table.
30+ Dim sheetData As SheetData = worksheetPart.Worksheet.GetFirstChild( Of SheetData)()
31+
32+ ' Add a row to the cell table.
33+ Dim row As New Row() With {.RowIndex = 1 }
34+ sheetData.Append(row)
35+
36+ ' In the new row, find the column location to insert a cell in A1.
37+ Dim refCell As Cell = Nothing
38+ For Each cell As Cell In row.Elements( Of Cell)()
39+ If String .Compare(cell.CellReference.Value, "A1" , True ) > 0 Then
40+ refCell = cell
41+ Exit For
42+ End If
43+ Next
44+
45+ ' Add the cell to the cell table at A1.
46+ Dim newCell As New Cell() With {.CellReference = "A1" }
47+ row.InsertBefore(newCell, refCell)
48+
49+ ' Set the cell value to be a numeric value of 100.
50+ newCell.CellValue = New CellValue( "100" )
51+ newCell.DataType = New EnumValue( Of CellValues)(CellValues.Number)
52+
53+ ' Explicitly save the worksheet part and workbook part
54+ worksheetPart.Worksheet.Save()
55+ workbookpart.Workbook.Save()
56+
57+ End Using ' This will automatically dispose of the spreadsheetDocument
5758 End Sub
58- End Module
59+ End Module
0 commit comments