讨论一段代码!!!

wosizy 2011-06-02 11:04:32
最近在网上看了一段往word文档中写入文字的代码例子
第一次写这方面的代码 所以请各位懂的来讨论一二

问题:1、下面这段代码能往服务器端的路径下的word文档写入文字吗。
2、当我的路径下面的word文档名称一样时 怎么使它能把文字追加进去
现在不能往同一个word里面追加文字 它现在直接替换了。

求改写...

Object Nothing = System.Reflection.Missing.Value;
//取得Word文件保存路径
object filename = "C:\myword.doc";
//创建一个名为WordApp的组件对象
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
//创建一个名为WordDoc的文档对象
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//增加一表格
Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range, 1, 1, ref Nothing, ref Nothing);
//在表格第一单元格中添加自定义的文字内容
table.Cell(1, 1).Range.Text = wordText.Text;
//在文档空白地方添加文字内容
WordDoc.Paragraphs.Last.Range.Text = DateTime.Now.ToString("yyyy年M月d日h时s分m秒");//写入时间
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.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);
//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
...全文
60 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
1,权限足够的话,可以写入。
2,先打开,再追加。
子夜__ 2011-06-02
  • 打赏
  • 举报
回复
追加文字 替换 有啥区别啊。。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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