·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> C# 导出word文档及批量导出word文档(4)
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