MVC导出Excle
2016-01-03 14:51
197 查看
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application
();
app.Workbooks.Add();
Microsoft.Office.Interop.Excel.Worksheet sheet = app.ActiveSheet;
sheet.Cells[5, "B"] = "会员卡号";
sheet.Cells[5, "C"] = "姓名";
sheet.Cells[5, "D"] = "性别";
sheet.Cells[5, "E"] = "电话";
sheet.Cells[5, "F"] = "会员等级";
sheet.Cells[5, "G"] = "登记时间";
//sheet.get_Range(sheet.Cells[5, "E"]).set_ColumnWidth(60);
//sheet.get_Range(sheet.Cells[5, "G"]).set_ColumnWidth(60);
int rowIndex = 5;
for (int i = 0; i < dt.Rows.Count; i++)
{
rowIndex++;
sheet.Cells[rowIndex, "B"] = dt.Rows[i]["MC_CardID"].ToString();
sheet.Cells[rowIndex, "C"] = dt.Rows[i]["MC_Name"].ToString();
var Sex = "男";
if (int.Parse(dt.Rows[i]["MC_Sex"].ToString()) == 2)
{
Sex = "女";
}
sheet.Cells[rowIndex, "D"] = Sex;
sheet.Cells[rowIndex, "E"] = dt.Rows[i]["MC_Mobile"].ToString();
sheet.Cells[rowIndex, "F"] = dt.Rows[i]["CL_LevelName"].ToString();
sheet.Cells[rowIndex, "G"] = dt.Rows[i]["MC_CreateTime"].ToString();
}
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString
("yyyyMMddhhmmss") + ".xlsx");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
return Json(true, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return RedirectToAction("Index", "Error", e);
}
}
();
app.Workbooks.Add();
Microsoft.Office.Interop.Excel.Worksheet sheet = app.ActiveSheet;
sheet.Cells[5, "B"] = "会员卡号";
sheet.Cells[5, "C"] = "姓名";
sheet.Cells[5, "D"] = "性别";
sheet.Cells[5, "E"] = "电话";
sheet.Cells[5, "F"] = "会员等级";
sheet.Cells[5, "G"] = "登记时间";
//sheet.get_Range(sheet.Cells[5, "E"]).set_ColumnWidth(60);
//sheet.get_Range(sheet.Cells[5, "G"]).set_ColumnWidth(60);
int rowIndex = 5;
for (int i = 0; i < dt.Rows.Count; i++)
{
rowIndex++;
sheet.Cells[rowIndex, "B"] = dt.Rows[i]["MC_CardID"].ToString();
sheet.Cells[rowIndex, "C"] = dt.Rows[i]["MC_Name"].ToString();
var Sex = "男";
if (int.Parse(dt.Rows[i]["MC_Sex"].ToString()) == 2)
{
Sex = "女";
}
sheet.Cells[rowIndex, "D"] = Sex;
sheet.Cells[rowIndex, "E"] = dt.Rows[i]["MC_Mobile"].ToString();
sheet.Cells[rowIndex, "F"] = dt.Rows[i]["CL_LevelName"].ToString();
sheet.Cells[rowIndex, "G"] = dt.Rows[i]["MC_CreateTime"].ToString();
}
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString
("yyyyMMddhhmmss") + ".xlsx");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
return Json(true, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return RedirectToAction("Index", "Error", e);
}
}
相关文章推荐
- 一个可以用于在百科搜索大量关键词的python爬虫
- HDU 2602 Bone Collector(01背包)
- mysql 导入&导出sql文件
- 一致性Hash算法
- 云计算架构
- POJ2570 Fiber Network(Floyd)
- Swift开发IOS-UITextField
- 我们要科学不要激情
- 2016-2019三年计划
- 【费用流】hdu1533 poj2516 bzoj1070 bzoj1061
- 重拾博客
- ubuntu 14.04 安装openssh-server报错问题解决
- 【数学模型】椅子能在不平的地面上放平吗?(1)
- Hello 2016
- tomcate端口设定和服务器虚拟目录设定
- Apache源码安装
- ubuntu14.04 ssh允许root用户远程登录
- ubuntu14.04 ssh允许root用户远程登录
- IOS基础总结
- 从window.console&&console.log(123)浅谈JS的且运算逻辑(&&)