richTextBox实现带格式word文档的打开、保存、另存为.

http://blog.sina.com.cn/s/blog_991ce3020100zn8p.html

        连日来,被这个问题困扰。因为用了第三方插件,所以无法使用RichtextBox原本的rtf输出功能。

        想要通过剪贴板来完成却屡屡受挫。终于找到这篇博文,解决了问题。

        其实我遇到的问题就是,控件所带的Clipboardcopy()(C1 Richtextbox)方法在复制到剪贴板时,必须得经过一次MessageBox才行(很奇怪的)。


        

早在网上查找richTextBox打开保存带格式的word文档的方法,但是在网上只找到了保存成不带格式的(纯文本)的word文档的代码。今天对这些代码进行修改,终于实现了我想要的功能。

 

我这里其实是用剪切板来实现带格式文档的打开和保存的。话不多说,下面是代码:

 

//用richTextBox打开带格式的word文档。
//先打开word文档,全选其中的内容并保存的剪切板中,最后在richTextBox中粘贴数据,并关闭文档
public void OpenWord(string fileName)
{
    Word.ApplicationClass app = new Word.ApplicationClass();
    Word.Document doc = null;
    object missing = System.Reflection.Missing.Value;
    object File = fileName;
    object readOnly = false;
    object isVisible = true;
    try
    {
        doc = app.Documents.Open(ref File, 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();//全选word文档中的数据
        doc.ActiveWindow.Selection.Copy();//复制数据到剪切板
        richTextBox1.Paste();//richTextBox粘贴数据
        //richTextBox1.Text = doc.Content.Text;//显示无格式数据
    }
    finally
    {
        if (doc != null)
        {
            doc.Close(ref missing, ref missing, ref missing);
            doc = null;
        }
 
        if (app != null)
        {
            app.Quit(ref missing, ref missing, ref missing);
            app = null;
        }
    }
}
 
//保存richTextBox数据到已经存在的word文档中。
//先打开word文档,全选word文档中的数据,然后全选richTextBox中的数据并复制,粘贴到word文档中,最后保存word文档,并关闭文档
public void SaveWord(string fileName)
{
    Word.ApplicationClass app = new Word.ApplicationClass();
    Word.Document doc = null;
    object missing = System.Reflection.Missing.Value;
    object File = fileName;
    object readOnly = false;
    object isVisible = true;
    try
    {
        doc = app.Documents.Open(ref File, 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();//全选
        richTextBox1.SelectAll();
        Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);//复制RTF数据到剪贴板
        doc.ActiveWindow.Selection.Paste();
        //doc.Paragraphs.Last.Range.Text = richTextBox1.Text;//word文档赋值数据,不带格式
        doc.Save();
    }
    finally
    {
        if (doc != null)
        {
            doc.Close(ref missing, ref missing, ref missing);
            doc = null;
        }
 
        if (app != null)
        {
            app.Quit(ref missing, ref missing, ref missing);
            app = null;
        }
    }
}
 
//将richTextBox带格式的文本另存为word文档
//先创建一个word文档,全选word文档中的数据,然后全选richTextBox中的数据并复制,粘贴到word文档中,最后保存word文档,并关闭文档
public void SaveAsWord(string fileName)
{
    Word.ApplicationClass app = new Word.ApplicationClass();
    Word.Document doc = null;
    object missing = System.Reflection.Missing.Value;
    object File = fileName;
    try
    {
        doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
 
        doc.ActiveWindow.Selection.WholeStory();//全选
        richTextBox1.SelectAll();
        Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);//复制RTF数据到剪贴板
        doc.ActiveWindow.Selection.Paste();
 
        doc.SaveAs(ref File, ref missing, ref missing,
            ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing,
            ref missing);
    }
    finally
    {
        if (doc != null)
        {
            doc.Close(ref missing, ref missing, ref missing);
            doc = null;
        }
 
        if (app != null)
        {
            app.Quit(ref missing, ref missing, ref missing);
            app = null;
        }
    }
}


你可能感兴趣的:(C#初级阶段学习,word,richtextbox,clipboard)