C++WORD操作实现代码

上传人:ji****72 文档编号:37505499 上传时间:2018-04-17 格式:DOC 页数:7 大小:51KB
返回 下载 相关 举报
C++WORD操作实现代码_第1页
第1页 / 共7页
C++WORD操作实现代码_第2页
第2页 / 共7页
C++WORD操作实现代码_第3页
第3页 / 共7页
C++WORD操作实现代码_第4页
第4页 / 共7页
C++WORD操作实现代码_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《C++WORD操作实现代码》由会员分享,可在线阅读,更多相关《C++WORD操作实现代码(7页珍藏版)》请在金锄头文库上搜索。

1、1.先通过程序生成报表样式的 HTML 页面,然后修改 HTML 页面的后缀名为 DOC。 2.定制 WORD 文档的模板文件,在 C#中操作 WORD 模板,生成新的 WORD 文档。 第一方案简单,只需要改动文件的扩展名就行了,但是也存在了一些问题,譬如生成的 WORD 文档样式的丢失。这样对于客户来说可能是一个无法通过的方案。第二方案比较复 杂,需要调用 OFFICE 的 WORD 组件通过 C#来操作 WORD,进而生成 WORD。此方法类 似于我们在 c#中的后台拼接数据。虽然麻烦,但是能够灵活定制,只不过是操作 WORD 对象而已。 经过再三考虑:决定用第二种方法来生成 WORD

2、报告文档。 通过自己的实践,这个需求总算是搞定了,在实际开发的过程中,遇到了这样那样的问题, 还好,通过不断的查找网络资源,结合实际开发中的情况,问题都得到了解决。现将本人 在开发过程中的一些理解与经验总结一下: 在 VS2008 平台下,引用.netMicrosoft.Office.Interop.Word.12,这样就可以在程序用操作 WORD 对象了。 通过简单执行,报了 80070005 错误,这个错误是因为权限不够,需要在 DCOM 配置中更改. net 和 IIS 用户的操作权限,具体修改过程如下: 解决方法一: 1.控制面板管理工具组件服务计算机我的电脑DCom 配置找到 Mic

3、rosoft Word 文档之后,单击属性打开此应 用程序的属性对话框。 2.单击标识选项卡,然后选择交互式用户。 3.单击“安全“选项卡,分别在“启动和激活权限“和“访问权限“组中选中“自定义“,然后自定义- 编辑-添加 ASP.NET 账户和 IUSER_计算机 名。 4. 确保允许每个用户访问,然后单击确定。 5. 单击确定关闭 DCOMCNFG。 如果上述方法不能解决问题,就应该是权限问题,请尝试用下面的方法: 在 web.config 中使用身份模拟,在节中加入 解决了上述问题,开始考虑如何创建 WORD 模板文件,WORD 的模板文件其实就是通过 书签来添加内容的。也就是通过在 W

4、ORD 文档中创建书签,然后在程序中获取模板文件 的所有书签,通过给书签赋值来进行文档生成的。 在程序中的操作流程如下: 声明 WORD 程序的对象 声明一个 WORD 文档对象 获取当前的操作文档对象 获取文档所有的书签 将数据库数据赋值到对应的书签 将文档另存为指定的文件夹 下. 下面将针对农业植物测试报告来分析具体的代码实现: 复制代码 代码如下: /生成 WORD 程序对象和 WORD 文档对象 Microsoft.Office.Interop.Word.Application appWord = new Application(); Microsoft.Office.Interop.

5、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,

6、 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 = 0; oIndex 0) Sys

7、tem.Diagnostics.ProcessModule pm = p.Modules0; myS += “n Modules0.FileName:“ + pm.FileName; myS += “n Modules0.ModuleName:“ + pm.ModuleName; myS += “n Modules0.FileVersionInfo:n“ + pm.FileVersionInfo.ToString(); if (pm.ModuleName.ToLower() = “winword.exe“) p.Kill(); catch finally 目前为止,一个 WORD 文档就生成了

8、。上述为我在这个程序开发中遇到的问题和解决方 法,可能有好多地方都是考虑不全的,如果在程序开发中对 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

9、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 fileN

10、ame, 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

11、 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; Wo

12、rd._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing,

13、 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 = 0; object end = 0

14、; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables1; object beforeRow = newTable.Rows1; newTable.Rows.Add(ref beforeRow); .单元格合并 复制代码 代码如下: object oMissing = System.Reflection.Missing.Value;

15、 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 = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables1; object beforeRow = newTable.Rows1; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2); .单元格分离 复制代码 代码如下: object oMissing = System.Reflection.Missing.Value; Word._Appli

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号