|
6 | 6 |
|
7 | 7 | static void CreateSpreadsheetWorkbook(string filepath) |
8 | 8 | { |
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(); |
16 | 15 |
|
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()); |
20 | 19 |
|
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()); |
23 | 22 |
|
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); |
27 | 26 |
|
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()); |
30 | 29 |
|
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); |
35 | 33 |
|
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; |
38 | 36 |
|
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>()) |
42 | 38 | { |
43 | | - refCell = cell; |
44 | | - break; |
| 39 | + if (string.Compare(cell.CellReference?.Value, "A1", true) > 0) |
| 40 | + { |
| 41 | + refCell = cell; |
| 42 | + break; |
| 43 | + } |
45 | 44 | } |
46 | | - } |
47 | 45 |
|
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); |
51 | 49 |
|
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); |
55 | 53 |
|
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 | + } |
58 | 58 | } |
0 commit comments