Adding parts to WorksheetPart derived class throws exception
See original GitHub issueDescription When I try to add parts to a worksheet part within a class derived from WorksheetPart, such as SpreadsheetPrinterSettingsPart or VmlDrawingPart, an OpenXmlPackageException is thrown with a message “The part cannot be added here.” This just started when I updated the reference SDK to 10.0 or 10.1. The code works as expected when built and run against SDK version 9.1 or earlier.
I’m still trying different things to understand this issue better but this is what I am observing in my code. However, if I open and existing workbook and simply add the part to a worksheet, it works fine. The main difference that I can tell is that the error happens in my code that inherits from WorksheetPart. Please let me know if there are any additional questions.
Information
- netstandard2.0
- DocumentFormat.OpenXml Version: 10.0+
Repro
// Please add a self-contained, minimum viable repro of the issue.
// If you require external resources, please provide a gist or GitHub repro
// An Xunit style test is preferred, but a console application would work too.
public class SampleWorksheetPart : WorksheetPart
{
public SampleWorksheetPart() {}
public void AddPrinterSettingsPart()
{
// The error would occur when this method is called in the line
// below in 10.0+ but works as expected
// in 9.1 or earlier.
var psPart = this.AddNewPart<SpreadsheetPrinterSettingsPart>();
}
}
class Program
{
static void Main(string[] args)
{
var fInfo = new FileInfo(@"C:\Temp\NewWorkbook.xlsx");
// This assumes that file that fInfo references already exists
using (var xlsx = fInfo.Open(FileMode.Open, FileAcess.ReadWrite))
{
using (var doc = SpreadsheetDocument.Open(xlsx, true))
{
var workbook = doc.WorkbookPart;
var worksheet = workbook.Parts.FirstOrDefault(w => w.OpenXmlPart is WorksheetPart).OpenXmlPart;
// This works fine in all versions for some reason.
var ps = worksheet.AddNewPart<SpreadsheetPrinterSettingsPart>();
}
}
}
}
Observed OpenXmlPackageException: The part cannot be added here –> Inner exception: ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Expected
The part is added to the current object.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top GitHub Comments
Sounds good. Again, thanks for taking the time to address.
We’re planning on releasing 2.11.0 soon - have to renew a cert for signing and then that’ll be released. So it’ll just go into that