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选项会导致:如果纹理问长方形时上传会 ...
随机推荐
- laravel5.2总结--返回值
基本概念和概述 1>StdClass 对象=>基础的对象 2>Eloquent 模型对象(Model 对象)=>和模型相关的类对象 3>Eloquent 集合=>可 ...
- 《图解HTTP》阅读笔记--第七章---确保WEB安全的HTTPS
第七章.确保WEB安全的HTTPSHTTP的缺点:通信使用明文(不加密),内容可能会被窃听 解决---加密处理: //将通信加密 :通过SSL(安全套接层)---HTTPS(超文本传输安全协议)--- ...
- P1080 国王游戏
题意: 让n 位大臣排成一排,国王站在队伍的最前面. 排好队后,所有的大臣都会获得国王奖赏的若干金币, 每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向 ...
- 洛谷2685 [TJOI2012]桥
[TJOI2012]桥 题目大意:给定一无向图,求删除一条边后1到n最短路的最大值,以及方案数. 做法:我们先从1为起点.从n为起点跑两边dij,获得每一个点到起点1.终点n的最短距离,其实距离和边权 ...
- 关于MVC模型的NSNotification用法
对于iOS开发开发者, Model View Controller 模型能帮你快速理清开发思路,最近在使用Model给Controller传递数据时候了解了关于 NSNotification的一些用法 ...
- vue.js路由学习笔记二
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- HashMap 1.8的源码分析三
线程安全问题: 在添加时候并没有进行安全考虑,枷锁 所以是线程不安全的,接下来进行代码测试; package com.mmall.concurrency.example.commonUnsafe; i ...
- python BeautifulSoup基本用法
#coding:utf-8 import os from bs4 import BeautifulSoup #jsp 路径 folderPath = "E:/whm/google/src_j ...
- python 编程基础-字典类型和方法(课后习题)
#创建一个字典 dic = {'k1':'v1','k2':'v2','k3':'v3'} #1.请循环遍历出所有的KEY for k in dic: print(k) #2请循环遍历出所有的valu ...
- python模块之openpyxl扩展
主要是对openpyxl扩展进行扩展,使用归类等 1. 安装 pip install openpyxl 想要在文件中插入图片文件,需要安装pillow,安装文件:PIL-fork-1.1.7.win- ...