Skip to content

Commit 7ddd960

Browse files
authored
Update Program.cs
Updated to correctly reflect the following: 1. Using block for unmanaged object that implements IDisposable 2. Removed explict call to Dispose() 3. Added explicit call to Save() methods of the worksheet and workbook
1 parent 45cd994 commit 7ddd960

File tree

1 file changed

+37
-37
lines changed
  • samples/spreadsheet/working_with_sheets/cs

1 file changed

+37
-37
lines changed

samples/spreadsheet/working_with_sheets/cs/Program.cs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,53 @@
66

77
static void CreateSpreadsheetWorkbook(string filepath)
88
{
9-
// Create a spreadsheet document by supplying the filepath.
10-
// By default, AutoSave = true, Editable = true, and Type = xlsx.
11-
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
12-
13-
// Add a WorkbookPart to the document.
14-
WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
15-
workbookPart.Workbook = new Workbook();
9+
// Use 'using' block to ensure proper disposal of the document
10+
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook))
11+
{
12+
// Add a WorkbookPart to the document.
13+
WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
14+
workbookPart.Workbook = new Workbook();
1615

17-
// Add a WorksheetPart to the WorkbookPart.
18-
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
19-
worksheetPart.Worksheet = new Worksheet(new SheetData());
16+
// Add a WorksheetPart to the WorkbookPart.
17+
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
18+
worksheetPart.Worksheet = new Worksheet(new SheetData());
2019

21-
// Add Sheets to the Workbook.
22-
Sheets sheets = workbookPart.Workbook.AppendChild<Sheets>(new Sheets());
20+
// Add Sheets to the Workbook.
21+
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
2322

24-
// Append a new worksheet and associate it with the workbook.
25-
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
26-
sheets.Append(sheet);
23+
// Append a new worksheet and associate it with the workbook.
24+
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
25+
sheets.Append(sheet);
2726

28-
// Get the sheetData cell table.
29-
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>() ?? worksheetPart.Worksheet.AppendChild(new SheetData());
27+
// Get the sheetData cell table.
28+
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>() ?? worksheetPart.Worksheet.AppendChild(new SheetData());
3029

31-
// Add a row to the cell table.
32-
Row row;
33-
row = new Row() { RowIndex = 1 };
34-
sheetData.Append(row);
30+
// Add a row to the cell table.
31+
Row row = new Row() { RowIndex = 1 };
32+
sheetData.Append(row);
3533

36-
// In the new row, find the column location to insert a cell in A1.
37-
Cell? refCell = null;
34+
// In the new row, find the column location to insert a cell in A1.
35+
Cell? refCell = null;
3836

39-
foreach (Cell cell in row.Elements<Cell>())
40-
{
41-
if (string.Compare(cell.CellReference?.Value, "A1", true) > 0)
37+
foreach (Cell cell in row.Elements<Cell>())
4238
{
43-
refCell = cell;
44-
break;
39+
if (string.Compare(cell.CellReference?.Value, "A1", true) > 0)
40+
{
41+
refCell = cell;
42+
break;
43+
}
4544
}
46-
}
4745

48-
// Add the cell to the cell table at A1.
49-
Cell newCell = new Cell() { CellReference = "A1" };
50-
row.InsertBefore(newCell, refCell);
46+
// Add the cell to the cell table at A1.
47+
Cell newCell = new Cell() { CellReference = "A1" };
48+
row.InsertBefore(newCell, refCell);
5149

52-
// Set the cell value to be a numeric value of 100.
53-
newCell.CellValue = new CellValue("100");
54-
newCell.DataType = new EnumValue<CellValues>(CellValues.Number);
50+
// Set the cell value to be a numeric value of 100.
51+
newCell.CellValue = new CellValue("100");
52+
newCell.DataType = new EnumValue<CellValues>(CellValues.Number);
5553

56-
// Dispose the document.
57-
spreadsheetDocument.Dispose();
54+
// Explicitly save the worksheet part and workbook part
55+
worksheetPart.Worksheet.Save();
56+
workbookPart.Workbook.Save();
57+
}
5858
}

0 commit comments

Comments
 (0)