积极答复者
HELP!!C#winfrom写入文件操作!!

问题
答案
-
在向Word文档中写入文本时,仍然需要使用上节介绍的Microsoft Word X Object Library COM组件,写入文本的方法主要为设置MSWord.Document.Paragraphs.Last.Range.Text属性
引用一下命名空间
using Words = Microsoft.Office.Interop.Word;
using System.Reflection;object path;
string strContent;
Words.Application wordApp;//word应用程序变量
Words.Document wordDoc;//word文档变量path = Application.StartupPath + "试卷5.docx";
wordApp = new Words.ApplicationClass();//Init
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
object nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
strContent = this.textBox1.Text;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//WdSaveFormat为Word 2007文档的保存格式
object forma = Words.WdSaveFormat.wdFormatDocumentDefault;
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc.SaveAs(ref path, ref forma, 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.Close(ref nothing,ref nothing,ref nothing );
//关闭
wordApp.Quit(ref nothing, ref nothing, ref nothing);PS:新手一名
- 已标记为答案 feiyun0112Moderator 2011年4月11日 1:48
全部回复
-
在向Word文档中写入文本时,仍然需要使用上节介绍的Microsoft Word X Object Library COM组件,写入文本的方法主要为设置MSWord.Document.Paragraphs.Last.Range.Text属性
引用一下命名空间
using Words = Microsoft.Office.Interop.Word;
using System.Reflection;object path;
string strContent;
Words.Application wordApp;//word应用程序变量
Words.Document wordDoc;//word文档变量path = Application.StartupPath + "试卷5.docx";
wordApp = new Words.ApplicationClass();//Init
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
object nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
strContent = this.textBox1.Text;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//WdSaveFormat为Word 2007文档的保存格式
object forma = Words.WdSaveFormat.wdFormatDocumentDefault;
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc.SaveAs(ref path, ref forma, 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.Close(ref nothing,ref nothing,ref nothing );
//关闭
wordApp.Quit(ref nothing, ref nothing, ref nothing);PS:新手一名
- 已标记为答案 feiyun0112Moderator 2011年4月11日 1:48
-
word文件是专有的格式,不是简单的文本
http://feiyun0112.cnblogs.com/