1
我使用Spire.doc创建一个Word文件

,我跟着他们为榜样这样从控制器下载Spire.doc文档进行查看

public class WordController : Controller 
{ 
    public void Download() 
    { 
     Document doc = new Document(); 

     Paragraph test = doc.AddSection().AddParagraph(); 

     test.AppendText("This is a test"); 

     doc.SaveToFile("Doc.doc"); 

     try 
     { 
      System.Diagnostics.Process.Start("Doc.doc"); 
     }catch(Exception) 
     { 

     } 
    } 
} 

这将打开在Microsoft Word中的Word文件,但如何能我做到了,所以它的下载呢?

我已经使用​​将PDF文档返回给视图,但它不适用于此。

回答

3

可否请你尝试下面的代码,让,我知道它的工作或没有因为我没有执行该代码,但相信这应该工作,我根据自己的规定─修改了现有的工作代码

 public class WordController : Controller 
     { 
      public void Download() 
      { 
       byte[] toArray = null; 
       Document doc = new Document(); 
       Paragraph test = doc.AddSection().AddParagraph(); 
       test.AppendText("This is a test"); 
       using (MemoryStream ms1 = new MemoryStream()) 
       { 
        doc.SaveToStream(ms1, FileFormat.Doc); 
        //save to byte array 
        toArray = ms1.ToArray(); 
       } 
       //Write it back to the client 
       Response.ContentType = "application/msword"; 
       Response.AddHeader("content-disposition", "attachment; filename=Doc.doc"); 
       Response.BinaryWrite(toArray); 
       Response.Flush(); 
       Response.End(); 
      } 
     }