1 /*************************************** 2 * @methodname→批量導出word文檔 3 * @createtime 2015-05-11 4 ***************************************/ 5 function AllExportWord(objid, tfile) { 6 var ckvalue = "", ntid = "", code = "", name = ""; 7 var chkbox = $("input[type=checkbox][name=chk]:checked"); 8 for (var i = 0; i < chkbox.length; i++) { 9 ckvalue = ckvalue + chkbox[i].value + ","; //主鍵值 10 ntid = ntid + $(chkbox[i]).attr("data-ntid") + ","; //學生主鍵 11 code = code + $(chkbox[i]).attr("data-code") + ","; //胸卡號 12 name = name + $(chkbox[i]).attr("data-name") + ","; //姓名 13 } 14 if (ckvalue != "") { 15 ntid = ntid.substring(0, ntid.length - 1); 16 code = code.substring(0, code.length - 1); 17 name = name.substring(0, name.length - 1); 18 $.Ajax({ 19 type: "POST", 20 url: "/NewStudent/BatchExportWord", 21 dataType: "json", 22 data: { tempFile: tfile, ntid: ntid }, 23 async: false, 24 success: function (data) { 25 if (data.isOK) { 26 var count = ntid.split(',').length; 27 if (count == data.ls.length && count > 0) { 28 DownLoadWord(objid, tfile, ntid, code, name, true); 29 } 30 else { 31 if (confirm("選擇導出的數據中包含沒有學習記錄的數據,是否確認導出或打印?")) { 32 DownLoadWord(objid, tfile, ntid, code, name, true); 33 } 34 } 35 } 36 else { 37 alert(data.msg); 38 } 39 } 40 }); 41 } 42 else { 43 alert("請選擇數據!"); 44 } 45 } 46 /*************************************** 47 * @methodname→批量導出word文檔 48 * @createtime 2015-05-11 49 ***************************************/ 50 function BatchExportWord(objid, tfile) { 51 var ckvalue = "", ntid = "", code = "", name = ""; 52 var chkbox = $("input[type=checkbox][name=chk]:checked"); 53 for (var i = 0; i < chkbox.length; i++) { 54 ckvalue = ckvalue + chkbox[i].value + ","; //主鍵值 55 ntid = $(chkbox[i]).attr("data-ntid"); //學生主鍵 56 code = $(chkbox[i]).attr("data-code"); //胸卡號 57 name = $(chkbox[i]).attr("data-name"); //姓名 58 } 59 if (ckvalue != "") { 60 ckvalue = ckvalue.substring(0, ckvalue.length - 1); //去掉最后一個逗號 61 $.ajax({ 62 type: "POST", 63 url: "/NewStudent/BatchExportWord", 64 dataType: "json", 65 data: { tempFile: tfile }, 66 async: false, 67 success: function (data) { 68 if (data.isOK) { 69 DownLoadWord(objid, tfile, ntid, code, name, false) 70 } 71 else { 72 alert(data.msg); 73 } 74 75 } 76 }); 77 } 78 else { 79 alert("請選擇數據!"); 80 } 81 } 82 /************************************************* 83 * @methodname→只能通過流的方式批量導出word文檔 84 在頁尾添加form,通過提交form表單才能下載 85 * @createtime 2015-05-12 86 *************************************************/ 87 function DownLoadWord(idName, tfile, ntid, code, name, isAll) { 88 $("body").find("form.#form1").remove(); 89 var form = "<form id='form1'></form>"; 90 var input = "<input />"; 91 var input1 = "", input2 = "", input3 = "", input4 = "", input5 = "", input6 = ""; 92 input1 = $(input); input1.attr("type", "hidden"); input1.attr("name", "tempFile"); input1.attr("value", tfile); 93 input2 = $(input); input2.attr("type", "hidden"); input2.attr("name", "ntid"); input2.attr("value", ntid); 94 input3 = $(input); input3.attr("type", "hidden"); input3.attr("name", "code"); input3.attr("value", code); 95 input4 = $(input); input4.attr("type", "hidden"); input4.attr("name", "name"); input4.attr("value", name); 96 input5 = $(input); input5.attr("type", "hidden"); input5.attr("name", "isAll"); input5.attr("value", isAll); 97 input6 = $(input); input6.attr("type", "hidden"); input6.attr("name", "idName"); input6.attr("value", idName); 98 $("body").append(form); //將表單放置在web中 99 //添加表單屬性100 $("#form1").attr("style", "display:none");101 $("#form1").attr("target", "");102 $("#form1").attr("method", "post"); 103 $("#form1").attr("action", "/NewStudent/DownLoadWord"); 104 105 //添加input到表單里106 $("#form1").append(input1)107 $("#form1").append(input1);108 $("#form1").append(input2);109 $("#form1").append(input3);110 $("#form1").append(input4);111 $("#form1").append(input5);112 $("#form1").append(input6);113 $("#form1").submit();114 }js
相關后臺代碼如下:
1 #region 壓縮文件及文件夾 2 /// <summary> 3 /// 壓縮文件及文件夾 4 /// </summary> 5 public class ComPRessFileZip 6 { 7 private ZipOutputStream zos = null; 8 private string strBaseDir = ""; 9 10 #region 臨時文件夾名稱 11 /// <summary> 12 /// 臨時文件夾名稱 13 /// </summary> 14 /// <param name="name">學生名稱</param> 15 /// <param name="value">學生卡號</param> 16 /// <returns></returns> 17 public string FolderName(string name, string value) 18 { 19 string tempName = ""; 20 if (!string.IsNullOrEmpty(name)) 21 { 22 tempName = name; 23 if (!string.IsNullOrEmpty(value)) 24 tempName += "_" + value; 25 tempName += "_" + DateTime.Now.ToString("yyyyMMddHHmm"); 26 } 27 else 28 { 29 tempName = DateTime.Now.ToString("yyyyMMddHHmmss"); //臨時文件夾名稱 30 } 31 return tempName; 32 } 33 #endregion 34 35 #region 創建臨時文件夾 36 /// <summary> 37 /// 創建臨時文件夾 38 /// </summary> 39 /// <param name="name">學生名稱</param> 40 /// <param name="value">學生卡號</param> 41 /// <returns></returns> 42 public string CreateTempFolder(string name, string value = null) 43 { 44 //遍歷服務器指定文件夾下的所有文件 45 string path = "UploadFile"; 46 string serverPath = WordFilePath.GetFilePath(path); 47 string tempName = ""; 48 if (!string.IsNullOrEmpty(value)) 49 tempName = FolderName(name, value); 50 else 51 tempName = name; 52 string tempFolder = Path.Combine(serverPath, tempName); 53 Directory.CreateDirectory(tempFolder); //創建臨時文件夾 54
新聞熱點
疑難解答