[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")));
        }

posted on 2017-04-13 18:44  jerry liu  阅读(1493)  评论(0编辑  收藏  举报