码迷,mamicode.com
首页 > Web开发 > 详细

mvc简单execl导出

时间:2016-01-05 18:14:43      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

直接上代码:

技术分享
public static byte[] GetExecl(DataTable dt, List<string> list)
        {
            var sbHtml = new StringBuilder();
            sbHtml.Append("<table border=‘1‘ cellspacing=‘0‘ cellpadding=‘0‘>");
            sbHtml.Append("<tr>");
            foreach (var item in list)
            {
                sbHtml.AppendFormat("<td style=‘font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;‘ height=‘25‘>{0}</td>", item);
            }
            sbHtml.Append("</tr>");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbHtml.Append("<tr>");
                for (int j = 0; j < list.Count; j++)
                {
                    sbHtml.AppendFormat("<td style=‘font-size: 12px;height:20px;‘>{0}</td>", dt.Rows[i][j].ToString());
                }
                sbHtml.Append("</tr>");
            }
            sbHtml.Append("</table>");
            byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());
            return fileContents;
        }
View Code

这样可以写成公共方法方便多次调用

技术分享
public FileResult ExportExcel(int page,int PageSize,string CusName="")
        {
            var orderby = "order by Createdate desc";
            var wherestr = "";
            //要导出的字段
            var tablefield = "ID,CusName,CusShort,Salesman,Industry,CusType,CusAdrss,Region,CusContact,"
                + "CusIphone,CusMobile,FinName,FinIphone,FinMail,FinFax,Qqnumber,BankAccount,"
                + "BankDeposit,TariffItem,TpayerDate,Contracttime,Remark,VerifierYes,"
                + "Verifier,Verifierdate,Creater,Createdate,Updater,Updatedate";
            if (CusName != "")
            {
                wherestr = "and CusName like ‘%" + CusName + "%‘";
            }
            //得到要导出的数据
            DataSet ds = DAL.SQLBasic.GetDateSetfengye(PageSize, page, tablename, wherestr, orderby, tablefield);
            //设置execl第一行标题
            List<string> list = new List<string> { "客户编号","客户名称","客户简称",
                "所属业务员","行业","客户类型","客户地址","区域","指定联系人",
                "联系人电话","联系人手机","财务员","财务员电话","财务员邮箱","财务员传真","财务员QQ","银行账号",
                "开户行","税号","成为纳税人时间","合同有效期","备注","是否审核",
                "审核人","审核时间","创建人","创建时间","修改人","修改时间"};
            byte[] fileContents = Utilitiy.ExcelHelper.GetExecl(ds.Tables[0], list);
            
            return File(fileContents, "application/ms-excel", "客户信息.xls");

        }
View Code

代码写的有点乱,请忽略

mvc简单execl导出

标签:

原文地址:http://www.cnblogs.com/zxayxh1314/p/5103193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!