dotnet / Open-XML-SDK

Open XML SDK by Microsoft
https://www.nuget.org/packages/DocumentFormat.OpenXml/
MIT License
4.02k stars 546 forks source link

Bug with Open XML SDK v3.0.2 - Exception in AddWorkbookPart Method #1745

Open bhargavgaglani07 opened 3 months ago

bhargavgaglani07 commented 3 months ago

Describe the bug With v3.0.2 AddWorkbookPart method is throwing exception - The specified package is invalid. The main part is missing.

Refer attached file (EmptyExcel.xlsx) to be used with below sample code.

using (SpreadsheetDocument document = SpreadsheetDocument.Open($"EmptyExcel.xlsx", true))
 {
     if (document.WorkbookPart != null)
     {
         document.DeletePart(document.WorkbookPart);
     }

     /*** below line throws error ***/
     var workbookPart = document.AddWorkbookPart();
     workbookPart.Workbook.Save();
 }

Observed behavior Exception - The specified package is invalid. The main part is missing. Kindly note that same is working with v2.20.0.

Expected behavior It should add workbook part instead of throwing exception.

Desktop (please complete the following information):

Debug Info Adding below line post delete part call resolves issue

document.ChangeDocumentType(SpreadsheetDocumentType.Workbook);