閱讀679 返回首頁    go 阿裏雲 go 技術社區[雲棲]


打開word文檔到richtextbox

打開word文檔到richtextbox:

private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = "F:\\";
            ofd.Filter = "Word文檔(*.docx)|*.docx";
            ofd.ShowDialog();

            ApplicationClass app = new ApplicationClass();
            Document doc = null;
            object missing = System.Reflection.Missing.Value;

            object FileName = ofd.FileName;
            object readOnly = false;
            object isVisible = true;
            object index = 0;
            try
            {
                doc = app.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 isVisible, ref missing,
                    ref missing, ref missing, ref missing);

                doc.ActiveWindow.Selection.WholeStory();
                doc.ActiveWindow.Selection.Copy();
                IDataObject data = Clipboard.GetDataObject();
                this.richTextBox1.Text = data.GetData(DataFormats.Text).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (doc != null)
                {
                    app.Quit(ref missing, ref missing, ref missing);
                    app = null;
                }
            }

            textBox1.Text = ofd.FileName;
        }


最後更新:2017-04-03 05:39:17

  上一篇:go 阿裏雲全線雲產品免費計劃
  下一篇:go 【北大夏令營筆記-線段樹】POJ3468-A Simple Problem with Integers