2015-07-15 16 views
7

Mam DateTable. Trzeba wstawić go zamiast jakiegoś zestawu znaków w dokumencie teplate.Jak wstawić tabelę do dokumentu programu Word zamiast znacznika tekstowego?

mogę zamienić tekst na tekst w ten sposób (dużo missingObj aby uniknąć błędów):

using Word = Microsoft.Office.Interop.Word; 

    Word._Application application; 
    Word._Document document; 
    Object missingObj = System.Reflection.Missing.Value; 
    Object trueObj = true; 
    Object falseObj = false; 

    private void create_button1_Click(object sender, EventArgs e) { 
      application = new Word.Application(); 
      Object templatePathObj; 
      templatePathObj = "template.dot"; 

      try { 
       document = application.Documents.Add(ref templatePathObj, 
        ref missingObj, ref missingObj, ref missingObj); 
      } 
      catch (Exception error) { 
       document.Close(ref falseObj, ref missingObj, ref missingObj); 
       application.Quit(ref missingObj, ref missingObj, ref missingObj); 
       document = null; 
       application = null; 
       throw error; 
      } 

      object strToFindObj = "%%mark%%"; 
      object replaceStrObj = "text to replace"; 
      Word.Range wordRange; 
      object replaceTypeObj; 
      replaceTypeObj = Word.WdReplace.wdReplaceAll; 
      for (int i = 1; i <= document.Sections.Count; i++) { 
       wordRange = document.Sections[i].Range; 
       Word.Find wordFindObj = wordRange.Find; 
       object[] wordFindParameters = new object[15] { strToFindObj, missingObj, 
        missingObj, missingObj, missingObj, missingObj, missingObj, missingObj, 
        missingObj, replaceStrObj, replaceTypeObj, missingObj, missingObj, 
        missingObj, missingObj }; 
       wordFindObj.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, 
        null, wordFindObj, wordFindParameters); 
     } 
     application.Visible = true; 
    } 

Co muszę zmienić, więc ten kod odbędzie DataTable zamiast strToFindObj?

W tym przykładzie mogę wymienić rzeczy w Range, co jest pewne fragmen dokumentu w tym tabele, format, itp

Odpowiedz

5

mnóstwem missingObj aby uniknąć Błędy

docelowa .Net 4.0 lub powyżej obsługuje nazwane i opcjonalne argumenty z wywołaniami COM, więc nie będziesz musiał uwzględniać wszystkich ref missingObj. Zobacz artykuł MSDN: Named and Optional Arguments - Ta funkcja znacznie ułatwia wywoływanie interfejsów COM, takich jak interfejsy API Microsoft Office Automation.


Co muszę zmienić, więc mogę wyszukiwać za pomocą DataTable zamiast sznurka zmiennej strToFindObj?

Trzeba by iterację DataTables Row i komórki zastępując DataTable komórek w dokumencie programu Word, np:

foreach(var dr in dt.Rows) 
{ 
    foreach (var cell in dr.ItemArray) 
    { 
    string strToFind = cell.ToString(); 
    string replaceStr = "replace old value"; 
    ReplaceTextInWord(@"C:\Temp\template.docx", strToFind, replaceStr); 
    } 
} 

Jeśli okaże użyciu DataTable zbyt twarde i chcą listę (jak Słownik) to jeszcze łatwiej:

var listOfTextToReplace = new Dictionary<string,string>(); 
listOfTextToReplace.Add("%%mark%%","text to replace"); 
foreach(var item in listOfTextToReplace) 
{ 
    string strToFind = item.Key; 
    string replaceStr = item.Value; 
    ReplaceTextInWord(@"C:\Temp\template.docx", strToFind, replaceStr); 
} 

Oto ReplaceTextInWord Metoda:

using Word = Microsoft.Office.Interop.Word; 
Word._Application application; 
Word._Document document; 
Object missingObj = System.Reflection.Missing.Value; 
Object trueObj = true; 
Object falseObj = false; 

private void create_button1_Click(object sender, EventArgs e) { 
    //ReplaceTextInWord("template.dot", "find me", "Found"); <-- Are you sure you want to replace in a Template? 
    ReplaceTextInWord(@"C:\Temp\template.docx", "%%mark%%","text to replace"); //I think you want a .DOC or DOCX file 
} 

private void ReplaceTextInWord(string wordDocFilePath, string strToFind, string replaceStr) { 
    application = new Word.Application(); 
    try { 
     //document = application.Documents.Add(ref templatePathObj, ref missingObj, ref missingObj, ref missingObj); 
     document = application.Documents.Open(wordDocFilePath); //You need to open Word Documents, not add them, as per https://msdn.microsoft.com/en-us/library/tcyt0y1f.aspx 
    } 
    catch (Exception error) { 
     document.Close(ref falseObj, ref missingObj, ref missingObj); 
     application.Quit(ref missingObj, ref missingObj, ref missingObj); 
     document = null; 
     application = null; 
     throw error; 
    } 

    for (int i = 1; i <= document.Sections.Count; i++) { 
    Word.Range wordRange = document.Sections[i].Range; 
    Word.Find findObject = wordRange.Find; 
    findObject.ClearFormatting(); 
    findObject.Text = strToFind; 
    findObject.Replacement.ClearFormatting(); 
    findObject.Replacement.Text = replaceStr; 

    object replaceAll = Word.WdReplace.wdReplaceAll; 
    findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, 
     ref missing, ref missing, ref missing, ref missing, ref missing, 
     ref replaceAll, ref missing, ref missing, ref missing, ref missing); 
    } 
    application.Visible = true; 
} 

Nr MSDN: How to: Programmatically Search for and Replace Text in Documents

Powiązane problemy