MVC 将视图页table导出成excel
前台代码:
<table class="tablelist" id="myTable">
<thead>
<tr>
<th colspan="1" rowspan="3">进出口标志</th>
<th colspan="3" rowspan="1">纠错项数</th>
<th colspan="10" rowspan="1">退补税额</th>
</tr>
<tr>
<th colspan="1" rowspan="2">涉及补税</th>
<th colspan="1" rowspan="2">涉及退税</th>
<th colspan="1" rowspan="2">无退补税</th>
<th colspan="5" rowspan="1">补税</th>
<th colspan="5" rowspan="1">退税</th>
</tr>
<tr>
<th colspan="1" rowspan="1">关税</th>
<th colspan="1" rowspan="1">增值税</th>
<th colspan="1" rowspan="1">消费税</th>
<th colspan="1" rowspan="1">反倾销税</th>
<th colspan="1" rowspan="1">协定关税</th>
<th colspan="1" rowspan="1">关税</th>
<th colspan="1" rowspan="1">增值税</th>
<th colspan="1" rowspan="1">消费税</th>
<th colspan="1" rowspan="1">反倾销税</th>
<th colspan="1" rowspan="1">协定关税</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td style="text-align:center;">@item.I_E_Flag</td>
<td>
@item.involSup
</td>
<td>
@item.involRef
</td>
<td>
@item.NoRefSup
</td>
<td>
@item.customsTariff_Sup
</td>
<td>
@item.addedValueTax_Sup
</td>
<td>
@item.ConsumptTax_Sup
</td>
<td>
@item.antiDumpingTax_Sup
</td>
<td>
@item.agreementTariff_Sup
</td>
<td>
@item.customsTariff_Ref
</td>
<td>
@item.addedValueTax_Ref
</td>
<td>
@item.ConsumptTax_Ref
</td>
<td>
@item.antiDumpingTax_Ref
</td>
<td>
@item.agreementTariff_Ref
</td>
</tr>
}
</tbody>
</table>
<script type="text/javascript">
$('.tablelist tbody tr:odd').addClass('odd');
AjaxStop();
</script>
@using (Html.BeginForm("ExportExcel", "ClassifyCorrectionRefSupTaxStatistial", new { name = "exportData", id = "exportData" })) { <li>@Html.Hidden("hHtml")</li> <li id="DownLoad"><label> </label><input name="btnsearch" id="btnDownload" type="submit" class="scbtn" onclick="exportExcel()" value="下载" /></li>}
JS代码:
<script > function exportExcel() { var html = document.getElementById("myTable").outerHTML; var shtml = htmlEncode(html); $("#output").val(shtml); $("input[name='hHtml']").val(shtml); //表单提交 $("#exportData").submit(); } function htmlEncode(value) { //create a in-memory div, set it's inner text(which jQuery automatically encodes) //then grab the encoded contents back out. The div never exists on the page. return $('<div/>').text(value).html(); } </script>
后台控制器代码:
/// <summary> /// 下载统计表数据 /// </summary> /// <param name="form"></param> /// <returns></returns> [HttpPost] public FileResult ExportExcel(FormCollection form) { //第一种:使用FileContentResult string content = Request.Form["hHtml"]; string strHtml = form["hHtml"]; strHtml = HttpUtility.HtmlDecode(strHtml);//Html解码 byte[] fileContents = Encoding.UTF8.GetBytes(strHtml); string filename = DateTime.Now.ToString("yyyyMMddHHmmss"); return File(fileContents, "application/ms-excel", "进出口退补税额统计表" + filename + ".xls"); //第二种:使用FileStreamResult var fileStream = new MemoryStream(fileContents); return File(fileStream, "application/ms-excel", "fileStream.xls"); //第三种:使用FilePathResult //服务器上首先必须要有这个Excel文件,然会通过Server.MapPath获取路径返回. var fileName = Server.MapPath("~/uploads/选题信息导入模板.xls"); return File(fileName, "application/ms-excel", "fileName.xls"); }
遇到的问题及解决方案:
1、中文字符变成乱码:
导出的Excel中文字符变成乱码,网上查询到可能是编码格式的问题,通过查看网页源码发现是“UTF-8”的格式。所以我一直认为解码的默认格式就是“UTF-8”,
所以在转成byte[] 流时就一直用的默认的编码方式byte[] fileContents = Encoding.Default.GetBytes(strHtml);
转码成GB2312的byte[]方式:byte[] buffer= Encoding.GetEncoding("GB2312").GetBytes(strHtml);
或者转成字符串:string str=Encoding.GetEncoding("GB2312").GetString(buffer);
2、IE8下文件名丢失。后缀名丢失。
ie不支持中文文件名输出。将文件名变成英文就可以了。