#region  读取word
/// <summary>
/// 读取word所有文字内容(不包含表格)
/// </summary>
/// <returns>word中的字符内容(纯文本)</returns>
public string ReadAllFromWord()
{
Word.ApplicationClass app = null;
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
object readOnly = true;
object isVisible = false;
try
{
app = new Word.ApplicationClass();
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); string textString = "";
//读取全部内容 何问起 hovertree.com
textString = doc.Content.Text.Trim();
// int ParCount = this.getParCount(doc);//段数
// for (int i = 1 ; i <= ParCount ; i++)
// {
// textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
// }
textString = textString.Replace("/a",""); //替换空串为空。(word中/a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
textString = textString.Replace("/r","/n"); //替换回车为回车换行
return textString;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
try
{
doc.Close(ref missing, ref missing, ref missing);
}
catch
{}
doc = null;
}
if (app != null)
{
try
{
app.Quit(ref missing, ref missing, ref missing);
}
catch
{}
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers(); }
}
#endregion #region 追加写入word /// <summary>
/// 追加写入word
/// </summary>
/// <param name="InsertText">需要写入的字符串</param>
public void WriteToWord(string InsertText)
{
Word.ApplicationClass app = null;
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
object readOnly = false;
object isVisible = false;
try
{
app = new Word.ApplicationClass();
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);
//激活word文档
doc.Activate();
//追加到最后一段(段落是按照 /n 来作为标志的)
doc.Paragraphs.Last.Range.Text = InsertText + "/n";//加个结束符(增加一段),否则再次插入的时候就成了替换.
//保存
doc.Save();
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
try
{
doc.Close(ref missing, ref missing, ref missing);
}
catch
{}
doc = null;
}
if (app != null)
{
try
{
app.Quit(ref missing, ref missing, ref missing);
}
catch
{}
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
} #endregion

推荐:http://www.cnblogs.com/roucheng/p/3521864.html

C#操作 word代码的更多相关文章

  1. 转载:C# Word操作实现代码

    转载自:http://www.jb51.net/article/17770.htm 在VS2008平台下,引用.net-Microsoft.Office.Interop.Word.12,这样就可以在程 ...

  2. C# WORD操作实现代码(转载)

    在当前项目开发过程中,客户有根据数据库数据生成WORD文档的需求,在和同事沟通的过程中,找到了两个解决方案 1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC. 2.定制W ...

  3. C#中操作Word(1)—— word对象模型介绍

    一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...

  4. C#操作Word的超详细总结

    本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...

  5. C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  6. C#操作Word的辅助类(word2003) 修改完善版

    转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...

  7. c#操作word表格

    http://www.webshu.net/jiaocheng/programme/ASPNET/200804/6499.html <% if request("infoid" ...

  8. OpenXML操作word

    OpenXML概述 项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错.现在开始采用OpenXML.OpenXML(OOXML)是微软在Office 2007中提出的一 ...

  9. ASP.NET操作Word的IIS权限配置

    ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-000 ...

随机推荐

  1. [ACM_几何] Wall

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/E 题目大意:依次给n个点围成的一个城堡,在周围建围墙,要求围墙 ...

  2. CSS控制样式的三种方式优先级对比验证

    入职已经一个月了,自此后,就好久没有写过博客了,在此先跟关注我的博友们说声抱歉.今天,在公司的一个培训作业的驱动以及伟哥那句“再不写博客就开除你”的监督下,我终于重拾旧爱,再次登录博客园,继续与大家分 ...

  3. java source not found

    今天的挑战是,为什么与源码明明放在本机,调试的时候却说source not found呢? 这个是因为,我重新建立了debug,然后没有选择对应的项目. 另外一个问题是,如果给底层的jar附上源码呢?

  4. Post方式的Http流请求调用

    HttpRequest公共类: public static class HttpRequestAction { /// <summary> /// 发送http请求并返回响应 /// &l ...

  5. 知方可补不足~SqlServer连接的复用MultipleActiveResultSets

    回到目录 MultipleActiveResultSets可以使数据库连接复用,但当你上了moebius这种集群工具后,这个选项不能开启(默认是false),当你使用EF等ORM工具时,这个选项会默认 ...

  6. EF架构~Cannot attach the file as database

    回到目录 Cannot attach the file as database这个异常是在EF的code frist里经常出现的,解决方法很简单,只要重新启动一下V11实例即可. CMD> sq ...

  7. TSQL order by 子句中排序列的多种写法

    Order by 子句用于对结果进行排序,执行顺序位于select子句之后,排序列有4中写法: column_name column_alias,由于order by子句的执行顺序位于select子句 ...

  8. 深入理解CSS定位中的堆叠z-index

    × 目录 [1]定义 [2]堆叠规则 [3]堆叠上下文[4]兼容 前面的话 对于所有定位,最后都不免遇到两个元素试图放在同一位置上的情况.显然,其中一个必须盖住另一个.但,如何控制哪个元素放在上层,这 ...

  9. Dijkstra算法(一)之 C语言详解

    本章介绍迪杰斯特拉算法.和以往一样,本文会先对迪杰斯特拉算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法 ...

  10. 用实例讲解Spark Sreaming--转

    原文地址:http://www.infoq.com/cn/articles/spark-sreaming-practice 本篇文章用Spark Streaming +Hbase为列,Spark St ...