导出EXCEL是经常用到的功能,之前接触过,最近又再次用到了,就总结下!
将以下方法写在Action里就好。
public ActionResult OutExcel()
{
string whereStr = "";
whereStr += string.Format("***");
IList<LLM.Model.****> List = BLL.GetModelList(whereStr);
string strHtml = "<meta http-equiv='content-type' content='application/ms-excel; charset=GB2312'/><table><tr>";
strHtml += "<td>***</td>" + "<td>***</td>" + "<td>***</td>" + "<td>***</td></td></tr><tr>";
foreach (LLM.Model.T_Base_Order model in orderList)
{
strHtml += "<td>" + model.***+ "</td>";
strHtml += "<td>" + model.***+ "</td>";
strHtml += "<td>" + model.***+ "</td>";
strHtml += "</tr>";
}
strHtml += "</table>";
strHtml = HttpUtility.HtmlDecode(strHtml);//Html解码
byte[] b = System.Text.Encoding.Default.GetBytes(strHtml);//字串转byte阵列
string filename = "***.xls";//文件名字
return File(b, "application/ms-excel", filename);//输出档案给Client端
}
比较简单的比特流转换Excel方法,通过浏览器解码,简单好用。