我正在使用Spire.doc创建一个Word文件,并且我遵循了下面的示例
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文件,但我如何使它被下载呢?
我以前曾使用return File()将PDF文档返回到视图,但它并不适用于此。
发布于 2015-05-04 13:03:59
请您试一下下面的代码,让我知道它是否有效,因为我没有执行这段代码,但我相信它应该能工作,我根据您的要求修改了我现有的工作代码-
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();
}
}发布于 2020-08-29 14:44:06
将文件.docx加载到richtextbox.rtf (使用Spire.Doc ):
byte[] toArray = null;
//Paragraph test = doc.AddSection().AddParagraph();
//test.AppendText("This is a test") --> this also works ;
Document doc = new Document();
doc.LoadFromFile("C://Users//Mini//Desktop//doc.docx");
// or - Document doc = new Document("C://Users//Mini//Desktop//doc.docx");
using (MemoryStream ms1 = new MemoryStream())
{
doc.SaveToStream(ms1, FileFormat.Rtf);
toArray = ms1.ToArray();
richTextBox1.Rtf = System.Text.Encoding.UTF8.GetString(toArray);
}https://stackoverflow.com/questions/30030480
复制相似问题