您的位置:首页 > 编程语言 > C#

C#导出Excel

2015-08-01 15:05 453 查看

导出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方法,通过浏览器解码,简单好用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  导出 Excel C#