文献种类:专题技术文献;
开发工具与关键技术:Visual Studio
作者: ;年级: ;撰写时间: 年 月 日
文献编号: 归档时间: 年 月 日
数据导出
开发工具与关键技术:Visual Studio
作者: 吴新洋
撰写时间:2019年6月2日
function ExportToExcel() {
//获取当前表格数据的筛选条件
var WineProjectID = $("#WineProjectID").val();
var Time = $("#Time").val();
//判断
if (WineProjectID == “” || WineProjectID == undefined) {
WineProjectID = 0;
}
if (Time == Time) {
Time = “”;
}
layer.confirm(“是否要导出当前表格中的数据,是请点击确定按钮,否则请筛选需要导出的数据!”, { icon: 3, title: “提示” }, function (layerIndex) {
layer.close(layerIndex);
window.open(“ExportToExcel?WineProjectID=” + WineProjectID + “&Time=” + Time);
});
}
public ActionResult ExportToExcel(string WineNameNum, string time)
{
List listGreensQk = (from tbGreensQk in myModels.S_GreensQk
join tbWineProject in myModels.S_WineProject on tbGreensQk.WineProjectID equals tbWineProject.WineProjectID
orderby tbGreensQk.GreensPresenterID descending
select new GreensQkVo {
WineNumber = tbWineProject.WineNumber,
WineName = tbWineProject.WineName,
Uniter = tbWineProject.Uniter,
WinePrice = tbWineProject.WinePrice,
Reason = tbGreensQk.Reason,
Counts = tbGreensQk.Counts,
Time = tbGreensQk.Time,
time = tbGreensQk.Time.ToString(),
}).ToList();
if (!string.IsNullOrEmpty(WineNameNum))
{
listGreensQk = listGreensQk.Where(m => m.WineName.Contains(WineNameNum) || m.WineNumber.Contains(WineNameNum)).ToList(); }
//1、创建工作簿
HSSFWorkbook exBook = new HSSFWorkbook();
//2、创建工作表
ISheet sheet = exBook.CreateSheet(“菜品赠送情况表”);
//3、设计表头
//3.1、创建表头
IRow headRow = sheet.CreateRow(0);
//3.2、设计表头字段
headRow.CreateCell(0).SetCellValue(“菜品编号”);
headRow.CreateCell(1).SetCellValue(“菜品名称”);
headRow.CreateCell(2).SetCellValue(“单位”);
headRow.CreateCell(3).SetCellValue(“价格”);
headRow.CreateCell(4).SetCellValue(“时间”);
headRow.CreateCell(5).SetCellValue(“原因”);
headRow.CreateCell(6).SetCellValue(“数量”);
//4、写入表格数据
for (int i = 0; i < listGreensQk.Count(); i++)
{
//4.1、创建行
IRow rowTemp = sheet.CreateRow(i + 1);
//4.2、写入数据
rowTemp.CreateCell(0).SetCellValue(listGreensQk[i].WineNumber);
rowTemp.CreateCell(1).SetCellValue(listGreensQk[i].WineName);
rowTemp.CreateCell(2).SetCellValue(listGreensQk[i].Uniter);
rowTemp.CreateCell(3).SetCellValue(listGreensQk[i].WinePrice.ToString());
rowTemp.CreateCell(4).SetCellValue(listGreensQk[i].time.ToString());
rowTemp.CreateCell(5).SetCellValue(listGreensQk[i].Reason);
rowTemp.CreateCell(6).SetCellValue(listGreensQk[i].Counts);
}
//5、将Excel文件转化为文件流输出
MemoryStream exStream = new MemoryStream();
exBook.Write(exStream);
//6、输出之前调用Seek(偏移量,游标位置)方法:确定流开始的位置
exStream.Seek(0, SeekOrigin.Begin);
//7、为下载的Excel文件命名
string exFileName = “学生安全教育测试成绩” + DateTime.Now.ToString(“yyyyMMddHHmmss”) + “.xls”;
//返回
return File(exStream, “application/vnd.ms-excel”, exFileName);
}