zoukankan      html  css  js  c++  java
  • 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")));
            }

  • 相关阅读:
    readonly const
    多线程的安全性
    【C++】operator运算符重载
    C++ 多态的实现及原理(转载)
    C语言宽字符处理函数对照表
    UTF8编码规则(转)
    PE文件结构(转)
    C语言中宏定义
    汇编语言移位指令
    数组指针应该结合二维数组多维数组来理解
  • 原文地址:https://www.cnblogs.com/sojastudio/p/6705467.html
Copyright © 2011-2022 走看看