public FileResult ExportExcels(string StartTime, string EndTime, string SendType)
{

List<SMSSExportExcelsModel> list = new UserReturnSMSBLL().SMSStatistics(SendType, StartTime, EndTime);

byte[] fileContents = GetExcel(list);
return File(fileContents, "application/vnd.ms-excel", "测试名字" + DateTime.Now.ToString("yyyMMddhhmmssms") + ".xls");
}
public byte[] GetExcel(List<SMSSExportExcelsModel> list)
{
HSSFWorkbook hssfworkbook;
if (list.Count() > 0)
{
hssfworkbook = new HSSFWorkbook();
ISheet sheet = hssfworkbook.CreateSheet("用户回流");
sheet.SetColumnWidth(0, 20 * 200);//列宽
sheet.SetColumnWidth(1, 20 * 200);//列宽

ICellStyle style1 = hssfworkbook.CreateCellStyle();//样式
IFont font1 = hssfworkbook.CreateFont();//字体
font1.Color = HSSFColor.Black.Index;//字体颜色
font1.Boldweight = (short)FontBoldWeight.Bold;//字体加粗样式
style1.SetFont(font1);//样式里的字体设置具体的字体样式
style1.Alignment = HorizontalAlignment.Right;//文字水平对齐方式
style1.VerticalAlignment = VerticalAlignment.Center;//文字垂直对齐方式

IRow row = sheet.CreateRow(0);
row.Height = 400;//行高

var lstTitle = new List<string> {"推送手机号码", "类型", "奖品", "短信内容", "最后一次打开时间", "推送时间" };
for (int i = 0; i < lstTitle.Count; i++)
{
ICell cel0 = row.CreateCell(i);
cel0.SetCellValue(lstTitle[i].ToString());
cel0.CellStyle.Alignment = HorizontalAlignment.Right;//设置对齐
cel0.CellStyle = style1;//单元格式设置样式
}
int j = 1;
for (int i = 0; i < list.Count; i++)
{
IRow rows = sheet.CreateRow(j);
rows.Height = 330;//行高
j++;
ICell cell0 = rows.CreateCell(0);
cell0.SetCellValue(list[i].u_phone);
ICell cell1 = rows.CreateCell(1);
cell1.SetCellValue(list[i].u_push_days);
ICell cell2 = rows.CreateCell(2);
cell2.SetCellValue(list[i].u_prize);
ICell cell3 = rows.CreateCell(3);
cell3.SetCellValue(list[i].u_content);
ICell cell4 = rows.CreateCell(4);
cell4.SetCellValue(list[i].u_open_app_time.ToString());
ICell cell5 = rows.CreateCell(5);
cell5.SetCellValue(list[i].c_addtime.ToString());
}
MemoryStream stream = new MemoryStream();
hssfworkbook.Write(stream);
stream.Seek(0, SeekOrigin.Begin);
byte[] file = stream.ToArray();
stream.Close();
return file;
}
return null;
}

第二种 这种容易出现乱码

List<SMSSExportExcelsModel> list = new UserReturnSMSBLL().SMSStatistics(SendType, StartTime, EndTime);
#region

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 < list.Count; i++)
{
 sbHtml.Append("<tr>");
 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].u_phone);
 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].u_push_days);
 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].u_prize);
 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].u_content);
 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].u_open_app_time);
 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", list[i].c_addtime);
 sbHtml.Append("</tr>");
}
sbHtml.Append("</table>");

//第一种:使用FileContentResult
byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());
return File(fileContents, "application/vnd.ms-excel", "用户回流" + DateTime.Now.ToString("yyyMMddhhmmssms") + ".xls");

//第二种:使用FileStreamResult
////var fileStream = new MemoryStream(fileContents);
////return File(fileStream, "application/ms-excel", "fileStream.xls");
#endregion

04-17 00:49