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

 

 

controller


 /// <summary> /// 导入Word 文档 /// </summary> /// <param name="PaperId"></param> /// <returns></returns> public ActionResult GetWord(int Id) { try { var __data = _App.GetWord(Id); string tempPath = Server.MapPath("~/Template/试卷导出模版.docx"); string outputPath = Server.MapPath("~/Resources/Output/试卷导出模版_temp.doc"); //载入模板 var doc = new Document(tempPath); //提供数据源 String[] fieldNames = new String[] { "PaperName", "PaperTypeName", "SingleChoiceCount", "SingleChoiceScore", "SingleChoiceContent", "MultipleChoiceCount", "MultipleChoiceScore", "MultipleChoiceContent", "TrueFalseCount", "TrueFalseScore", "TrueFalseContent" }; Object[] fieldValues = new Object[] { __data.PaperName, __data.PaperTypeName, __data.SingleChoiceCount, __data.SingleChoiceScore, __data.SingleChoiceContent, __data.MultipleChoiceCount, __data.MultipleChoiceScore, __data.MultipleChoiceContent, __data.TrueFalseCount, __data.TrueFalseScore, __data.TrueFalseContent }; //合并模版,相当于页面的渲染 doc.MailMerge.Execute(fieldNames, fieldValues); //保存合并后的文档 doc.Save(outputPath);//在MVC中采用,保存文档到流中,使用base.File输出该文件 var docStream = new MemoryStream(); doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); return base.File(docStream.ToArray(), "application/msword", "试卷" + __data.PaperName + ".doc"); } catch (Exception ex) { return Error(ex.Message); } }

GetWordController

 

相关文章