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")));
}
最新文章
- C和指针 第十二章 使用结构和指针 双链表和语句提炼
- if、if elif判断
- Windows 8 一起学习
- li标签包含img的问题
- Android studio 签名使用转
- finally 语句
- Java调用ASP.NET的webservice故障排除
- EF Code First教程-02.1 Fluent API约定配置
- IOS设置button 图片 文字 上下、左右
- PC-XP系统忘记密码怎么办
- 64位 CentOS NDK 编译 FFMPEG
- SendMessage API
- [转] GMT、UTC与24时区 等时间概念
- BZOJ 3207: 花神的嘲讽计划Ⅰ( hash + 可持久化线段树 )
- (一个)kafka-jstorm集群实时日志分析 它 ---------kafka实时日志处理
- mysql 学习心得5
- Zookeeper笔记3——原理及其安装使用
- 设置RHEL-7.0的运行级别
- <;input >; -- radio
- 从GitHub远程仓库中删除文件夹或文件