关于 在Winform中 Microsoft.Office.Interop.Word 的操作问题

猴头 2014-09-12 05:18:02
Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();

Microsoft.Office.Interop.Word._Document wordDocument = wordApplication.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);

请问wordApplication 和 wordDocument 能直接 保存到 二进制数组里面吗??

现在 是把wordDocument保存成文件,然后从文件中读数据到数组中的,我想直接保存为二进制数组数据。

另外,能通过wordDocument或者wordApplication 在代码中直接调用打印机打印出来文档吗???
...全文
513 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
多一些背景知识,包括是 VBA 知识,才能理解 Office 二次开发。
  • 打赏
  • 举报
回复
不知道你说的“直接 保存到 二进制数组里面”具体是什么意思。如果你说的是.net对象二进制序列化,那么显然不行。所有的基于COM的“桥接”对象都不支持序列化。只有 Office 系统自己懂得如何真正创建内存中的对象。 word、excel等等,“文档就是应用程序”。因此你传送给别人一个文档,也就传送给别人一个(word或者excel)程序。代码就编译在文档中(这些被 Office 系统创建 VBProject对象)。你可以通过 Microsoft.Office.Interop 跨进程去操作 COM 对象,被 Office 系统创建的、表示文档或者工作簿的 COM 对象。但是这跟.net内部的对象不同,隔着好几层“大山”无法解决。 最后一个问题就非常容易理解了。可以通过 Microsoft.Office.Interop 跨进程操作对方,例如调用对方的 Print 方法启动打印功能。凡是你可以通过 VBA 编程的东西,你都可以试试通过你的 c# 调用相同的东西。Office 在这一点上设计的足够完善了,而且那是几十年前(还没有.net的时候)就已经完善了,并且任何一个支持 COM 跨进程服务互操作的应用的程序都可以这样控制 Office,10年前的互联网上就有着数不尽的文档,不是只有c# 才能对 Office 对象进行调用。
devmiao 2014-09-12
  • 打赏
  • 举报
回复
不可以,Document可以先SaveAs到文件,再读取到二进制数组。 Application更不可以,它是Word程序的一个代理接口,脱离了Word,它的存在就没有意义了。
首先机子上安装有office,在COM添加引用Microsoft.Word.11.0.Object.Library(或11.0以上) Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率 myWordApp = new Microsoft.Office.Interop.Word.Application(); doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; for (int i = 2; i < 102; i++)//举例100台 { doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行 doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号 doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号 doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数 doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 } doc.SaveAs(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印,文档不显示 //doc.PrintOut(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧