欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-24 12:33:42
controller ......

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

 

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

 

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         }