richbox怎么显示doc文件 C#
展开全部
//用richTextBox打开带格式的word文档
//先打开word文档,全选其中的内容并保存的剪切板中,最后在richTextBox中粘贴数据,并关闭文档
public void OpenWord(string fileName)
{
//C#读取word文件
Microsoft.Office.Interop.Word.ApplicationClass app = new ApplicationClass();
Microsoft.Office.Interop.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)
{
//C#读取word文件之关闭文件
doc.Close(ref missing, ref missing, ref missing);
doc = null;
}
if (app != null)
{
//C#读取word文件
app.Quit(ref missing, ref missing, ref missing);
app = null;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询