C#进行Word文件的互操作
来源:互联网 发布:西安java 编辑:程序博客网 时间:2024/06/16 15:37
近日用到word文件的读取,先摘录最基本的从word文件读取文本和往word文件中加入文本的操作。具体关于文档格式的读取还在学习中。
using Microsoft.Office.Interop.Word;
using System.IO;
//操作Word文件
//新建一个word文件,然后追加内容
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object path = @"D:RogerWithLogWORDNewTest.doc";
Document myWordDoc;
if (File.Exists(path.ToString()))
...{
File.Delete(path.ToString());
}
Object Nothing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
string con = "This message will be added to the end of the passage Another Line?";
myWordDoc.Paragraphs.Last.Range.Text = con;
//将WordDoc文档对象的内容保存为DOC文档
myWordDoc.SaveAs(ref path, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭WordDoc文档对象
myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//打开文件读取内容
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object path = @"D:RogerWithLogWORDNewTest.doc";
Document myWordDoc;
if (File.Exists(path.ToString()))
...{
Object oMissing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Open(ref path, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
myWordDoc.Save();
TextBox1.Text = myWordDoc.Content.Text;
myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
}
using System.IO;
//操作Word文件
//新建一个word文件,然后追加内容
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object path = @"D:RogerWithLogWORDNewTest.doc";
Document myWordDoc;
if (File.Exists(path.ToString()))
...{
File.Delete(path.ToString());
}
Object Nothing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
string con = "This message will be added to the end of the passage Another Line?";
myWordDoc.Paragraphs.Last.Range.Text = con;
//将WordDoc文档对象的内容保存为DOC文档
myWordDoc.SaveAs(ref path, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭WordDoc文档对象
myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//打开文件读取内容
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object path = @"D:RogerWithLogWORDNewTest.doc";
Document myWordDoc;
if (File.Exists(path.ToString()))
...{
Object oMissing = System.Reflection.Missing.Value;
myWordDoc = myWordApp.Documents.Open(ref path, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
myWordDoc.Save();
TextBox1.Text = myWordDoc.Content.Text;
myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
}
- C#进行Word文件的互操作
- C#进行文件操作
- C#进行INI文件的读写操作
- c# 对word的操作
- C# 操作Word的类
- 操作Word的C# 类库
- C#的word操作札记
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- C#使用DOM进行XML文件的读写操作介绍
- C# 中使用Word文档对图像进行操作
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#的office操作[word&Excel]
- 用C#操作word的一些体会
- C#的office操作[word&Excel]
- C#的office操作[word&Excel]
- 很好的C#操作word例子
- C#的office操作[word&Excel]
- 常用的文章系统,如何用一条sql语句提取出每个分类的第一条并按OrderCol字段排序。分类大概几十个,一共只要取top 10 这样
- 社会生活中的著名法则
- Silverlight时钟
- 经济好用 佳能顶级喷墨一体机MP600降价
- 数据库行列交换实例
- C#进行Word文件的互操作
- 李维
- java中的文件操作
- 改变一生的五句话
- 夫妻程序员的故事
- 去爱吧
- OLTP OLAP
- Thinking In Java(java编程思想)
- Struts 2 + Spring 2 + JPA + AJAX