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

  • 相关阅读:
    Java之修改文件内容:字符串逐行替换
    Java之"instanceof"和"isInstance"代码举例
    spring之跨模块引用配置文件
    Java之相对路径找不到文件问题解决方法
    html 页面模块的常用命名
    移动端资源集锦
    手机web不同屏幕字体大小高度自适应
    meta标签整理
    css文字超出自动显示省略号
    display:inline、block、inline-block的区别
  • 原文地址:https://www.cnblogs.com/sojastudio/p/6705467.html
Copyright © 2020-2023  润新知