首先:

引用 ICSharpCode.SharpZipLib.dll,百度下载

然后引用命名空间:

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

我自己的代码:

                      //取数据dt   
 
                      string path = Server.MapPath("~\\tempzt\\");
                      if (dt.Rows.Count > 0)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string wjmc = "xxxxxxxxxxxx".txt"; for (int n = 0; n < dt.Rows.Count; n++)
{
for (int m = 0; m < dt.Columns.Count; m++)
{
sb.Append(dt.Rows[n][m].ToString()); sb.Append("\t");
}
sb.Append("\r\n");
}
txt_export(sb, wjmc, path);
}
}
}
//zip下载
string[] files = Directory.GetFiles(path);
if (files.Length > 0)
{
string name = "xxxxxxxxxxxx.zip";
ZipFileMain(files, path + name, 9);
DownLoadZip(path + name, name);
}
else
{
Response.Write("<script>alert('没有目标文件!请先写入')</script>");
}
public void txt_export(StringBuilder sb, string wjmc, string path)
{
//zip下载
string txtPath = path + wjmc;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
} if (File.Exists(txtPath))
{
File.Delete(txtPath);
} StreamWriter sw = new StreamWriter(txtPath, false, System.Text.Encoding.Default);
sw.Write(sb.ToString());
sw.Close();
//zip下载
} //zip下载
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileName">要压缩的所有文件(完全路径)</param>
/// <param name="name">压缩后文件路径</param>
/// <param name="Level">压缩级别</param>
public void ZipFileMain(string[] filenames, string name, int Level)
{
if (File.Exists(name))
{
File.Delete(name);
}
ZipOutputStream s = new ZipOutputStream(File.Create(name));
Crc32 crc = new Crc32();
//压缩级别
s.SetLevel(Level); // 0 - store only to 9 - means best compression
try
{
foreach (string file in filenames)
{
if (!Path.GetExtension(file).Equals(".txt"))
{
continue;
}
//打开压缩文件
FileStream fs = File.OpenRead(file);//文件地址
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//建立压缩实体
ZipEntry entry = new ZipEntry(Path.GetFileName(file));//原文件名
//时间
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);
File.Delete(file);
}
}
catch
{
throw;
}
finally
{
s.Finish();
s.Close();
}
} public void DownLoadZip(string fileName,string name)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + name + "");
Response.TransmitFile(fileName);
}

asp.net用zip方法批量导出txt的更多相关文章

  1. 多表批量导出txt及打压缩包下载

     在一些特殊的业务系统中,有些客户查看报表数据时不需要在浏览器上逐一查看,需要在页面端选择要查看的报表名称(可多选),选择条件,然后将所选中的报表批量导出到txt文件中并且要把批量导出的结果文件打 ...

  2. python批量导出导入MySQL用户的方法

    这篇文章主要介绍了 数据库迁移(A -> B),需要把用户也迁移过去,而用户表(mysql.user)有上百个用户.有2种方法进行快速迁移: 1,在同版本的条件下,直接备份A服务器的mysql数 ...

  3. MVC批量导出数据方法

    近段时间做了个数据平台,其中涉及到批量导出CSV格式数据的业务,主要使用了部分视图和视图之间传值等知识点,今天做了下整理,特此分享下: 主要分为四步: 1:要打印的数据格式陈列View: 2:自定义导 ...

  4. asp mvc 导出txt 文件泛型方法

    asp mvc 导出txt 文件泛型方法分享: public static void ExportFile<T>(T obj) { StringBuilder str = new Stri ...

  5. ASP.Net MVC中数据库数据导出Excel,供HTTP下载(转)

    转自http://www.cnblogs.com/hipo/archive/2012/03/13/2394019.html 一.关于下载 一般对下载权限有没有限制,或安全性要求不高的情况下,基于web ...

  6. C# 导出word文档及批量导出word文档(4)

          接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...

  7. ASP.Net MVC中数据库数据导出Excel,供HTTP下载

    本文来自:http://www.cnblogs.com/hipo/archive/2012/03/13/2394019.html 一.关于下载 一般对下载权限有没有限制,或安全性要求不高的情况下,基于 ...

  8. Max批量导出工具

    Max批量导出工具 http://www.paulneale.com/scripts/batchItMax/batchItMax.htm Scripts Batch It Max: Batch It ...

  9. ATF批量导出工具

    ATF批量导出工具 08 / 31, 2013 批量导出Atf的工具,使用是adobe atf 编码核心 先说一下关于atf的bug 当atf导出时候启用了mips选项会导致:如果纹理问长方形时上传会 ...

随机推荐

  1. recommonmark

    一 简要介绍 recommonmark是个到commonMark文档的兼容性桥,那么什么是commonMark是什么的呢?CommonMark是规范版的markdown,下边是部分commonmark ...

  2. ubuntu - 14.04,如何让从托盘消失的输入法图标再次显示出来?

    ubuntu14.04,我也不知道怎么搞的,突然输入法图标就从托盘上消失了,这可真太不方便了,不知道自己当前是否正在使用输入法,怎么能让输入法图标再次显示在托盘上? 解决办法:确保你的“系统设置”中有 ...

  3. MapReduce Kmeans算法含测试数据

    使用时,需要修改K值,args值 运行流程: 先初始化中心点->map中和距离最近的中心点生成一对传入reduce->reduce中把相同key值的存到一起->更新中心点,计算和上一 ...

  4. redis-淘汰策略

    将redis用作缓存时,如果内存空间用满,就会自动驱逐老的数据.默认情况下,memcached就是这种方式. LRU是Redis唯一支持的回收算法. maxmemory配置指令 maxmemory用于 ...

  5. 贪心+DP【洛谷P4823】 [TJOI2013]拯救小矮人

    P4823 [TJOI2013]拯救小矮人 题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以 ...

  6. ArcGIS-各类问题

    arcgis 10.4破解方法*注意!Desktop,Engine,Server必须为同一版本 1.先安装License10.4 2.再安装Desktop10.4 3.再安装Engine10.4 4. ...

  7. arcgis图片文件

  8. [TJOI2013]循环格 费用流 BZOJ3171

    题目背景 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为(0,0).给定一个起始位(r,c),你可以沿着箭头方向在格子间行走.即:如果 ...

  9. php字符串中去除html标签

    strip_tags() 函数剥去字符串中的 HTML.XML 以及 PHP 的标签.

  10. Spring注解实现定时功能以及Quartz定时器

    一:Spring注解实现--------->Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz 1:maven配置: <!-- quartz--> <d ...