function exportExcel() { //查询条件 var startTime = F.ui.startTime.getText() == "" ? null : F.ui.startTime.getText(); var endTime = F.ui.endTime.getText() == "" ? null : F.ui.endTime.getText(); var major = F.ui.major.getText() == "" ? "" : F.ui.major.getText(); var Department = F.ui.Department.getText() == "" ? "" : F.ui.Department.getText(); var ReportPerson = F.ui.ReportPerson.getText() == "" ? "" : F.ui.ReportPerson.getText(); var RectPerson = F.ui.RectPerson.getText() == "" ? "" : F.ui.RectPerson.getText(); var data = "startTime=" + startTime + "&endTime=" + endTime + "&major=" + major + "&Department=" + Department + "&ReportPerson=" + ReportPerson + "&RectPerson=" + RectPerson; download('Export', data, 'post'); } function download(url, data, method) { if (url && data) { data = typeof data == 'string' ? data : jQuery.param(data); var inputs = ''; $.each(data.split('&'), function () { var pair = this.split('='); inputs += '<input type="hidden" name = "' + pair[0] + '" value ="' + pair[1] + '">'; }); $('<form action = "' + url + '" method ="' + (method || 'post') + '">' + inputs + '</form>').appendTo('body').submit().remove(); } }
后台接收方法添加NOPI引用
public FileStreamResult Export(HiddenDangerDto input) { int recordCount = 0; var list = HiddenDangerService.GetAll(input, 1, Int32.MaxValue, out recordCount); HSSFWorkbook book = ExportExcel.GridToExcelByNPOI<HiddenDangerDto>(list); System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); ms.Seek(0, System.IO.SeekOrigin.Begin); var cookie = new HttpCookie("time"); cookie.Values.Add("time", "1"); Response.AppendCookie(cookie); return File(ms, "application/vnd.ms-excel", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); }
类必须加[Display(Name = "")]
public class HiddenDangerDto { public int ID { get; set; } [Display(Name = "时间")] public DateTime Time { get; set; } [Display(Name = "专业")] public string major { get; set; } [Display(Name = "部门")] public string Department { get; set; } [Display(Name = "隐患上报人")] public string ReportPerson { get; set; } [Display(Name = "整改责任人")] public string RectPerson { get; set; } [Display(Name = "隐患描述")] public string Description { get; set; } [Display(Name = "整改措施")] public string RectMeasures { get; set; } }