【MVC】 页面导出 WORD, EXCEL
前端 js
function output() { var para = new Object(); para.html = getHtml("outputData"); para.type = "excel"; getAjaxText("/Test/SaveData", para, function (data) { if (data != "") { openWindow(stringFormat("/Test/Output?id={0}&type={1}&title={2}", data, para.type, "ceshi")); } }); }
后端 Action
public ActionResult Output(string id, string title, string type) { var byteArray = System.IO.File.ReadAllBytes("D:\\" + id + GetType(type)); return File(byteArray, "application/ms-" + type, title + GetType(type)); } [ValidateInput(false)] public ActionResult SaveData(string html, string type) { var guid = Guid.NewGuid(); System.IO.File.WriteAllText("D:\\" + guid + GetType(type), html); return Content(guid.ToString()); } private static string GetType(string type) { if (type.ToLower().Trim() == "word") { return ".doc"; } return ".xls"; }
原文:http://www.cnblogs.com/fzz2727551894/p/4922001.html