客户端
function PostAndGetFileByUrl(url,type,postdata) { var temp; $.ajax({ url: url, type: type, data: {keyword:postdata}, async: true, dataType: "Text", success: function (data) { downloadFile(data); } }); return temp; } function btn_export() { var $gridList = $("#gridList"); var rowData = $gridList.jqGrid(‘getGridParam‘,‘selarrrow‘); PostAndGetFileByUrl("/RunManage/EnvData/WriteToExcel", "POST", rowData.join(",")); } <a id="NF-export" class="btn btn-primary" onclick="btn_export()"><i class="fa fa-plus"></i>导出XLS</a>
[HttpPost] [HandlerAjaxOnly] public ActionResult WriteToExcel(string keyword) { DataTable dt = new DataTable(); dt.Columns.Add("AAAA"); dt.Columns.Add("BBBB"); List<ItemEntity> dataList = EnvDataApp.GetListByKeywordArray(keyword);//请写自己的获取List方法 foreach (var item in dataList) { DataRow dr = dt.NewRow(); dr["AAAA"] = item.F_ID; dr["BBBB"] = item.F_FullName; dt.Rows.Add(dr); } NPOIExcel npoiexel = new NPOIExcel(); string fileDir = DateTime.Now.ToString("yyyyMMdd"); string fileName = "G" + Guid.NewGuid().ToString("N") + ".xls"; string destDir = Server.MapPath(@"/XlsTemp") + "\\" + fileDir + "\\"; if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); } npoiexel.ToExcel(dt, "数据", "Sheet1", destDir + fileName); return Content("/XlsTemp/" + fileDir + "/" + fileName); } }
时间: 2024-10-10 01:10:21