MVC post 方法导出word文档

View code:

function ExportWord(){
var html = $("#div_workInfo").html(); $("#hidWord").val(encodeURIComponent(html)); $("#formWord").submit(); }
html code:

 <div class="prc_view" id="div_workInfo">
            <div class="ques" id="div_sel"><h1>选择题:</h1>
                   <div class="test_lst" id="subId6">
                       </span><i>1.</i><div class="nr"><p>11</p></div>
                          <ul>
                              <li style=""><i><input name="check6" type="radio">A:</i><div class="nr"><p>22</p> </div>
                               </li>
                               <li style=""><i><input name="check6" type="radio">B:</i><div class="nr"><p>33</p> </div>
                                </li>
                                 <li style=""><i><input name="check6" type="radio">C:</i><div class="nr"><p>33让人</p> </div>
                                 </li>
                            </ul>
                     </div>
            </div>
</div>
<input type="button" onclick="ExportWord()" class="huge_btn_blue" value="导出Word">

<form action="@Url.Action("ExportWord")" method="post" id="formWord"><input type="hidden" id="hidWord" name="hidWord" /></form>
Controller code:

        /// <summary>
        /// Word文件导出
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       [HttpPost]
        public ActionResult ExportWord(FormCollection form)
        {
            StringBuilder sb = new StringBuilder();
            string html = Server.UrlDecode(form["hidWord"]);
            sb.Append("<!DOCTYPE html>");
            sb.Append("<body>");
            sb.Append(html);
            sb.Append("</body>");
            var byteArray = System.Text.Encoding.Default.GetBytes(sb.ToString());
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            return File(byteArray, "application/ms-word", "wordtest" + ".doc");
        }

 

posted @ 2016-05-26 11:32  wushuaizaiza  阅读(1989)  评论(0编辑  收藏  举报