如何在Word文档中显示ICollection?

时间:2019-05-22 15:23:34

标签: c# .net ms-word office-interop

我想使用Microsoft.Office.Interop.Word在Word文档中显示i集合。 到目前为止,我设法编写了日期和状态,但是当我编写ICollection DemandeModuleList时,总是会找到相同的名称。如果您能帮助我使其动态化,我会很高兴。我可以在文档和代码中添加些什么来解决此问题,谢谢

  public void CreateWord(int id)
        {
            CreateWordDocument(@"c:\devis.docx", @"c:\devisdone.docx",id);
        }

public void CreateWordDocument(object filename ,object SaveAs , int id)
        {
            Demande demande = _context.Demandes.Where(a => a.IdDemande == id)
                      .Include(a => a.DemandeModuleList)
                      .ThenInclude(a => a.Module)
                      .ToList().First();

            Word.Application WordApp = new Word.Application();
            object missing = Missing.Value;
            Word.Document myWordDoc = null;

            object readOnly = false;
            object isVisible = false;
            WordApp.Visible = false;
            myWordDoc = WordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing
                );
      this.FindAndReplace(WordApp, "<<DateDemande>>", demande.DateDemande);
         this.FindAndReplace(WordApp, "<<LibelleStatut>>", demande.Statut.LibelleStatut);
//THE PROBLEME IS HERE !
            foreach (var item in demande.DemandeModuleList)
            {
              this.FindAndReplace(WordApp, "<<IntituleModule>>", item.Module.IntituleModule);
            }


            myWordDoc.SaveAs2(ref SaveAs, ref missing, ref missing, ref missing
                , ref missing, ref missing, ref missing
                , ref missing, ref missing, ref missing
                , ref missing, ref missing, ref missing
                , ref missing, ref missing, ref missing
                );
            myWordDoc.Close();
            WordApp.Quit();
        }


public void FindAndReplace(Word.Application wordApp,Object ToFindText,Object replaceWithText)
        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object nmatchAllForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiactitics = false;
            object matchAlefhamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            wordApp.Selection.Find.Execute(ref ToFindText,
                ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundLike
                , nmatchAllForms, ref forward,
                ref wrap, ref format, ref replaceWithText,
                ref replace, ref matchKashida,
                ref matchDiactitics, ref matchAlefhamza,
                ref matchControl);


        }

//here the word doc devis.docx
Date  :  <<DateDemande>>
Statut :  <<LibelleStatut>> 
Demand's module : <<IntituleModule0>>
                  <<IntituleModule1>>
                  <<IntituleModule2>>
                 .............

0 个答案:

没有答案