2017-10-12 63 views
0

使用Open Xml SDK,我在WorksheetPart中添加了一个DrawingsPart,后来我尝试引用WorksheetPart内的DrawingsPart,但我收到了ArgumentOutOfRangeException图纸部分未被添加到WorksheetPart。指定参数超出有效值范围

下面是代码中的相关片段:

// Add a new drawings part to the worksheet 
var drawingsPart = worksheetPart.AddNewPart<DrawingsPart>(); 

// make a drawing DOM 
var drawingRootElement = new WorksheetDrawing(); 

// add to the drawing DOM 
... 

// and then... 
// associate the drawing DOM to the drawings part 
drawingsPart.WorksheetDrawing = drawingRootElement; 

// save the drawing DOM back to the drawings part 
drawingsPart.WorksheetDrawing.Save(); 

// and finally... 
// here is where it throws the ArgumentOutOfRangeException 
// whether I supply the drawingsPart as the argument 
// or the value worksheet.DrawingsPart 
// it reports the same exception 
// I looked up the source of OpenXmlPartContainer.GetIdOfPart 
// and it looks like the DrawingsPart is not yet added to the 
// PartDictionary of the WorksheetPart. I wonder why? 
var relationshipIdOfDrawingsPart = drawingsPart 
          .GetIdOfPart(worksheetPart.DrawingsPart /* drawingsPart */); 


// Create a new drawing element and add it to the Worksheet DOM 
var drawingElement = new DocumentFormat.OpenXml.Spreadsheet.Drawing { Id = relationshipIdOfDrawingsPart }; 
worksheetPart.Worksheet.Append(drawingElement); 

异常详细信息:

System.ArgumentOutOfRangeException发生的HResult = 0x80131502
消息=指定参数超出有效范围的值。
源= DocumentFormat.OpenXml堆栈跟踪:在在 DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.GetIdOfPart(OpenXmlPart 部分)......我的代码

我抬头的OpenXmlPartContainer.GetIdOfPart源(转载如下):

// DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer 
/// <summary> 
/// Gets the relationship ID of the part. 
/// </summary> 
/// <param name="part">The part.</param> 
/// <returns>The relationship ID of the part.</returns> 
/// <exception cref="T:System.ArgumentNullException">Thrown when "part" is null reference.</exception> 
/// <exception cref="T:System.ArgumentOutOfRangeException">Thrown when the part does not exist.</exception> 
public string GetIdOfPart(OpenXmlPart part) 
{ 
    this.ThrowIfObjectDisposed(); 
    if (part == null) 
    { 
     throw new ArgumentNullException("part"); 
    } 
    if (this.PartDictionary.ContainsValue(part)) 
    { 
     foreach (KeyValuePair<string, OpenXmlPart> current in this.PartDictionary) 
     { 
      if (part == current.Value) 
      { 
       return current.Key; 
      } 
     } 
    } 
    throw new ArgumentOutOfRangeException("part"); 
} 

它看起来像DrawingsPart没有被添加到WorksheetPartPartDictionary。我想知道为什么?

+0

@mjwills我相信'worksheetPart.DrawingsPart'由代码的第一行填充,即调用'worksheetPart.AddNewPart ()'?我解压缩了我的程序生成的输出文件,图纸部分在'sheet1.xml.rels'文件中。 –

+0

@mjwills'drawingsPart'的数量在上面代码的第三行代码中描述:'drawingsPart.WorksheetDrawing = drawingRootElement;' –

+0

@mjwills它在同一行代码中被声明和分配。 –

回答

1

鉴于worksheetPart.DrawingsPartdrawingsPart是同一个对象,这个代码是没有意义的:

// and finally... 
// here is where it throws the ArgumentOutOfRangeException 
// whether I supply the drawingsPart as the argument 
// or the value worksheet.DrawingsPart 
// it reports the same exception 
// I looked up the source of OpenXmlPartContainer.GetIdOfPart 
// and it looks like the DrawingsPart is not yet added to the 
// PartDictionary of the WorksheetPart. I wonder why? 
var relationshipIdOfDrawingsPart = drawingsPart 
          .GetIdOfPart(worksheetPart.DrawingsPart /* drawingsPart */); 

因为如果该对象的PartDictionary载到本身参考,只会工作。

因此,您需要改为在父对象上调用GetIdOfPart,而不是drawingsPart本身。

相关问题