<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="布局.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID ="btnExport" runat ="server" Text ="导出"
onclick="btnExport_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO; namespace 布局
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void btnExport_Click(object sender, EventArgs e)
{
try
{
List<FileInfo> fileList = new List<FileInfo>();
string fileName1 = @"D:\Export\新建 Microsoft Office Excel 工作表1.xlsx";
string fileName2 = @"D:\Export\新建 Microsoft Office Excel 工作表2.xlsx";
fileList.Add(new FileInfo(fileName1));
fileList.Add(new FileInfo(fileName2)); string fileName = "Export_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip";
//调用方法
string targetZipFilePath =Server.MapPath("/")+ "Export\\" + fileName;// 扩展名可随意
//压缩csv文件
FileCompression.Compress(fileList, targetZipFilePath, , ); //把压缩文件输出到浏览器
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "attachment; FileName=" + HttpUtility.UrlEncode(fileName));
Response.BinaryWrite(File2Bytes(targetZipFilePath));
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.End();//这句代码不能少,否则可能导致输出的文件数据不全,导致解压时出现压缩格式未知的错误 //删除产生的压缩文件
try
{
FileInfo fi1 = new FileInfo(targetZipFilePath);
fi1.Delete();
}
catch (Exception ex)
{ }
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "CreateExcelErrorTip", "alert('Export Failed!');", true);
return;
}
} public static byte[] File2Bytes(string path)
{
if (!System.IO.File.Exists(path))
{
return new byte[];
} FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length]; FileStream fs = fi.OpenRead();
fs.Read(buff, , Convert.ToInt32(fs.Length));
fs.Close(); return buff;
}
}
}

压缩用到的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Threading; namespace 布局
{
public class FileCompression
{
public FileCompression()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 加密、压缩文件 /// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileNames">要打包的文件列表</param>
/// <param name="GzipFileName">目标文件名</param>
/// <param name="CompressionLevel">压缩品质级别(0~9)</param>
/// <param name="SleepTimer">休眠时间(单位毫秒)</param>
public static void Compress(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, int SleepTimer)
{
ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
try
{
s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression
foreach (FileInfo file in fileNames)
{
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
}
catch
{ continue; }
// 方法二,将文件分批读入缓冲区
byte[] data = new byte[];
int size = ;
ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);
s.PutNextEntry(entry);
while (true)
{
size = fs.Read(data, , size);
if (size <= ) break;
s.Write(data, , size);
}
fs.Close();
file.Delete();
Thread.Sleep(SleepTimer);
}
}
finally
{
s.Finish();
s.Close();
}
}
#endregion #region 解密、解压缩文件
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="GzipFile">压缩包文件名</param>
/// <param name="targetPath">解压缩目标路径</param>
public static void Decompress(string GzipFile, string targetPath)
{
//string directoryName = Path.GetDirectoryName(targetPath + "\\") + "\\";
string directoryName = targetPath;
if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
string CurrentDirectory = directoryName;
byte[] data = new byte[];
int size = ;
ZipEntry theEntry = null;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
{
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{// 该结点是目录
if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
}
else
{
if (theEntry.Name != String.Empty)
{
//解压文件到指定的目录
using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
{
while (true)
{
size = s.Read(data, , data.Length);
if (size <= ) break; streamWriter.Write(data, , size);
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
#endregion
}
}

压缩DLL https://pan.baidu.com/s/1kIgRcvvzB5ZybHITsRi4ZQ

最新文章

  1. tomcat下jsp要加工程名后缀才能访问的问题解决
  2. paper 127:机器学习中的范数规则化之(二)核范数与规则项参数选择
  3. qt小问题
  4. 经典!HTML5 Canvas 模拟可撕裂布料效果
  5. Android画图Path的使用
  6. 2013 Multi-University Training Contest 8
  7. 本文使用springMVC和ajax,实现将JSON对象返回到页面
  8. 如何自动拼接 Update语句,仅Update已修改的字段
  9. Codeforces Round #208 (Div. 2)
  10. iOS程序员对算法的要求
  11. javaScript的2种变量范围有什么不同
  12. Web指纹识别目的Discuz识别+粗糙的版本演绎
  13. 备注: ubt 16.04 安装 gtx 1060 --- 成功运行 tensorflow - gpu
  14. SharePoint 2007 单列表模糊查询SPD定制
  15. 值得一看的35个Redis常用问题总结
  16. XSS Stored 测试
  17. 图论分支-Tarjan初步-点双连通分量
  18. css学习_css精灵技术、字体图标
  19. Linux常用命令(一)查看日志
  20. QT.Qt qmake报错(TypeError: Property &#39;asciify&#39; of object Core::Internal::UtilsJsExtension)

热门文章

  1. HIVE metastore Duplicate key name &#39;PCS_STATS_IDX&#39; (state=42000,code=1061)
  2. sql 时间转换格式 convert(varchar(10),字段名,转换格式)
  3. Ceph相关
  4. Sword libcurl回调函数相关知识
  5. 在Linux下使用gcc编译mesa文件报undefined reference to symbol &#39;sin@@GLIBC_2.2.5和DSO missing from command line两个错误的解决方案
  6. [ICPC 北京 2017 J题]HihoCoder 1636 Pangu and Stones
  7. Apache kylin的基础环境
  8. css 调转180度:transform: rotate(180deg);
  9. java的Io流学习
  10. Assignments---(贪心)