/// <summary>
/// 导出EXCEL
/// </summary>
/// <returns></returns>
public ActionResult ExportExcel(FormCollection form)
{
//var sbHtml = new StringBuilder();
//sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
//sbHtml.Append("<tr>");
//var lstTitle = new List<string> { "编号", "姓名", "年龄", "创建时间" };
//foreach (var item in lstTitle)
//{
// sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
//}
//sbHtml.Append("</tr>");
//for (int i = 0; i < 1000; i++)
//{
// sbHtml.Append("<tr>");
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", i);
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>屌丝{0}号</td>", i);
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", new Random().Next(20, 30) + i);
// sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", DateTime.Now);
// sbHtml.Append("</tr>");
//}
//sbHtml.Append("</table>");
//byte[] fileContents = Encoding.UTF8.GetBytes(sbHtml.ToString());
//var fileStream = new MemoryStream(fileContents);
//return File(fileStream, "application/ms-excel", "fileStream.xls");
//获取查询后条件的数据
var query = from s in iWorkOrderService.Search(c => true)
where
(string.IsNullOrEmpty(form["hid"]) ? true : s.Id == form["hid"]) &&
(string.IsNullOrEmpty(form["hState"]) ? true : s.State.Contains(form["hState"])) &&
(string.IsNullOrEmpty(form["hType"]) ? true : s.Type.Contains(form["hType"])) &&
(string.IsNullOrEmpty(form["hPhone"]) ? true : s.Phone.Contains(form["hPhone"])) &&
(string.IsNullOrEmpty(form["hService"]) ? true : s.Service.Contains(form["hService"])) &&
(string.IsNullOrEmpty(form["hProcessingMode"]) ? true : s.ProcessingMode.Contains(form["hProcessingMode"])) &&
(string.IsNullOrEmpty(form["hProductId"]) ? true : s.ProductId == int.Parse(form["hProductId"])) &&
(string.IsNullOrEmpty(form["hCstart"]) && string.IsNullOrEmpty(form["hCend"]) ? true : DateTime.Parse(s.CreateTime) >= DateTime.Parse(form["hCstart"]) && DateTime.Parse(s.CreateTime) <= DateTime.Parse(form["hCend"])) &&
(string.IsNullOrEmpty(form["hAstart"]) && string.IsNullOrEmpty(form["hAend"]) ? true : DateTime.Parse(s.ProcessingTime) >= DateTime.Parse(form["hAstart"]) && DateTime.Parse(s.ProcessingTime) <= DateTime.Parse(form["hAend"]))
select new
{
s.Id,
s.Type,
s.SIM,
s.Product.Name,
s.Terminal,
s.Service,
s.Quantity,
s.Company,
s.Contact,
s.Sex,
s.Phone,
s.ProblemDescription,
s.Attachment,
s.ProcessingMode,
s.SI,
s.Suggestion,
s.State
};
#region MyRegion
System.Web.UI.WebControls.DataGrid dgExport = null;
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
string filename = DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_"
+ DateTime.Now.Hour + "_" + DateTime.Now.Minute;
byte[] str = null;
// 设置编码和附件格式
curContext.Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
curContext.Response.ContentType = "application/vnd.ms-excel";
//System.Text.Encoding.UTF8;
// 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = query;// db.WorkOrders.ToList();
dgExport.AllowPaging = false;
dgExport.DataBind();
dgExport.RenderControl(htmlWriter);
// 返回客户端
str = System.Text.Encoding.UTF8.GetBytes(strWriter.ToString());
return File(str, "attachment;filename=" + filename + ".xls");
#endregion
}