2016-07-13 11 views
1

c#を使用して単語ファイルにテキストを書き込んで追加しようとしましたが、期待した結果が得られません。これで私を助けてくれますか?以下はC#、.NETを使用して単語ファイルにテキストを書き込む方法

上記のコードは、背後にあるWindowsフォーム・アプリケーション・コードである私のコード -

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace WFA1 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      //FileStream F = new FileStream("testdoc2.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite); 
      Console.WriteLine("Sourav");   
      string filename = @"C:\\Desktop\\myfile.docx"; 
      Console.WriteLine(filename); 
      try 
      { 
       using (FileStream fs = File.OpenWrite(filename)) 
       { 
        Byte[] content = new UTF8Encoding(true).GetBytes("Hello I am learning C#"); 
        fs.Write(content, 0, content.Length); 
       } 
      } 
      catch (Exception Ex) 
      { 
       Console.Write(e.ToString()); 
      } 



     } 
    } 
} 

です。私はFileStreamクラスを使ってデータを書きました。しかし、私は問題の下に直面しています: -

  1. んが、ファイルが作成されません取得され
  2. コードは、私はそれを手動で停止するまで実行し続けています。

私は以下のアプローチも試みましたが、ファイルにテキストを書き込めました。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
using Microsoft.Office.Interop.Word; 

namespace WFA1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); 
      Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\\Users\\SS5014874\\Desktop\\testdoc1.docx"); 
      object missing = System.Reflection.Missing.Value; 
      //string s = "Hi"; 
      //Console.WriteLine(s);    
      doc.Content.Text = textBox1.Text.ToString(); 
      doc.Save(); 
      doc.Close(ref missing); 

      app.Quit(ref missing); 
     } 
    } 
} 

しかし、まだ期待した結果が得られませんでした。以下は私の問題です: -

  1. テキストを追加できません。このアプローチを使用して追加する方法を教えてください。テキストを追加するために呼び出すことができるメソッドはありますか?
  2. Quitメソッドを使用していても、手動で終了するまで、アプリケーションは終了しません。私はクラスのメソッドのリストを見つけることができます。また

Microsoft.Office.Interop.Word

は私が他の情報についてお知らせください。

+1

http://stackoverflow.com/questions/28761877/how-to-insert-text-from-c-sharp-application-into-ms - 私はあなたのコードのようにこれらのコードを維持しようとしています-word-document-and-save-it-as- – MethodMan

+0

またはこれを使用してドキュメントを作成し、 'ActiveDocument.Save'に注意してください。http://stackoverflow.com/questions/32693480/how-to-insert-text-あなたがそれらを好きでない場合は、ドキュメントの終わりに - ..単純なGoogle検索をそこにたくさんの例がたくさんあります。 – MethodMan

+0

@MethodManとupvoter:その投稿(最初のコメントではなく、2番目ではない)の受け入れられた解決策は完全に間違っています。ポスターはドキュメントを上書きしてテキストファイルに置き換えますが、テキストファイルに '.docx'拡張子まるでそれがそうでない単語文書であるかのように。しかし、誰かが助けてくれるリンクを少なくとも投稿していました。[How to:Word文書にプログラムでテキストを挿入する方法(https://msdn.microsoft.com/en-us/library/6b9478cs.aspx) – Quantic

答えて

1

このリンクに従ってくださいhttps://support.microsoft.com/en-us/kb/316384

それとも

あなたはこれを試すことができます。

は、以下のディレクティブを追加:

  • Microsoft.Office.Interop.Wordを使用します。

  • using System.Reflection;

Microsoft.Office.Interop.Word;

  • [プロジェクト]メニューの[参照の追加]をクリックします。
  • [COM]タブで、Microsoft Word Object Libraryを見つけて[選択]をクリックします。

(私の場合はそれがのMicrosoft Word 12.0 Object Libraryのです)

は、これらのコードを使用してください。

private void button1_Click(object sender, EventArgs e) 
{ 
    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();  
    Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(@"e:\testdoc1.docx"); 
    object missing = System.Reflection.Missing.Value;       
    doc.Content.Text += textBox1.Text; 
    app.Visible = true; //Optional 
    doc.Save();    
    this.Close();   
} 
関連する問題