通過MVC+jQuery實(shí)現(xiàn)頁面部分區(qū)域?qū)С龀蓋ord文檔腥光,直接上代碼:
Controller
<pre>
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", "word"+ ".doc");
}
</pre>
js
<pre>
$("#btn_export").click(function (){
var fullPage = $("#list-container").html();
var url = "/Report/ExportWord";
$('<form action="' + url + '" method="post"><input type="hidden" id="hidWord" name="hidWord" value="' + encodeURIComponent(fullPage) + '" /></form>').appendTo('body').submit().remove();
});
</pre>
View
<pre>
<div class="content">
<input type="button" id="btn_export" value="導(dǎo)出" />
<div class="list" id="list-container">
<table cellpadding="0" cellspacing="0" width="1880px" id="data-grid00">
<thead>
<tr>
<th data-field="cfh">處方號(hào)</th>
<th>患者姓名</th>
<th>處方內(nèi)容</th>
<th>問題標(biāo)題</th>
<th>問題詳情</th>
</tr>
</thead>
<tbody>
<tr class="row-4">
<td>CF20160225094242</td>
<td>王霞</td>
<td>維生素B12注射液</td>
<td>2-3藥品劑型或給藥途徑不適宜</td>
<td>藥品說明書未指出本品可口服</td>
</tr>
<tr class="row-4">
<td>CF20160220095745</td>
<td>蘭玉</td>
<td>維生素C注射液</td>
<td>2-3藥品劑型或給藥途徑不適宜</td>
<td>藥品說明書未指出本品可口服</td>
</tr>
</tbody>
</table>
</div>
</div>
</pre>