C#打开Word文档内容并显示是如何实现的呢?让我们通过C#打开Word文档的实现代码来学习C#打开Word文档的具体过程和注意事项以及在C#打开Word文档过程中涉及到的类的使用,希望对你有所帮助。
C#打开Word文档实例如下:
//在项目引用里添加上对Microsoft Word 11.0 object library的引用
private void button1_Click(object sender, System.EventArgs e)
{
//调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称
OpenFileDialog opd = new OpenFileDialog();
opd.InitialDirectory = \"c:\\\\\";
opd.Filter =
\"Word文档(*.doc)|*.doc|文本文档(*.txt)|
*.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*\";
opd.FilterIndex = 1;
if (opd.ShowDialog() ==
DialogResult.OK && opd.FileName.Length > 0)
{
//建立Word类的实例,缺点:不能正确读取表格,图片等等的显示
Word.ApplicationClass app = new Word.ApplicationClass();
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = opd.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();
}
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;[Page]
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
C#打开Word文档的具体实现的基本内容就向你介绍到这里,希望对你了解和学习C#打开Word文档有所帮助。
【编辑推荐】
- C#项目初期准备工作浅析
- C#项目的创建过程详解
- 详解C#读取word内容操作
- C#读取Word文件实例详解
- C#读取Word学习经验总结