使用Delphi

时间:2018-10-15 17:30:20

标签: delphi sharepoint ms-word

借助以下功能,我成功​​地从本地存储(通过OneDrive与Server同步)的Word文档中检索了其Server属性(存储为SharePoint列的属性),而所有这些都无需Ole自动化。函数的结构为:

  • 由于Word文档是压缩文件,因此请解压缩存储此类属性的文件。

  • 将文件的内容提取为字符串。

  • 将字符串加载到XML文档中。

  • 将字段名称及其内容输入到StringList中。

``

function WordGetServerProperties (FName:string):TStringList;
var
s,ss:string;
i,ii:integer;
St:TStringList;
XML:IXMLDocument;
N,NN: IXMLNode;
begin

s:=ExtractZipToStr(FName,'customXml/item1.xml',ExtractFilePath(FName));

if StrContains('<p:properties',s)=False then
 s:=ExtractZipToStr(FName,'customXml/item2.xml',ExtractFilePath(FName));
if StrContains('<p:properties',s)=False then
 s:=ExtractZipToStr(FName,'customXml/item3.xml',ExtractFilePath(FName));

XML:=NewXMLDocument;
St:=TStringList.Create;
XML.Active := True;
XML.LoadFromXML(s);
  N:=xml.DocumentElement;
  try
  for i := 0 to N.ChildNodes.Count -1 do
  begin
    if N.ChildNodes[i].NodeName = 'documentManagement' then
    begin
    NN:=N.ChildNodes[i];
    for ii := 0 to NN.ChildNodes.Count -1 do
     begin
     ss:=AnsiReplaceStr(NN.ChildNodes[ii].NodeName,'_x0020_',' ');
     if ss='SharedWithUsers' then continue;
     ss:=ss+'='+NN.ChildNodes[ii].Text;
     st.Add(ss)
     end;
    end;
  end;
   finally
    XML.Active := False;
   end;
Result:=st;
end;


function ExtractZipToStr(const ZipFileName: string; const ZippedFileName, ExtractedFileName: string): widestring;
var
  ZipFile: TZipFile;
  F,s:string;
  i:integer;
  Exists:Boolean;
  LStream: TStream;
  FStream:TFileStream;
  LocalHeader: TZipHeader;
begin
  Exists:=False;
  ZipFile := TZipFile.Create;
  LStream := TStream.Create;
  try
  try
   ZipFile.Open(ZipFileName,zmRead);
  except on EZipException do begin Result:='noprops'; ZipFile.Close; ZipFile.Free;  LStream.Free; exit; end; end;
  for i := 0 to ZipFile.FileCount - 1 do
          begin
            F:= ZipFile.FileNames[i];
            if F='docProps/custom.xml' then begin Exists:=True; system.Break; end;
          end;
  if exists=True then
    begin
    ZipFile.Read(ZippedFileName, LStream, LocalHeader);
    LStream.Position:=0;
    Result:=StreamToString(LStream);
    end
  else Result:='noprops';
  finally
  ZipFile.Close;
  ZipFile.Free;
  LStream.Free;
  end;
end;

function StreamToString(aStream: TStream): widestring;
var
  SS: TStringStream;
begin
  if aStream <> nil then
  begin
    SS := TStringStream.Create('');
    try
      SS.CopyFrom(aStream, 0);
      Result := SS.DataString;
    finally
      SS.Free;
    end;
  end else
  begin
    Result := '';
  end;
end;

这是相对较快的,但没有我想要的那么快。希望我已经证明(对此是业余的)我精疲力尽。您会发现任何方法可以通过更有效的方法来改进或完全取代这些例程吗?

0 个答案:

没有答案