怎样用C#实现新建Word文档的功能

我想实现单击一个按钮,然后新建Word文档的功能,代码如下:
private void Btnnigao_Click(object sender, System.EventArgs e)
{

Object oMissing =System.Reflection.Missing.Value;
Word.Application Word_App=new Word.Application();
Word.Document Word_doc=new Word.Document();
Word.Documents Docs= Word_App.Documents;
Word._Document my_Doc=(Word._Document) Word_doc;
Word_doc=Docs.Add(ref oMissing,ref oMissing,ref oMissing, ref oMissing);
Word_App.Visible =true;
object start=0;
object end= 0;
Word.Range range = Word_doc.Range(ref oMissing,ref oMissing);

object missing = Type.Missing;
//用创建时间代替文件名
string fileTime=DateTime.Now.Year.ToString()
+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()
+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()
+DateTime.Now.Second.ToString()+Datetime.Now.MilliSecond.ToString();

filename=fileTime;

object path =Server.MapPath("UpFile")+"\\"+fileTime; //指定保存路径
filePath=path.ToString();

Word_doc.SaveAs(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
//在关闭的时候自动保存
object save=true;
Word_doc.Close(ref save, ref missing, ref missing);

}
麻烦各位大侠帮我看看,头一天都正常的,第二天单击按钮也没反应,现在急用呢,别的步骤都要在这个前提下才能解决。

这里提供一种新的思路,借助现成的 C# Word API实现新建Word文档功能。

GrapeCity Documents for Word 适用于所有 .NET Standard 2.0 及以上标准的平台,以编码的方式,无需 Microsoft Word 组件 ,超快批量处理 Word 文件,满足您关于 Word 文档的一切需求。网页链接

加载DOCX文件并向内容添加注释代码如下:

//
//此代码是GrapeCity Documents for Word示例的一部分。
//版权所有(c)GrapeCity,Inc。保留所有权利。
//
使用系统; 
使用系统。IO ; 
使用系统。绘画; 
使用GrapeCity 。文件。字; 
 
命名空间GcWordWeb 。样品 
{
    //此示例显示如何将现有DOCX文件加载到GcWord中。
    //它还会在加载的文档末尾附加一个简短的注释。
    公共类LoadDocx  
    {
        public GcWordDocument CreateDocx ()  
        {
            var doc = new GcWordDocument ();  
 
            //加载现有的DOCX文件:
            var path = Path 。合并(“资源” ,“WordDocs” ,“JsFrameworkExcerpt.docx” );   
            doc 。加载(路径);
 
            //在文档末尾添加注释:
            doc 。身体。部分。最后。GetRange ()。段落。添加($ “加载到{DateTime.Now}上的GcWord。” );
 
            //完成:
            返回文档;
        }
    }
}

网页链接

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-02-07

可以用第三方免费Word组件Free Spire.Doc for .NET创建和操作Word,不依赖于Office,不容易出问题而且更简单,例子:创建 Word 文档

第2个回答  2008-05-04
object path =Server.MapPath("UpFile")+"\\"+fileTime; //指定保存路径

//如果文件名重复,加上一句
System.IO.File.Delete(path.ToString());

filePath=path.ToString();

再将 C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\OFFICE 下的文件全部清空试试本回答被网友采纳
第3个回答  2008-05-04
出现这种奇怪的情况,一般你将

C:\Documents and Settings\用户名\Local Settings\Application Data\Microsoft\Office\12.0

下的临时文件删除之后便恢复了