.net MVC使用Aspose.Words 获取文本域获取文档

匿名 (未验证) 提交于 2019-12-02 22:06:11

controller

 1 using Aspose.Words;  2 using Aspose.Words.Saving;  3 using System.IO;  4   5   6         /// <summary>  7         /// 获取导入Word 文档  8         /// </summary>  9         /// <param name="PaperId"></param> 10         /// <returns></returns> 11         public ActionResult GetWord(int PaperId) 12         { 13             try 14             { 15                 var __data = _paperApp.GetWord(PaperId); 16                 string tempPath = Server.MapPath("~/Template/导出模版.docx"); 17                 string outputPath = Server.MapPath("~/Resources/Output/模版_temp.doc"); 18                 //载入模板 19                 var doc = new Document(tempPath); 20                 //提供数据源 21                 String[] fieldNames = new String[] { "PaperName", "PaperTypeName", "SingleChoiceCount", "SingleChoiceScore", "SingleChoiceContent", 22                 "MultipleChoiceCount", "MultipleChoiceScore", "MultipleChoiceContent", "TrueFalseCount", "TrueFalseScore", "TrueFalseContent" }; 23                 Object[] fieldValues = new Object[] { __data.PaperName, __data.PaperTypeName, __data.SingleChoiceCount, __data.SingleChoiceScore, __data.SingleChoiceContent, 24                 __data.MultipleChoiceCount, __data.MultipleChoiceScore, __data.MultipleChoiceContent, __data.TrueFalseCount, __data.TrueFalseScore, __data.TrueFalseContent }; 25                 //合并模版,相当于页面的渲染 26                 doc.MailMerge.Execute(fieldNames, fieldValues); 27                 //保存合并后的文档 28                 doc.Save(outputPath);//在MVC中采用,保存文档到流中,使用base.File输出该文件 29                 var docStream = new MemoryStream(); 30                 doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); 31                 return base.File(docStream.ToArray(), "application/msword", "试卷" + __data.PaperName + ".doc"); 32             } 33             catch (Exception ex) 34             { 35                 return Error(ex.Message); 36             } 37         }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!