2017-11-23 13 views
-1

私は、muiltilneオプションを有効にしたテキストボックスを持っています。 特定の単語ファイルの内容をそのテキストボックスに表示したいと思います。これどうやってするの?このコードを使用しますが、ファイルの名前を表示しているだけです。Wordファイルの内容をテキストボックスに表示

private void btnOpen_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
{ 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
    openFileDialog1.Title = "Open Word File"; 
    openFileDialog1.Filter = "Word Files (*doc)|*docx"; 

    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 
     Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); 

     object fileName = openFileDialog1.FileName; 
     // Define an object to pass to the API for missing parameters 
     object missing = System.Type.Missing; 
     doc = word.Documents.Open(ref fileName,ref missing, ref missing); 

     String read = string.Empty; 
     List<string> data = new List<string>(); 
     for (int i = 0; i < doc.Paragraphs.Count; i++) 
     { 
      string temp = doc.Paragraphs[i + 1].Range.Text.Trim(); 
      if (temp != string.Empty) 
      data.Add(temp); 
     } 
     doc.Close(); 
     word.Quit(); 
     txtTxt.Text = data.ToString(); 

    } 
} 

これはC#のWindowsフォームアプリケーションです。

助けてください!あなたは

txtTxt.Text = data.ToString(); 

dataラインを使用して、テキストボックスにデータを追加している

+0

- あなたはそれを開いたが、それはに見えましたそれでは。 – BugFinder

+0

@BugFinderどのようにして内容を読むことができますか?私のコードを編集できますか? – Uniquedesign

答えて

0

は、文字列のリストです。 .ToString()メソッドがオーバーロードされていないかぎり、このように割り当てることはできません。

リストを繰り返し、テキストボックスに追加します。このようなもの

foreach(var item in data) 
{ 
    txtTxt.Text += item; 
} 
+0

ありがとう、兄。私はこれを試してみましょう – Uniquedesign

0

解決策を見つける。 最後にループを追加するだけです。 "Mohamed Najiullah"に感謝します。

private void btnOpen_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
{ 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
    openFileDialog1.Title = "Open Word File"; 
    openFileDialog1.Filter = "Word Files (*doc)|*docx"; 

    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 
     Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); 

     object fileName = openFileDialog1.FileName; 
     // Define an object to pass to the API for missing parameters 
     object missing = System.Type.Missing; 
     doc = word.Documents.Open(ref fileName,ref missing, ref missing); 

     String read = string.Empty; 
     List<string> data = new List<string>(); 
     for (int i = 0; i < doc.Paragraphs.Count; i++) 
     { 
      string temp = doc.Paragraphs[i + 1].Range.Text.Trim(); 
      if (temp != string.Empty) 
      data.Add(temp); 
     } 
     doc.Close(); 
     word.Quit(); 
     foreach(var item in data) 
{ 
    txtTxt.Text += item; 
} 

    } 
} 
あなたはtxtTxt.Text = file`以前にファイルを設定する場所は、ファイル名であることを `と言ったので、それがどの時点で持っているあなたは、ファイルの内容を読み取る。..行います
関連する問題