大致的方式沒有差異 都是在後臺進行整合 DataTable =>foreach =>下載
現在有兩種下載方式
1.利用MVC裏面的 return File() 進行下載 (如果數據多久執行反饋會慢或者是卡死)
public ActionResult ExploreExcel(string TimeBeg, string TimeEnd, int page = 1)
{
var list = new PayStatBLL().GetPayStat(TimeBeg, Convert.ToDateTime(TimeEnd).AddDays(1).ToString(), page, pagesize, ref counts).ToPagedList<PayStat>(page, pagesize);
StringBuilder sbhtml = new StringBuilder();
sbhtml.Append(" <table border='1' cellspacing='0' cellpadding='0'>");
sbhtml.Append("<tr><td>日期</th><th>網銀</th><th>ios</th><th>支付寶</th><th>微信</th><th>人氣</th><th>每天合計</th><th>人均</th></tr>");
foreach (var item in list)
{
sbhtml.AppendFormat("<tr><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td> </tr>"
, item.TimeDay, item.WangYin, item.ios, item.zfb, item.Weixin, item.Ren, item.RenAvg, item.SumPay);
}
sbhtml.Append("</table>");
string html = sbhtml.ToString();
byte[] fileContents = Encoding.UTF8.GetBytes(html);//一定要加上 避免亂碼
return File(fileContents, "application/ms-excel", DateTime.Now.ToString("yyMMddHHssyy") + "充值記錄.xls");
}
2.利用 先打包後下載(注意刪除服務器中的文件 )
using Excel = Microsoft.Office.Interop.Excel; //注意引入
DateTime time = DateTime.Now;
//表格的頂層
Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//創建Excel工作薄
Excel.Workbook workbook = excel.Workbooks.Add(Server.MapPath("~/Excel/員工列表模板.xlsx"));
//引用Excel工作簿
excel.Visible = true;
//創建Excel工作表
Excel.Worksheet sheet = workbook.Worksheets[1] as Excel.Worksheet;
//創建Excel工作表名稱
sheet.Name = "員工信息表";
var list = db.Employees.Include((e) => e.Department).ToList();
sheet.Cells[1, 2] = time.ToShortDateString() + "_員工列表";
//標頭單位信息
sheet.Cells[3, 3] = list.Count;
sheet.Cells[3, 5] = list.Where(e => e.E_Sex == "男").Count();
sheet.Cells[3, 7] = list.Where(e => e.E_Sex == "女").Count();
int i = 6;
//遍歷泛型集合中的所有記錄,把數據填充到單元格里面去
foreach (var item in list)
{
sheet.Cells[i, 2] = item.E_ID;
sheet.Cells[i, 3] = item.E_Name;
sheet.Cells[i, 4] = item.E_Sex;
sheet.Cells[i, 5] = item.E_Age;
sheet.Cells[i, 6] = "'" + item.E_Tel;//注意加這個的意思是讓電話在Excel裏爲文本,而不是數字,不然顯示不好看
sheet.Cells[i, 7] = item.Department.D_Name;
i += 1;
}
string path = "~/Excel/員工列表_" + time.ToString("yyyy-MM-dd_hhmmss") + ".xlsx";
//先把文件保存到網站的Excel文件夾裏面,然後再下載到客戶端來
workbook.SaveAs(Server.MapPath(path), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
workbook.Close(Missing.Value, Missing.Value, Missing.Value);
excel.Quit();
//保存好後實現文件的下載
FileInfo loadFile = new FileInfo(Server.MapPath(path));
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(loadFile.Name, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", loadFile.Length.ToString());
Response.WriteFile(loadFile.FullName);
Response.Flush();
//下載完文件後將excel文件夾中的臨時文件刪除
System.IO.File.Delete(Server.MapPath(path));
//釋放進程
GC.Collect();
return RedirectToAction("Index");
一個頁面處理只有一個action 多個按鈕時就需要採用 @Html.ActionLink(‘文本’,’actionName’,’ControllerName’);
或者是異步 但是當你用異步時 會執行後臺的方法 但是就是不返回下載(自己沒研究透徹的問題)
解決方法之一:
function aa() {
window.location.href = "/paystat/ExploreExcel/?TimeBeg=" + $("#TimeBeg").val() + "&TimeEnd=" + $("#TimeEnd").val() + "&page=" + $("#page").text();
}
至於怎麼調用這個方法都行 隨你用 a 標籤阻止跳轉 還是用button 的onclick 都行