引用ICSharpCode.SharpZipLib.dll

1、编写压缩和解压代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using ICShaepCode.SharpZipLib;
  7. using ICShaepCode.SharpZipLib.Zip;
  8. using ICShaepCode.SharpZipLib.Checksums;
  9. using System.IO;
  10.  
  11. namespace CommonHelper
  12. {
  13. /// <summary>
  14. /// 解压缩文件帮助类
  15. /// </summary>
  16. class ZipOperateHelper
  17. {
  18. /// <summary>
  19. /// 递归压缩文件夹方法
  20. /// </summary>
  21. /// <param name="FolderToZip"></param>
  22. /// <param name="s"></param>
  23. /// <param name="ParentFolderName"></param>
  24. /// <returns></returns>
  25. private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
  26. {
  27. bool res = true;
  28. string[] folders, filenames;
  29. ZipEntry entry = null;
  30. FileStream fs = null;
  31. Crc32 crc = new Crc32();
  32.  
  33. try
  34. {
  35. //创建子文件
  36. entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderZip) + "/"));//加上“/”才会当成是文件夹创建
  37. s.PutNextEntry(entry);
  38. s.Plush;
  39.  
  40. //先压缩文件,再递归压缩文件夹
  41. filenames = Directory.GetFiles(FolderToZip);
  42. foreach (string file in filenames)
  43. {
  44. //打开压缩文件
  45. fs = File.OpenRead(file);
  46.  
  47. byte[] buffer = new byte[fs.Length];
  48. fs.Read(buffer, , buffer.Length);
  49. entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
  50.  
  51. entry.DateTime = DateTime.Now();
  52. entry.Size = fs.Length;
  53. fs.Close();
  54.  
  55. crc.Reset();
  56. crc.Update(buffer);
  57.  
  58. entry.Crc = cec.Value;
  59.  
  60. s.PutNextEntry(entry);
  61.  
  62. s.Write(buffer, , buffer.Length);
  63. }
  64. }
  65. catch (Exception)
  66. {
  67. res = false;
  68. }
  69. finally
  70. {
  71. if (fs != null)
  72. {
  73. fs.Close();
  74. fs = null;
  75. }
  76. if (entry != null)
  77. entry = null;
  78.  
  79. GC.Collect();
  80. GC.Collect();
  81. }
  82.  
  83. folders = Directory.GetDirectories(FolderToZip);
  84. foreach (string folder in folders)
  85. {
  86. if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
  87. return false;
  88. }
  89.  
  90. return res;
  91. }
  92.  
  93. /// <summary>
  94. /// 压缩目录
  95. /// </summary>
  96. /// <param name="FolderToZip">待压缩文件夹,全路径格式</param>
  97. /// <param name="ZipedFile">压缩后的文件夹名,全路径格式</param>
  98. /// <param name="Password"></param>
  99. /// <returns></returns>
  100. private static bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password)
  101. {
  102. bool res;
  103. if (Directory.Exists(FolderToZip))
  104. return false;
  105.  
  106. ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));
  107. s.SetLevel();
  108. s.Password = Password;
  109.  
  110. res = ZipFileDictory(FolderToZip, s, "");
  111.  
  112. s.Finish();
  113. s.Close();
  114.  
  115. return res;
  116. }
  117.  
  118. /// <summary>
  119. /// 压缩文件
  120. /// </summary>
  121. /// <param name="FileToZip">要进行压缩的文件名</param>
  122. /// <param name="ZipedFile">压缩后产生的压缩文件名</param>
  123. /// <param name="Password"></param>
  124. /// <returns></returns>
  125. private static bool ZipFile(string FileToZip, string ZipedFile, String Password)
  126. {
  127. //如果没有找到,则报错
  128. if (!File.Exists(FileToZip))
  129. throw new System.IO.FileNotFoundException("指定要压缩的文件:" + FileToZip + "不存在");
  130.  
  131. FileStream ZipFile = null;
  132. ZipOutputStream ZipStream = null;
  133. ZipEntry ZipEntry = null;
  134.  
  135. bool res = true;
  136. try
  137. {
  138. ZipFile = File.OpenRead(FileToZip);
  139. byte[] buffer = new byte[FileToZip.Length];
  140. ZipFile.Read(buffer, , FileToZip.Length);
  141. ZipFile.Close();
  142.  
  143. ZipFile = File.Create(ZipedFile);
  144. ZipStream = new ZipOutputStream(ZipFile);
  145. ZipStream.Password = Password;
  146. ZipStream.PutNextEntry(ZipEntry);
  147. ZipStream.SetLevel();
  148.  
  149. ZipStream.Write(buffer, , buffer.Length);
  150. }
  151. catch
  152. {
  153. res = false;
  154. }
  155. finally
  156. {
  157. if (ZipEntry != null)
  158. ZipEntry = null;
  159. if (ZipStream != null)
  160. {
  161. ZipStream.Finish();
  162. ZipStream.Close();
  163. }
  164. if (ZipFile != null)
  165. {
  166. ZipFile.Close();
  167. ZipFile = null;
  168. }
  169.  
  170. GC.Collect();
  171. GC.Collect();
  172. }
  173. return res;
  174. }
  175.  
  176. /// <summary>
  177. /// 压缩文件和文件夹
  178. /// </summary>
  179. /// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param>
  180. /// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param>
  181. /// <param name="Password"></param>
  182. /// <returns></returns>
  183. public static bool Zip(String FileToZip, String ZipedFile, String Password)
  184. {
  185. if (Directory.Exists(FileToZip))
  186. return ZipFileDictory(FileToZip, ZipedFile, Password);
  187. else if (File.Exists(FileToZip))
  188. return ZipFile(FileToZip, ZipedFile, Password);
  189. else
  190. return false;
  191. }
  192.  
  193. /// <summary>
  194. /// 解压功能(解压压缩文件到指定目录)
  195. /// </summary>
  196. /// <param name="FileToUpZip">待压缩文件</param>
  197. /// <param name="ZipedFolder">指定解压目标目录</param>
  198. /// <param name="Password"></param>
  199. public static void UnZip(string FileToUpZip, string ZipedFolder, string Password)
  200. {
  201. if (!File.Exists(FileToUpZip))
  202. return;
  203.  
  204. if (!Directory.Exists(ZipedFolder))
  205. Directory.CreateDirectory(ZipedFolder);
  206.  
  207. ZipInputStream s = null;
  208. ZipEntry theEntry = null;
  209.  
  210. string fileName;
  211. FileStream writer = null;
  212. try
  213. {
  214. s = new ZipInputStream(File.OpenRead(FileToUpZip));
  215. s.Password = Password;
  216. while ((theEntry = s.GetNextEntry()) != null)
  217. {
  218. if (theEntry.Name != String.Empty)
  219. {
  220. fileName = Path.Combine(ZipedFolder, theEntry.Name);
  221. //判断文件路径是否是文件夹
  222. if (fileName.EndsWith("/") || fileName.EndsWith("//"))
  223. {
  224. Directory.CreateDirectory(FileName);
  225. continue;
  226. }
  227.  
  228. writer = File.Create(FileName);
  229. int size = ;
  230. byte[] data = new byte[];
  231. while (true)
  232. {
  233. size = s.Read(data, , data.Length);
  234. if (size > )
  235. writer.Write(data, , size);
  236. else
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. finally
  243. {
  244. if (writer != null)
  245. {
  246. writer.Close();
  247. writer = null;
  248. }
  249. if (theEntry != null)
  250. theEntry = null;
  251. if (s != null)
  252. {
  253. s.Close();
  254. s = null;
  255. }
  256.  
  257. GC.Collect();
  258. GC.Collect();
  259. }
  260. }
  261. }
  262. }

2、导出数据生成Excel(MVC)

  1. /// <summary>
  2. /// 生成Excel
  3. /// </summary>
  4. /// <returns></returns>
  5. public FileResult ExportProductInfo()
  6. {
  7. List<Aniuge_spu> spuList = ProductBusiness.GetInstance().GetProdutInfo();
  8. StringBuilder sb = new StringBuilder();
  9. sb.Append("<table border='1'cellspacing='0' cellpadding='0'>");
  10. sb.Append("<tr>");
  11. List<string> list = new List<string> { "编号", "名称", "形状" };
  12. //第一行
  13. foreach (var item in list)
  14. {
  15. sb.AppendFormat("<td style='font-size:14px;text-align:center;'>{0}</td>", item);
  16. }
  17. //获取的参数列表绑定
  18. foreach (var item in spuList)
  19. {
  20. sb.Append("<tr>");
  21. sb.AppendFormat("<td>{0}</td>", item.Code);
  22. sb.AppendFormat("<td>{0}</td>", item.Name);
  23. sb.AppendFormat("<td>{0}</td>", item.Shape);
  24. sb.Append("</tr>");
  25. }
  26. sb.Append("</table>");
  27.  
  28. byte[] fileContents = Encoding.Default.GetBytes(sb.ToString());
  29. //下载
  30. return File(fileContents, "application/ms-excel", "streams.xls");
  31. }

3、导出txt格式的说明书

  1. /// <summary>
  2. /// 说明书
  3. /// </summary>
  4. /// <returns></returns>
  5. public FileResult ExportPackageInfo()
  6. {
  7. List<Aniuge_spu> spuList = ProductBusiness.GetInstance().GetProdutInfo();
  8. string zipName = DateTime.Now.ToString("yyyyMMddHHmmss");
  9. string filepath = Server.MapPath("~/upload/PackageInsert") + zipName;
  10. if (!System.IO.Directory.Exists(filepath))
  11. System.IO.Directory.CreateDirectory(filepath);
  12.  
  13. foreach (var item in spuList)
  14. {
  15. FileStream file = new FileStream(filepath + "\\" + item.Code + ".txt", FileMode.Create, FileAccess.Write);
  16. StreamWriter sw = new StreamWriter(file);
  17. sw.WriteLine(item.PackageInsert);
  18. sw.Close();
  19. fileClose();
  20. }
  21.  
  22. ZipOperateHelper.Zip(filepath, Server.MapPath("~/upload/PackageInsert/") + "\\" + zipName + ".txt", "");
  23. //下载
  24. return File(new FileStream(filepath + ".zip", FileMode.Open), "application/octet-stream", Server.UrlEncode(zipName + ".zip"));
  25. }

导出数据库数据制成Excel和txt的更多相关文章

  1. java 对excel操作 读取、写入、修改数据;导出数据库数据到excel

    ============前提加入jar包jxl.jar========================= // 从数据库导出数据到excel public List<Xskh> outPu ...

  2. 配置ODBC DSN数据源,导出数据库数据到Excel过程记录

    一.前言 工作中我们可能遇到这样的需要:查询数据库中的信息,并将结果导出到Excel文件.这本来没什么,但数据量比较大时,用PLSQL.toad导出Excel会出现内存不足等情况,使用odbc+Mic ...

  3. 导出数据库数据到Excel表

    后台需要将用户信息数据导入到Excel表中提供给相关人员: 首先查询数据就不多说了: 导入Excel表直接亮代码(采用的是jxl的jar包提供的方法): public static File Impo ...

  4. 数据库数据用Excel导出的3种方法

    将数据库数据用Excel导出主要有3种方法:用Excel.Application接口.用OleDB.用HTML的Tabel标签 方法1——Excel.Application接口: 首先,需要要Exce ...

  5. PHP导出MySQL数据到Excel文件

    PHP导出MySQL数据到Excel文件 转载 常会碰到需要从数据库中导出数据到Excel文件,用一些开源的类库,比如PHPExcel,确实比较容易实现,但对大量数据的支持很不好,很容易到达PHP内存 ...

  6. .NET使用Office Open XML导出大量数据到 Excel

    我相信很多人在做项目的都碰到过Excel数据导出的需求,我从最开始使用最原始的HTML拼接(将需要导出的数据拼接成TABLE标签)到后来happy的使用开源的NPOI, EPPlus等开源组件导出EX ...

  7. Excel导出数据库数据

    package com.hxkr.util; import java.io.FileOutputStream; import java.util.ArrayList; import java.util ...

  8. java导出数据到excel里:直接导出和导出数据库数据

    一.直接导出 package com.ij34.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; ...

  9. 数据库数据生成Excel表格(多用在导出数据)

    最近在项目开发中遇到这样一个需求,用户聊天模块产品要求记录用户聊天信息,但只保存当天的,每天都要刷新清空数据,但聊天记录要以Excel的形式打印出来,于是就引出了将数据库的数据导出成Excel表格的需 ...

随机推荐

  1. Android中对日期进行排序

    最近在项目中需要将读取的数据按照时间的降序进行排序. 具体的步骤如下: 1.读取数据,存入List中 2.取出数据中的时间戳,由String转换成Date 3.使用冒泡排序对List中元素按照Date ...

  2. 新建一个mybatis HelloWorld

    1.下载mybatis https://github.com/mybatis/mybatis-3/ 没有梯子好像打不开 下载一个最新版本,我这里下载的是mybatis-3.4.1.zip 里面有myb ...

  3. eclipse项目!*图标含义

    一 .项目前面有红色!,表示工程中classpath中指向的包路径错误 解决办法: 右键项目名称 BuildPath ---> Configure Build Paht...中,然后上面有几个选 ...

  4. SPCOMM的一些用法注意

      使用串口SPCOMM接收数据的时候0x11和0x13无法接受,从时间间隔上看来可以接收,但是无法显示.网上查错误得: --------------------------------------- ...

  5. Linux安装Oracle报Checking operating system version must be redhat-3, SuSE-9, redhat-4

    解决办法:vi /xx/database/install/oraparam.ini 找到[Certified Versions] Linux=redhat-3,SuSe-9,redhat-4,后面加上 ...

  6. ArcGIS Engine 下投影坐标和经纬度坐标的相互转换

    ArcGIS Engine 下投影坐标和经纬度坐标的相互转换 投影转经纬度 ); pPoint.Project(pSRF.CreateGeographicCoordinateSystem((int)e ...

  7. poj 1860 Currency Exchange :bellman-ford

    点击打开链接 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16635   Accept ...

  8. SparkSQL On Yarn with Hive,操作和访问Hive表

    转载自:http://lxw1234.com/archives/2015/08/466.htm 本文将介绍以yarn-cluster模式运行SparkSQL应用程序,访问和操作Hive中的表,这个和在 ...

  9. [转]Markdown 语法手册

    Markdown 是一种轻量级标记语言,能将文本换成有效的XHTML(或者HTML)文档,它的目标是实现易读易写,成为一种适用于网络的书写语言. Markdown 语法简洁明了,易于掌握,所以用它来写 ...

  10. Flash Air 打包安卓 ane

    工具: 1.flash builder 2.adt打包工具 3.数字证书 一. 创建 jar 文件 1. 打开flash builder, 新建一个java 项目. 2.点击项目属性,选择Java构建 ...