Discuss this help topic in SecureBlackbox Forum

Read or write package part data

To read and write data from/to a package part you need to

  1. get the part object for the needed part
  2. get the stream object using TElOfficePackagePart.GetStream() method
  3. use the stream
  4. close the stream using TElOfficePackagePart.CloseStream() method

C#:


TElOfficePackagePart Part = Document.OpenXMLDocument.OfficeDocumentPart;
if (Part==null)
    return;

Stream Stream = Part.GetStream(false);
try
{
    Stream.Position = 0;
    using (TElXMLDOMDocument Doc = new TElXMLDOMDocument())
    {
        Doc.LoadFromStream(Stream);
        // process the xml document
        ...
        // save the xml document
        Stream.Position = 0;
        Doc.SaveToStream(Stream);
        Stream.SetLength(Stream.Position);
    }
}
finally
{
    Part.CloseStream();
}
Delphi:

var
  Part : TElOfficePackagePart;
  Stream : TElStream;
  Doc : TElXMLDOMDocument;
begin
  Part := Document.OpenXMLDocument.OfficeDocumentPart;
  if not Assigned(Part) then
    Exit;

  Stream := Part.GetStream(False);
  try
    Stream.Position := 0;
    Doc := TElXMLDOMDocument.Create;
    try
      Doc.LoadFromStream(Stream);
      // process the xml document
      ...
      // save the xml document
      Stream.Position := 0;
      Doc.SaveToStream(Stream);
      Stream.Size := Stream.Position;
    finally
      FreeAndNil(Doc);
    end;
  finally
    Part.CloseStream;
  end;
end;

How To articles about MS Office OpenXML documents

Discuss this help topic in SecureBlackbox Forum