操作word之前需要在COM引入Microsoft Office 12.0 Object Library(文件库可能不一样)

然后添加using Microsoft.Office.Interop.Word;

读操作,docFilename为文件路径

private string Doc2Text(string docFileName)        
{
StringBuilder sb = new StringBuilder();
ApplicationClass wordApp = new ApplicationClass();
object fileobj = docFileName;
object unknow = System.Reflection.Missing.Value;
//打开指定文件
try
{
_Document doc = wordApp.Documents.Open(ref fileobj, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow
);
int paragraphsCount = doc.Paragraphs.Count;
for (int i = ; i <= paragraphsCount; i++)
{
sb.AppendLine(doc.Paragraphs[i].Range.Text.Trim());//获得文档内容
}
doc.Close(ref unknow, ref unknow, ref unknow);
wordApp.Documents.Save(ref unknow, ref unknow);
wordApp.Quit(ref unknow, ref unknow, ref unknow);
}
catch (Exception) { }
return sb.ToString();
}
 

写操作

 private Boolean WriteLocalFile(string DocFileName, string text)
{
try
{ object fileobj = DocFileName;
object unknow = System.Reflection.Missing.Value;
//打开word程序,创建一个新的word文档,但是还没有保存到硬盘中
ApplicationClass wordApp = new ApplicationClass();
_Document doc = wordApp.Documents.Add(ref unknow, ref unknow, ref unknow, ref unknow);
doc.Content.Text += text;
//保存word文档
doc.SaveAs(ref fileobj, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
doc.Close(ref unknow, ref unknow, ref unknow);
wordApp.Documents.Save(ref unknow, ref unknow);
wordApp.Quit(ref unknow, ref unknow, ref unknow);
return true;
}
catch (Exception) { return false; }
}

第一次写博客,也是为了记录和分享学过的东西。

C#操作word内容,可以参考https://msdn.microsoft.com/en-us/library/office/dn320614.aspx

C#读写word的更多相关文章

  1. java 读写word java 动态写入 模板文件

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...

  2. POI读写Word docx文件

    使用POI读写word docx文件 目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx ...

  3. python读写word、excel、csv、json文件

    http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...

  4. 使用POI读写word docx文件

    目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx文件 2.1     直接通过XWPF ...

  5. 使用POI读写Word doc文件

    使用POI读写word doc文件 目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写w ...

  6. 读写Word的组件DocX介绍与入门

    本文为转载内容: 文章原地址:http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 开源Word读写组件DocX介绍与入门 阅读 ...

  7. C#使用Spire.Doc Word for .Net读写Word

    以前对Excel或Word文档操作都使用微软的COM组件Microsoft Word 15.0 object library. 但是这种方式必须要求服务器上安装Office,而且会出现读写操作完成后未 ...

  8. android使用POI读写word doc文件

    目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写word doc文件 Apache p ...

  9. Docx读写Word

    Docx.dll功能比较强大,具备以下功能: 创建新的word文档或者读取已有的world文档 替换书签处内容: 插入表格或者在已有表格新增数据行: 插入图片,轻松设置图片大小: 保存或者另存为: 分 ...

  10. poi读写word模板 / java生成word文档

    有一word文档表格 形如: 姓名 ${name} 电话 ${tel} 从数据库读取记录替换上述变量 import java.io.FileOutputStream; import java.util ...

随机推荐

  1. 运行一个Hadoop Job所需要指定的属性

    1.设置job的基础属性 Job job = new Job(); job.setJarByClass(***.class); job.setJobName("job name") ...

  2. Win7中,取消共享文件夹后有个小锁

    用过windows7的朋友都知道,Windows 7 中设置某一个文件夹属性为共享后,文件夹的图标上就增加一个小锁图案.起到了一个标记作用,挺好的.但是即使你将该文件夹的共享功能取消后,该小锁图案还是 ...

  3. Scala学习笔记--提取器unapply

    提取器就是一个带有unapply方法的对象.你可以把unapply方法当做是伴生对象中apply方法的反向操作. apply方法接收构造参数,然后将他们变成对象. 而unapply方法接受一个对象,然 ...

  4. Form表单插件jquery.form.js

    常常使用到这个插件,但是老忘记怎么使用,现在对大家写的进行一定的整合. 使用插件实例: 一般的使用方法 <!-- 引入jquery文件 --> <script src="h ...

  5. ADT 连接手机运行android应用程序时报错

    The connection to adb is down, and a severe error has occured.    You must restart adb and Eclipse.  ...

  6. hdu 六度分离

    http://acm.hdu.edu.cn/showproblem.php?pid=1869 #include <cstdio> #include <cstring> #inc ...

  7. LeetCode_Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. LeetCode_Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. bitmap 内存溢出OOM的解决办法分享

    昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...

  10. java设计模式--结构型模式--组合模式

    什么是组合模式,这个有待研究,个人觉得是各类组合而形成的一种结构吧. 组合模式: 组合模式 概述 将对象组合成树形结构以表示"部分-整体"的层次结构."Composite ...