2017-11-17 10 views
0

これはローカルパスにコピーする古いコードですが、今はSharePointにファイルを保存する必要があります。どのように私はそれをストリームを使用してストリームをファイルに書き込むことができます。MergeFieldsを使用してストリームでワード文書を操作する

File.Copy(oTemplatePath、destinationPath、true);

 using (WordprocessingDocument document = WordprocessingDocument.Open(destinationPath, true)) 
     { 




      document.GetMergeFields("reference_number").ReplaceWithText(refrenceNumber); 
      document.MainDocumentPart.Document.Save(); 


      for (int i = 0; i < newDoc.jsonFields.Count; i++) 
      { 
       if (newDoc.jsonFields[i].type == "date") 
       { 
        document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(DateTime.Parse(newDoc.jsonFields[i].data).ToShortDateString()); 
        document.MainDocumentPart.Document.Save(); 
       } 
       else 
       { 
        document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(newDoc.jsonFields[i].data); 
        document.MainDocumentPart.Document.Save(); 

       } 



      } 
      //document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(newDoc.jsonFields[i].data); 
      //document.MainDocumentPart.Document.Save(); 


     } 

答えて

0

は、SharePointにアップロードするとローカル一時フォルダ

string tempDocx = Path.Combine(Path.GetTempPath(), fileName); 
logger.Trace($"Creating Word document {tempDocx}"); 
File.Delete(tempDocx); 
File.Copy("Template.docx", tempDocx); 

に保存することができ、ここでええのFileStream

using (IO.FileStream fs = new IO.FileStream(tempDocx, IO.FileMode.Open)) 
{ 
    List documentsList = clientContext.Web.Lists.GetByTitle(libTitle); 
    clientContext.Load(documentsList.RootFolder); 
    clientContext.ExecuteQuery(); 

    var fileCreationInformation = new FileCreationInformation(); 

    //Assign to content byte[] i.e. documentStream 
    fileCreationInformation.ContentStream = fs; 

    //Allow owerwrite of document 
    fileCreationInformation.Overwrite = true; 

    //Upload URL 
    fileCreationInformation.Url = documentsList.RootFolder.ServerRelativeUrl + "/" + IO.Path.GetFileName(tempDocx); 

    uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation); 
    clientContext.Load(uploadFile); 

    clientContext.ExecuteQuery(); 
} 
+0

を使用して、私は一時フォルダに保存しなければなりませんでした。ありがとう –

関連する問題