通过MVC+jQuery实现页面部分区域导出成word文档,直接上代码:
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="导出" />
<div class="list" id="list-container">
<table cellpadding="0" cellspacing="0" width="1880px" id="data-grid00">
<thead>
<tr>
<th data-field="cfh">处方号</th>
<th>患者姓名</th>
<th>处方内容</th>
<th>问题标题</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>