转载:C# Word操作实现代码
转载自:http://www.jb51.net/article/17770.htm
在VS2008平台下,引用.net-Microsoft.Office.Interop.Word.12,这样就可以在程序用操作WORD对象了。
通过简单执行,报了80070005错误,这个错误是因为权限不够,需要在DCOM配置中更改.net和IIS用户的操作权限,具体修改过程如下: 解决方法一:
1.控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档之后,单击属性打开此应
用程序的属性对话框。
2.单击标识选项卡,然后选择交互式用户。
3.单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后自定义->编辑->添加ASP.NET账户和IUSER_计算机
名。
4. 确保允许每个用户访问,然后单击确定。
5. 单击确定关闭 DCOMCNFG。
如果上述方法不能解决问题,就应该是权限问题,请尝试用下面的方法:
在web.config中使用身份模拟,在<system.web>节中加入 <identity impersonate="true"
userName="你的用户名 " password="密码 "/>
</system.web>
解决了上述问题,开始考虑如何创建WORD模板文件,WORD的模板文件其实就是通过书签来添加内容的。也就是通过在WORD文档中创建书签,然后在程序中获取模板文件的所有书签,通过给书签赋值来进行文档生成的。
在程序中的操作流程如下:
声明WORD程序的对象 → 声明一个WORD文档对象 → 获取当前的操作文档对象 → 获取文档所有的书签 →
将数据库数据赋值到对应的书签 → 将文档另存为指定的文件夹下.
下面将针对农业植物测试报告来分析具体的代码实现:
//生成WORD程序对象和WORD文档对象
Microsoft.Office.Interop.Word.Application appWord = new Application();
Microsoft.Office.Interop.Word.Document doc = new Document();
object oMissing = System.Reflection.Missing.Value;//这个是什么东西,我始终没搞明白-_-
//打开模板文档,并指定doc的文档类型
object objTemplate = Server.MapPath(p_TemplatePath);
object objDocType = WdDocumentType.wdTypeDocument;
doc = (Document)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//获取模板中所有的书签
Bookmarks odf = doc.Bookmarks;
string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"};
string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",};
//循环所有的书签,并给书签赋值
for (int oIndex = ; oIndex < testTableremarks.Length; oIndex++)
{
obDD_Name = WD + testTableremarks[oIndex];
doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = p_TestReportTable.Rows[][testTablevalues [oIndex]].ToString();//此处Range也是WORD中很重要的一个对象,就是当前操作参数所在的区域
}
//第四步 生成word,将当前的文档对象另存为指定的路径,然后关闭doc对象。关闭应用程序
object filename = Server.MapPath(p_SavePath) + "\\Testing_" + DateTime.Now.ToShortDateString() + ".doc";
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
this.Hid_ShowMessage.Value = "生成成功!";
上述代码就是一个通过模板文件生成WORD的过程。其实也就是一个替换书签内容的过程。
在开发的过程中,有些数据是动态增加的,假如我要向一个表格中动态的添加几行数据,就无法用替换书签的方式来进行操作,需要通过程序在文档页面的表格中添加行。
向表格中添加行,有两种操作形式:一种是在WORD模板中已经存在了一个表格。一种是我们在程序中直接添加一个表格对象。
第一种情况下,需要注意:在WORD模板中要操作的表格中,不能有纵向合并的单元格,不然程序无法获取到当前要操作对象导致程序报错.单元格的合并,我们可以在程序中控制。
第二种情况下就需要我们通过程序去直接添加表格了。
生成表格的代码具体如下:
1.获取文档中已存在的表格:
Microsoft.Office.Interop.Word.Table characterTable =
doc.Tables[2];//在document对象的集合操作中,起始点是从1开始,并不是从0开始的,此处需要注意。
2.在文档中直接生成表格,首先要获取插入表格的位置,然后添加表格对象:
object oEndOfDoc = "\\endofdoc";//WORD中预定义的书签,还有很多,此处就不一一列举。
object oMissing = System.Reflection.Missing.Value;
Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//获取当前文档的末尾位置。
wrdRng.InsertAfter(" ");//插入一行,此处不能用
wrdRng.InsertAfter(""),如果用这个,就不能换行,我也不知道为什么。
object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//分页符
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertBreak(ref oPageBreak);//插入了一页
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertAfter("图片信息");
wrdRng.Font.Size = ;//指定操作对象的文字大小
wrdRng.Font.Bold = ;//指定操作对象的粗体:1为粗体,0为正常
wrdRng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//指定操作区域的文字布局:居中对齐
//上述代码的意思是:找到当前的末尾位置,然后插入一个分页符,相当于跳到了一个新页,在这个新页的顶端写入文字“图片信息”,并指定文字大小为20,粗体居中显示。
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertAfter(" ");
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();//插入一个段落,在此段落上插入一个2行一列的表格。
Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(wrdRng, , , ref oMissing, ref oMissing);
我们还可以对表格进行格式设置,此处我们就不在一一列举。
3.下面我们分析一下对表格的单元格的操作:合并,拆分。这个就需要我们根据实际表格来进行操作:
//获取具体的某个单元格(1,1),获取第一行第一列的单元格
Cell cell = doc.Tables[1].Cell(1,1);
cell.Range.Text="Text";//指定当前单元格的内容为Text
在Table的操作中,添加新行:
object
beforeRow = doc.Tables[1].Rows[2];//此行是先获取到第二行
doc.Tables[1].Rows.Add(ref
beforeRow);//效果类似于在WORD中此表格的第二行上进行【插入行】操作,插入的新行将会插入到当前行的上一行中,格式也是和此行一致的。
//合并单元格:感觉在此处合并单元格挺傻瓜的,你只需要指定你要合并的起始单元格和结束单元格,然后通过Merge操作就行了
Cell cell = doc.Tables[1].Cell(iRow, 2);//列合并
cell.Merge(doc.Tables[1].Cell(iRow, 6));
Cell cell1 = doc.Tables[1].Cell(iRow - 1, 1);//行合并
cell1.Merge(doc.Tables[1].Cell(iRow + 1, 1));
上述操作就是在此程序中用到的一些知识点,还有好多的东西需要去熟悉、理解。
另外,在程序的测试过程中发现,当执行一次文档生成后,在资源管理器中始终有winword.exe进程杀不掉,目前的解决办法是:直接杀进程,代码如下:
protected void killAllProcess() // 杀掉所有winword.exe进程
{
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in myPs)
{
if (p.Id != )
{
string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
try
{
if (p.Modules != null)
if (p.Modules.Count > )
{
System.Diagnostics.ProcessModule pm = p.Modules[];
myS += "\n Modules[0].FileName:" + pm.FileName;
myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
if (pm.ModuleName.ToLower() == "winword.exe")
p.Kill();
}
}
catch
{ }
finally
{ }
}
}
}
目前为止,一个WORD文档就生成了。上述为我在这个程序开发中遇到的问题和解决方法,可能有好多地方都是考虑不全的,如果在程序开发中对WORD的操作有新的认识的话,欢迎和我沟通交流,彼此提高!
下边是在网上一些比较好的摘抄:
创建新Word
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
打开文档:
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
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);
导入模板
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
添加新表
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = ;
object end = ;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, , , ref oMissing, ref oMissing);
表插入行
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = ;
object end = ;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, , , ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[];
object beforeRow = newTable.Rows[];
newTable.Rows.Add(ref beforeRow);
单元格合并
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = ;
object end = ;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, , , ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[];
object beforeRow = newTable.Rows[];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(, );
cell.Merge(newTable.Cell(, ));
单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add( oMissing,
ref oMissing, ref oMissing);
object start = ;
object end = ;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, , , ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[];
object beforeRow = newTable.Rows[];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(, );
cell.Merge(newTable.Cell(, ));
object Rownum = ;
object Columnnum = ;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = ;
oPara1.Format.SpaceAfter = ; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
转载:C# Word操作实现代码的更多相关文章
- C# WORD操作实现代码(转载)
在当前项目开发过程中,客户有根据数据库数据生成WORD文档的需求,在和同事沟通的过程中,找到了两个解决方案 1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC. 2.定制W ...
- [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc
开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...
- 开源word操作组件DocX的记录
开源word操作组件DocX的记录 使用开源word操作组件DocX的记录 1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱 ...
- 关于Aspose对于Word操作的一些扩展及思考
Aspose.word Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务.Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocu ...
- 怎样在Word中插入代码并保持代码原始样式不变
怎样在Word中插入代码并保持样式不变 我们有时候需要在word中添加一段我们写的代码,但是把代码粘贴到word文档中之后就发现所有的代码的样子都变了,我们可以采用下边的方法来实现保持代码原来的样式和 ...
- DocX开源WORD操作组件的学习系列二
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- Word中的代码怎样语法高亮
在平常我们粘贴代码到Word中的时候,经常会遇到代码粘贴到Word中后没有语法高亮,看着很乱很不友好,Word自带的样式---语法使用着也不尽人意, 网上有很多做法可以使得在插入在Word中的代码能够 ...
- Entity Framework 实体框架的形成之旅--几种数据库操作的代码介绍(9)
本篇主要对常规数据操作的处理和实体框架的处理代码进行对比,以便更容易学习理解实体框架里面,对各种数据库处理技巧,本篇介绍几种数据库操作的代码,包括写入中间表操作.联合中间表获取对象集合.递归操作.设置 ...
- C#各种文件操作的代码与注释
C#各种文件操作的代码与注释,具体看下面代码: using System; using System.Collections.Generic; using System.Linq; using Sys ...
随机推荐
- 交互设计师常用的web设计模式(转)
交互设计师在设计线框图原型时,熟知常见的web设计模式很有帮助,做到“心中有数”才能创造出符合需求,用户易学易用的界面来.所谓“没有必要重复发明轮子”,模式往往容易解决常见问题,正确的模式能帮用户熟悉 ...
- 《Play for Java》学习笔记(七)数据类型解析——Body parser
一.什么是body parser? body parser(不知道具体如何翻译,~~~~(>_<)~~~~ )指一个HTTP请求 (如POST和PUT操作)所包含的文本内容(body),这 ...
- 转载python2进制打包相关
Python模块——struct(字节流,组包拆包实现) http://www.linuxidc.com/Linux/2014-02/97158.htm [日期:2014-02-24] 来源:Linu ...
- IKAnalyzer 和 solr4.3 冲突
solr4.3 运行之后发现异常:Exception in thread "main" java.lang.VerifyError: class org.wltea.analyze ...
- ANGULAR 开发用户选择器指令
在开发表单时,我们需要使用经常需要使用到用户选择器,用户的数据一般使用如下方式存储: 用户1,用户2,用户3 我们可以使用angular指令实现选择器. <!DOCTYPE html> ...
- Wcf Client 异常和关闭的通用处理方法
在项目中采用wcf通讯,客户端很多地方调用服务,需要统一的处理超时和通讯异常以及关闭连接. 1.调用尝试和异常捕获 首先,项目中添加一个通用类ServiceDelegate.cs public del ...
- 和小猪一起搞微信公众号开发—获取Access_token
前言 前一篇小猪和大家分享了如何回复用户的简单文本,这一篇我们来看看如何获取Access_token 介绍 在前一篇中,我们实现了这么一个简单的过程:用户发送一个文本到公众号后,公众号在该文本后面加上 ...
- 通过CoreImage生成二维码
从IOS7开始集成了二维码的生成和读取功能 生成二维码的步骤: 1.导入CoreImage框架 2.通过滤镜CIFilter生成二维码 二维码的内容(传统的条形码只能放数字): 纯文本 名片 URL ...
- No module ata_piix found的解决方法
在一台as4u6的机器上升级内核到2.6.18时,最好make install的时候报了一个WARNING: No module ata_piix found for 2.6.18, 开始没有在意,重 ...
- Android双击返回按钮退出程序
//双击退出事件 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KE ...