用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace MediaTools.Tool
{
public class WordHelper
{
public static void TxtToword(string docPath, string txtPath)
{
var txtContent = File.ReadAllText(txtPath, Encoding.UTF8);
WriteToWord(docPath, txtContent);
}
public static void WriteToWord(string docPath, string content)
{
if (!File.Exists(docPath)) CreateWordprocessingDocument(docPath);
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(docPath);
object missing = System.Reflection.Missing.Value;
doc.Content.Text = content;
app.Visible = false;
doc.Save();
doc.Close();
} private static void CreateWordprocessingDocument(string filepath)
{
// Create a document by supplying the filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure and add some text.
mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
Body body = mainPart.Document.AppendChild(new Body());
DocumentFormat.OpenXml.Drawing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Drawing.Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(""));
}
} public static string GetContentFromWord(string docPath)
{
const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; StringBuilder textBuilder = new StringBuilder();
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docPath, false))
{
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(wdDoc.MainDocumentPart.GetStream()); XmlNodeList paragraphNodes = xdoc.SelectNodes("//w:p", nsManager); foreach (XmlNode paragraphNode in paragraphNodes)
{
//XmlNodeList textNodes = paragraphNode.SelectNodes(".//['w: t'&'w:tab']", nsManager);
XmlNodeList textNodes = paragraphNode.SelectNodes(".//w:t", nsManager); foreach (System.Xml.XmlNode textNode in textNodes)
{
textBuilder.Append(textNode.InnerText);
}
textBuilder.Append(Environment.NewLine);
}
}
var result = textBuilder.ToString();
return result;
}
}
}
用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件的更多相关文章
- Microsoft.Office.Interop.Excel操作Excel文件时出现的问题及解决方案
问题描述: Microsoft.Office.Interop.Excel.Worksheet 打不开文件 Microsoft Office Excel 不能访问文件"a.xls". ...
- Microsoft.Office.Interop.Word 创建word
Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746 ...
- 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。
无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Wor ...
- 关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。
控制word表格单元格内部文字样式.我要将数据导出到word当中,对于word表格一个单元格中的一段文字,要设置不同的样式,比如第一行文字作为标题要居中,加粗,第二行为正常的正文. 代码如下 publ ...
- (转)无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。此操作失败的原因是对 IID 为“{00020970-
HRESULT:0x80030002 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft ...
- VS编程中找不到Microsoft.Office.Core、Microsoft.Office.Interop.Word和VBIDE
在使用vs2005. vs2008. vs2010 制作包含 word等office的应用程序时,有时找不到对Microsoft.Office.Core. Microsoft.Office.Inter ...
- C#引用Office.word出错的解决办法-无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass” 【转】
本文章转自 suchso 1.系统找不到 Microsoft.Office.Interop.Word" "Could not load file or assembly 'Micr ...
- 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。
引用里找到Microsoft.Office.Interop.Word右键属性 在嵌入互操作类型里,选上False就行了.
- System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。
报错:System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接 ...
随机推荐
- CXF2.7整合spring发布webservice
---------==========--服务端发布webservice-=============-------- 1.需要的jar包: 2.包结构 3.代码 1.实体类 package cn.ql ...
- gradle记录
set-env.bat set "JAVA_HOME=G:\jdk" set "CLASSPATH=%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\ ...
- $resource详解
$resource 首先添加$resource的引用 <script src="Scripts/angular.min.js"></script> < ...
- Shell基础总结
一.用户登陆进入系统后的系统环境变量 $HOME 使用者自己的目录 $PATH 执行命令时所搜寻的目录 $TZ 时区 $MAILCHECK 每隔多少秒检查是否有新的信件 $PS1 在命令列时的提示号 ...
- Keepalived详解(二):Keepalived安装与配置【转】
一.Keepalived安装与配置: 1.Keepalived的安装过程: Keepalived的安装非常简单,本实例以源码安装讲解: Keepalived的官方网址:http://www.keepa ...
- EntityFramework扩展之第三方类库
EntityFramework 非常好用,结构优美.. 但是美中有不足:1.对动态查询条件支持的不是很好 :2.批量操作支持的不是很好..下面就是几个第三方库,对EntityFramework 的扩展 ...
- JSON.stringify与JSON.parse
JSON.stringify(value [, replacer] [, space]) 用于将 对象 --> JSON 字符串. value:对象.数组.类 replacer: 数组时:v ...
- RGB、YUV和HSV颜色空间模型
一.概述 颜色通常用三个独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间.但被描述的颜色对象本身是客观的,不同颜色空间只是从不同的角度去衡量同一个对象.颜色空间按照基本机 ...
- 前端 ----- 01 -html介绍和head标签
01-html介绍和head标签 主要内容 web标准 浏览器介绍 开发工具介绍 HTML介绍 HTML颜色介绍 HTML规范 HTML结构详解 一.web标准 web准备介绍: w3c:万维网联 ...
- 【原创】大叔经验分享(29)cdh5使用已存在的metastore数据库部署hive
cdh5.16.1使用的hive版本是hive-1.1.0+cdh5.16.1+1431,详见:https://www.cloudera.com/documentation/enterprise/re ...