asp.net用zip方法批量导出txt
首先:
引用 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的更多相关文章
- 多表批量导出txt及打压缩包下载
在一些特殊的业务系统中,有些客户查看报表数据时不需要在浏览器上逐一查看,需要在页面端选择要查看的报表名称(可多选),选择条件,然后将所选中的报表批量导出到txt文件中并且要把批量导出的结果文件打 ...
- python批量导出导入MySQL用户的方法
这篇文章主要介绍了 数据库迁移(A -> B),需要把用户也迁移过去,而用户表(mysql.user)有上百个用户.有2种方法进行快速迁移: 1,在同版本的条件下,直接备份A服务器的mysql数 ...
- MVC批量导出数据方法
近段时间做了个数据平台,其中涉及到批量导出CSV格式数据的业务,主要使用了部分视图和视图之间传值等知识点,今天做了下整理,特此分享下: 主要分为四步: 1:要打印的数据格式陈列View: 2:自定义导 ...
- asp mvc 导出txt 文件泛型方法
asp mvc 导出txt 文件泛型方法分享: public static void ExportFile<T>(T obj) { StringBuilder str = new Stri ...
- ASP.Net MVC中数据库数据导出Excel,供HTTP下载(转)
转自http://www.cnblogs.com/hipo/archive/2012/03/13/2394019.html 一.关于下载 一般对下载权限有没有限制,或安全性要求不高的情况下,基于web ...
- C# 导出word文档及批量导出word文档(4)
接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...
- ASP.Net MVC中数据库数据导出Excel,供HTTP下载
本文来自:http://www.cnblogs.com/hipo/archive/2012/03/13/2394019.html 一.关于下载 一般对下载权限有没有限制,或安全性要求不高的情况下,基于 ...
- Max批量导出工具
Max批量导出工具 http://www.paulneale.com/scripts/batchItMax/batchItMax.htm Scripts Batch It Max: Batch It ...
- ATF批量导出工具
ATF批量导出工具 08 / 31, 2013 批量导出Atf的工具,使用是adobe atf 编码核心 先说一下关于atf的bug 当atf导出时候启用了mips选项会导致:如果纹理问长方形时上传会 ...
随机推荐
- ubuntu - 常用问题解决命令
查看本机网络通畅 - 网络是否可连接(远程链接时,检验防火墙) ping -c 10 localhost 查看本机进程/端口占用情况(看某一进程是否开启) netstat -tln 具体查看某一进程 ...
- Kruskal算法 克鲁斯卡尔
30行 #include <iostream> #include <algorithm> using namespace std; int f[5001],n,m,ans=0, ...
- SQL 模糊查询 可以正则匹配 (转)
1. % 表示任意0个或多个字符.如下语句:Select * FROM user Where name LIKE '%三%'; 将会把name为“张三”,“三脚猫”,“唐三藏”等等有“三”的全找出来. ...
- Codeforces Round #521 (Div. 3) C. Good Array
C. Good Array time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- react 什么是虚拟DOM?深入了解虚拟DOM
底层的理论基础 一. 原始生成步骤 1.state 数据 2.jsx 模版 3.数据 + 模板 结合,生成真实的DOM,来显示 4.state 发生改变了 5.数据 + 模板 结合,生成真实的DOM, ...
- CodeForces - 476B -Dreamoon and WiFi(DFS+概率思维)
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands throug ...
- MetricStatTimer
package org.apache.storm.metric.internal; import java.util.Timer; /** * Just holds a singleton metri ...
- Linux忘记roo密码的解决办法
Linux忘记root密码有三种解决办法: 下面详细介绍第一种: 重启系统后出现GRUB界面在引导装载程序菜单上,用上下方向键选择你忘记密码的那个系统键入“e” 来进入编辑模式. 接下来你可以看到 ...
- layer 弹出对话框 子父页面相互参数传递
转载:https://blog.csdn.net/flybridy/article/details/78610737
- 使用wget下载oracle jdk1.8
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com% ...