• 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");
            }
    
  • 相关阅读:
    小小杨的影视空间
    关于励志的事情
    关于2020年的总结
    关于心情不好的时候
    关于我的2020年
    单链表基本操作的实现
    原型模式
    android—安卓系统文件目录结构
    android——apk安装文件的组成结构
    android——项目的组成结构
  • 原文地址:https://www.cnblogs.com/wushuaizaiza/p/5530425.html
Copyright © 2020-2023  润新知