public ActionResult Export(string 查詢條件_名稱, string 查詢條件_分類) { var data = repo.GetExcelData(查詢條件_名稱, 查詢條件_分類); if (data == null) { return(Json(new { IsSuccess = false, Message = "無資料可匯出" }, JsonRequestBehavior.AllowGet)); } using (var workbook = new XLWorkbook()) using (var worksheet = workbook.Worksheets.Add("Index")) using (var memoryStream = new MemoryStream()) { Guid guid = Guid.NewGuid(); worksheet.Range("A1", "A7").Merge(); worksheet.Cell("A2").Value = repo.All(); workbook.SaveAs(memoryStream); memoryStream.Position = 0; TempData[guid.ToString()] = memoryStream.ToArray(); if (HttpContext.Request.IsAjaxRequest()) { return(Json(new { IsSuccess = false, Message = "無資料可匯出", @Guid = guid.ToString() }, JsonRequestBehavior.AllowGet)); } else { return(Export(guid.ToString())); } } }