pru*_*eba 4 c# ms-word c#-4.0 asp.net-mvc-4
这可能有一个非常简单的答案,但我找不到它.
我有一个使用C#MVC 4的项目使用Microsoft.Office.Interop.Word 12
在一个动作中,我尝试动态创建一个Word文件(使用数据库获取信息),然后我想下载它.该文件不存在(它是从头开始创建的),我不想将其保存在磁盘中(因为它的内容是动态的,所以不需要保存).
这是现在的代码:
public ActionResult Generar(Documento documento)
{
Application word = new Application();
word.Visible = false;
object miss = System.Reflection.Missing.Value;
Document doc = word.Documents.Add(ref miss, ref miss, ref miss, ref miss);
Paragraph par = doc.Content.Paragraphs.Add(ref miss);
object style = "Heading 1";
par.Range.set_Style(ref style);
par.Range.Text = "This is a dummy test";
byte[] bytes = null; // This is the part i need to get the bytes of the doc object
doc.Close();
word.Quit();
return File(bytes, "application/octet-stream", "NewFile.docx");
}
Run Code Online (Sandbox Code Playgroud)
使用Robert Harvey推荐的DocX.dll库(谢谢你,绅士),这将是解决方案:
using Novacode;
using System.Drawing;
.
.
.
public ActionResult Generar(Documento documento)
{
MemoryStream stream = new MemoryStream();
DocX doc = DocX.Create(stream);
Paragraph par = doc.InsertParagraph();
par.Append("This is a dummy test").Font(new FontFamily("Times New Roman")).FontSize(32).Color(Color.Blue).Bold();
doc.Save();
return File(stream.ToArray(), "application/octet-stream", "FileName.docx");
}
Run Code Online (Sandbox Code Playgroud)
我找不到使用Microsoft.Office.Interop.Word的解决方案(这么简单,我很失望).
再次感谢Robert,并希望这个例子可以帮助您解决问题.
| 归档时间: |
|
| 查看次数: |
11360 次 |
| 最近记录: |