c# html 导出excel

[CustomAuthorize]
        public FileResult ExportCustomerManagerVisitExcel(string dateType, string realVisitDate, string isRenew)
        {
            string dateFormat = "yyyy-MM-dd";
            switch (dateType)
            {
                case "0": dateFormat = "yyyy-MM-dd"; break;
                case "1": dateFormat = "yyyy-MM"; break;
                case "2": dateFormat = "yyyy"; break;
                default: dateFormat = "yyyy-MM-dd"; break;
            }

            IList<CustomerManagerVisitStatistics> listTotal = orderitemManager.GetCustomerManagerVisitStatistics(dateFormat, realVisitDate, isRenew);

            StringWriter strWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);

            if (listTotal.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
                sb.Append("<thead>");
                sb.Append("<tr>");
                sb.Append("<th>组名</th>");
                sb.Append("<th>姓名</th>");
                sb.Append("<th>日期</th>");
                sb.Append("<th>回访总数</th>");
                sb.Append("<th>意向客户总数</th>");
                sb.Append("<th>意向客户占比(%)</th>");
                sb.Append("<th>小组回访总数</th>");
                sb.Append("<th>小组意向客户总数</th>");
                sb.Append("<th>小组平均值(%)</th>");
                sb.Append("</tr>");
                sb.Append("</thead>");
                sb.Append("<tbody>");

                string html = "";
                string groupFirstRow = "";
                string groupOtherRow = "";
                string displayGroupName = "";
                string groupName = "";
                int groupRowspan = 0;
                int groupVisitCount = 0;
                int groupIntentionCount = 0;

                foreach (CustomerManagerVisitStatistics item in listTotal)
                {
                    if (item.ParentId == "0")
                    {
                        if (groupRowspan > 0 && groupFirstRow != "")
                        {
                            groupFirstRow = "<tr><td rowspan='" + groupRowspan.ToString() + "'>" + displayGroupName + "</td>" + groupFirstRow + "<td rowspan='" + groupRowspan.ToString() + "'>" + groupVisitCount
                            + "</td><td rowspan='" + groupRowspan.ToString() + "'>" + groupIntentionCount + "</td><td rowspan='" + groupRowspan.ToString() + "'>";
                            if (groupVisitCount == 0)
                            {
                                groupFirstRow = groupFirstRow + "0</td></tr>";
                            }
                            else
                            {
                                groupFirstRow = groupFirstRow + Math.Round(decimal.Parse(((decimal)groupIntentionCount / groupVisitCount * 100).ToString()), 2).ToString() + "</td></tr>";
                            }

                            html = html + groupFirstRow + groupOtherRow;
                        }
                        groupName = item.GroupName;
                        displayGroupName = item.GroupName + "(" + item.CustomerManagerName + ")";
                        groupRowspan = 0;
                        groupVisitCount = 0;
                        groupIntentionCount = 0;
                        groupFirstRow = "";
                        groupOtherRow = "";
                    }
                    else
                    {
                        groupRowspan = groupRowspan + 1;
                        groupVisitCount = groupVisitCount + item.VisitCount;
                        groupIntentionCount = groupIntentionCount + item.IntentionCount;

                        if (groupRowspan == 1)
                        {
                            groupFirstRow = "<td>" + item.CustomerManagerName + "</td>" + "<td>" + item.RealVisitDate + "</td>" + "<td>" + item.VisitCount + "</td>" + "<td>" + item.IntentionCount + "<td>" + item.IntentionRate + "</td>";
                        }
                        else
                        {
                            groupOtherRow = groupOtherRow + "<tr><td>" + item.CustomerManagerName + "</td>" + "<td>" + item.RealVisitDate + "</td>" + "<td>" + item.VisitCount + "</td>" + "<td>" + item.IntentionCount + "<td>" + item.IntentionRate + "</td></tr>";
                        }

                    }

                }

                if (groupRowspan > 0 && groupFirstRow != "")
                {
                    groupFirstRow = "<tr><td rowspan='" + groupRowspan.ToString() + "'>" + displayGroupName + "</td>" + groupFirstRow + "<td rowspan='" + groupRowspan.ToString() + "'>" + groupVisitCount
                            + "</td><td rowspan='" + groupRowspan.ToString() + "'>" + groupIntentionCount + "</td><td rowspan='" + groupRowspan.ToString() + "'>";
                    if (groupVisitCount == 0)
                    {
                        groupFirstRow = groupFirstRow + "0</td></tr>";
                    }
                    else
                    {
                        groupFirstRow = groupFirstRow + Math.Round(decimal.Parse(((decimal)groupIntentionCount / groupVisitCount * 100).ToString()), 2).ToString() + "</td></tr>";
                    }

                    html = html + groupFirstRow + groupOtherRow;
                }

                sb.Append(html);
                sb.Append("</tbody>");
                sb.Append("</table>");

                strWriter.Write(sb.ToString());
            }
            else
            {
                strWriter.WriteLine("没有数据!");
            }

            byte[] fileContents = Encoding.GetEncoding("gb2312").GetBytes("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">" + strWriter.ToString() + "</body></html>");
            return File(fileContents, "application/vnd.ms-excel", string.Format("{0}.xls", "回访统计汇总" + DateTime.Now.Date.ToString("yyyyMMdd")));
        }

转载于:https://www.cnblogs.com/sojastudio/p/6705467.html

具体内容请参考我的BLOG:http://blog.youkuaiyun.com/smallwhiteyt/archive/2009/11/08/4784771.aspx 如果你耐心仔细看完本文,相信以后再遇到导出EXCLE操作的时候你会很顺手觉得SO EASY,主要给新手朋友们看的,老鸟可以直接飘过了,花了一晚上的时间写的很辛苦,如果觉得对你有帮助烦请留言支持一下,我会写更多基础的原创内容来回报大家。 C#导出数据到EXCEL表格是个老生常谈的问题了,写这篇文章主要是给和我一样的新手朋友提供两种导出EXCEL的方法并探讨一下导出的效率问题,本文中的代码直接就可用,其中部分代码参考其他的代码并做了修改,抛砖引玉,希望大家一起探讨,如有不对的地方还请大家多多包涵并指出来,我也是个新手,出错也是难免的。 首先先总结下自己知道的导出EXCEL表格的方法,大致有以下几种,有疏漏的请大家补充。 1.数据逐条逐条的写入EXCEL 2.通过OLEDB把EXCEL做为数据源来写 3.通过RANGE范围写入多行多列内存数据到EXCEL 4.利用系统剪贴板写入EXCEL 好了,我想这些方法已经足够完成我们要实现的功能了,方法不在多,在精,不是么?以上4中方法都可以实现导出EXCEL,方法1为最基础的方法,意思就是效率可能不是太高,当遇到数据量过大时所要付出的时间也是巨大的,后面3种方法都是第一种的衍生,在第一种方法效率低下的基础上改进的,这里主要就是一个效率问题了,当然如果你数据量都很小,我想4种方法就代码量和复杂程度来说第1种基本方法就可以了,或当你的硬件非常牛逼了,那再差的方法也可以高效的完成也没有探讨的实际意义了,呵呵说远了,本文主要是在不考虑硬件或同等硬件条件下单从软件角度出发探讨较好的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值