当前位置: 首页 > news >正文

.net 批量导出文件,以ZIP压缩方式导出

1. 首先Nuget ICSharpCode.SharpZipLib

 <script type="text/javascript">$(function () {$("#OutPutLink").click(function () {    // 单击下文件时$.ajax({         // 先判断条件时间内没有文件url: "/Home/ExistsFile?statTime=" + $("#statTime").val() + "&endTime=" + $("#endTime").val(),type: "Get",success: function (data) {if (data == "Exists") {    // 如果有   就下载window.location.href = "/Home/OutputFile?statTime=" + $("#statTime").val() + "&endTime=" + $("#endTime").val();}elsealert("该时间区域内无文件");}});});});</script>
 /// <summary>/// 验证该时间段是否有文件/// </summary>/// <param name="statTime">开始时间</param>/// <param name="endTime">结束时间</param>/// <returns></returns>public string ExistsFile(DateTime statTime, DateTime endTime){DateTime sTime = DateTime.Parse(statTime.ToString("yyyy-MM-dd") + " 00:00:00");DateTime eTime = DateTime.Parse(endTime.ToString("yyyy-MM-dd") + " 23:59:59");DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("~/Content/ExcelFile"));//遍历文件夹下的文件List<string> listFJName = new List<string>();//保存附件名字foreach (FileInfo NextFile in TheFolder.GetFiles()){string fileType = Path.GetExtension(NextFile.FullName).ToString().ToLower();if (fileType == ".xls" || fileType == ".xlsx"){string fileDate = NextFile.Name.Substring(0, 8);DateTime dtime = DateTime.ParseExact(fileDate, "yyyyMMdd", null);if (dtime >= sTime && dtime <= eTime){listFJName.Add(NextFile.Name);}}}if (listFJName.Count > 0)return "Exists";return "NotExists";}/// <summary>/// 执行将文件压缩下载/// </summary>/// <param name="statTime"></param>/// <param name="endTime"></param>public void OutputFile(DateTime statTime,DateTime endTime){DateTime sTime = DateTime.Parse(statTime.ToString("yyyy-MM-dd") + " 00:00:00");DateTime eTime = DateTime.Parse(endTime.ToString("yyyy-MM-dd") + " 23:59:59");DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("~/Content/ExcelFile"));//遍历文件夹下的文件List<string> listFJ = new List<string>();//保存附件路径List<string> listFJName = new List<string>();//保存附件名字foreach (FileInfo NextFile in TheFolder.GetFiles()){string fileType = Path.GetExtension(NextFile.FullName).ToString().ToLower();if (fileType == ".xls" || fileType == ".xlsx"){string fileDate = NextFile.Name.Substring(0, 8);DateTime dtime = DateTime.ParseExact(fileDate, "yyyyMMdd", null);if (dtime >= sTime && dtime <= eTime){listFJ.Add(Server.MapPath("~/Content/ExcelFile/") + NextFile.Name);listFJName.Add(NextFile.Name);}}}if (listFJ.Count > 0 && listFJName.Count > 0){string time = DateTime.Now.Ticks.ToString();ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("~/Content/ExcelFile/" + time + ".zip"), 9);//压缩文件DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("~/Content/ExcelFile/" + time + ".zip"));//下载文件}}/// <summary>/// 下载文件/// </summary>/// <param name="fileName"></param>/// <param name="filePath"></param>private void DownloadFile(string fileName, string filePath){FileInfo fileInfo = new FileInfo(filePath);Response.Clear();Response.ClearContent();Response.ClearHeaders();Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);Response.AddHeader("Content-Length", fileInfo.Length.ToString());Response.AddHeader("Content-Transfer-Encoding", "binary");Response.ContentType = "application/octet-stream";Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");Response.WriteFile(fileInfo.FullName);Response.Flush();System.IO.File.Delete(filePath);//删除已下载文件Response.End();}/// <summary>/// 压缩文件/// </summary>/// <param name="fileName">要压缩的所有文件(完全路径)</param>/// <param name="fileName">文件名称</param>/// <param name="name">压缩后文件路径</param>/// <param name="Level">压缩级别</param>public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level){ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(name));Crc32 crc = new Crc32();//压缩级别s.SetLevel(Level); // 0 - store only to 9 - means best compressiontry{int m = 0;foreach (string file in filenames){//打开压缩文件FileStream fs = System.IO.File.OpenRead(file);//文件地址byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);//建立压缩实体ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名//时间entry.DateTime = DateTime.Now;//空间大小entry.Size = fs.Length;fs.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;s.PutNextEntry(entry);s.Write(buffer, 0, buffer.Length);m++;}}catch{throw;}finally{s.Finish();s.Close();}}
http://www.lryc.cn/news/21076.html

相关文章:

  • 数据分析:某电商优惠卷数据分析
  • 性能测试流程
  • zookeeper集群的搭建,菜鸟升级大神必看
  • C语言之习题练习集
  • Buuctf [ACTF新生赛2020]Universe_final_answer 题解
  • 【Linux】环境变量
  • 单一职责原则
  • golangの并发编程(GMP模型)
  • MacBook Pro错误zsh: command not found: brew解决方法
  • spring中BeanFactory 和ApplicationContext
  • HC32L17x的LL驱动库之dma
  • SSM项目 替换为 SpringBoot
  • RL笔记:动态规划(2): 策略迭代
  • 2023软件测试金三银四常见的软件测试面试题-【测试理论篇】
  • 蓝桥训练第二周
  • 详讲函数知识
  • gin 框架初始教程文档
  • Maven的下载和安装【详细】
  • [数据结构]:04-循环队列(数组)(C语言实现)
  • buu [GWCTF 2019]BabyRSA 1
  • codeforces 1669F
  • 高数考试必备知识点
  • [蓝桥杯] 二分与前缀和习题练习
  • SpringMvc中HandlerAdapter组件的作用
  • FreeRTOS优先级翻转
  • 服务器部署—部署springboot之Linux服务器安装jdk和tomcat【建议收藏】
  • golang项目----家庭收支记账软件
  • 中国LNG市场投资机会研究
  • Elasticsearch:索引数据是如何完成的
  • 处理器管理