日期:2014-05-17  浏览次数:20882 次

C#如何把word中的文字(word里面没有图片)显示在窗体的文本框中
如题,求详细代码

------解决方案--------------------
在Form里加一个button按钮和一个richtextbox,给按钮加一个Click事件

C# code

private void button1_Click(object sender, EventArgs e)
{
         object filepath= "D:\\Debugtest.doc";
         openWord(filepath);
}

//把Word文档内容取出来放到richtextBox里
private void openWord(object SPath)
{
            object file = SPath;
            object nullobj = System.Reflection.Missing.Value;
            Word.Document doc = myWordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,ref nullobj,ref nullobj,ref nullobj,ref nullobj);
            doc.ActiveWindow.Selection.WholeStory();
            doc.ActiveWindow.Selection.Copy();
            IDataObject data = Clipboard.GetDataObject();
            this.richTextBox1.Text = data.GetData(DataFormats.Text).ToString();
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
 }

------解决方案--------------------
C# code

using Word = Microsoft.Office.Interop.Word;

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

        private void button1_Click(object sender, EventArgs e)
        {
            Word.ApplicationClass wordApp = new Word.ApplicationClass();

            object file = @"C:\Users\aaa\Desktop\生产经营管理系统需求.docx";
            object nullobj = System.Reflection.Missing.Value;

            Word.Document doc = wordApp.Documents.Open(
                ref file, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj);

            doc.ActiveWindow.Selection.WholeStory();
            doc.ActiveWindow.Selection.Copy();
            IDataObject data = Clipboard.GetDataObject();
            //读取word中的文本
            string mytext = data.GetData(DataFormats.Text).ToString();
            doc.Close();
        }
    }
}