下载zip格式文件(压缩Excel文件为zip格式)
Mongodb配置文件参考这一篇:http://www.cnblogs.com/byteworld/p/5913061.html
package util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sun.corba.se.spi.orbutil.fsm.Input; public class CreateExcel {
/**
* 把文件压缩到zip中
* @Description:
* @param
* @return void 返回类型
*/
public static void createZip(String dir,OutputStream out){
// 用户目录下的文件
File[] f = new File("E:/"+dir).listFiles();
// 创建zip文件
ZipArchiveOutputStream zipOut = null;
InputStream input = null;
try {
zipOut = new ZipArchiveOutputStream(out);
zipOut.setEncoding("UTF-8");
zipOut.setUseZip64(Zip64Mode.AsNeeded);
// 遍历目录下的文件
for(File file:f){
if (file != null) {
ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, file.getName());
zipOut.putArchiveEntry(zipEntry);
// 读取文件
input = new BufferedInputStream(new FileInputStream(file));
byte[] buff = new byte[1024];
int len = 0;
while((len = input.read(buff)) != -1){
zipOut.write(buff, 0, len);
}
zipOut.closeArchiveEntry();
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (zipOut != null) {
try {
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 创建Excel文件到本地
* @Description:
* @param
* @return void 返回类型
*/
public static void createExcel(String userid,String file,DBCursor cursor){
// 根据用户名创建文件夹
File dir = new File("E:/" + userid);
if (!dir.isDirectory()) {
dir.mkdirs();
}
Workbook book = new HSSFWorkbook();
// 获取标题
DBObject ob = cursor.toArray().get(0);
ArrayList<String> title = new ArrayList<>();
for(String key:ob.keySet()){
if (key.equals("_id")) {
continue;
}
title.add(key);
}
// 创建sheet
Sheet sheet = book.createSheet();
OutputStream out = null;
try {
// 写入标题栏
Row row = null;
// 标题栏的行数
Cell cell = null;
for(int i = 0;i< (cursor.count() + 1);i++){
// 标题栏
if (i == 0) {
row = sheet.createRow(i);
for (int j = 0; j < title.size(); j++) {
cell = row.createCell(j);
// 设置标题栏
cell.setCellValue(title.get(j));
}
continue;
}
// 写入数据
row = sheet.createRow(i);
out = new FileOutputStream(dir+"/"+file);
DBObject obj = null;
for (int j = 0; j < title.size(); j++) {
cell = row.createCell(j);
obj = cursor.toArray().get(j);
for(String key :obj.keySet()){
if (key.equals("_id")) {
continue;
}
if (key.equals(title.get(j))) {
cell.setCellValue((String)(obj.get(key)));
}
}
}
}
// 写入到excel
book.write(out);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post">
<a href="/Demo/SimpleDown"><h2>下载</h2></a>
</form>
</body>
</html>
Servlet:
package servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import com.mongodb.DBCollection; import util.CreateExcel;
import util.DBConn; /**
* 单独文件的zip下载
*/
@WebServlet("/SimpleDown")
public class SimpleDown extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public SimpleDown() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//DBCollection conn = DBConn.getConn();
DBCollection conn = DBConn.getConn();
CreateExcel.createExcel("User2323","weixin_data.xls", conn.find());
//createZip("User2323");
// CreateExcel.delZip("User2323");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+"User2323.zip");
CreateExcel.createZip("User2323", response.getOutputStream()); } }
下载zip格式文件(压缩Excel文件为zip格式)的更多相关文章
- 导入数据任务(id:373985)异常, 错误信息:解析导入文件错误,请检查导入文件内容,仅支持导入json格式数据及excel文件
小程序导入,别人导出的数据库json文件,错误信息如下: 导入数据库失败, Error: Poll error, 导入数据任务(id:373985)异常,错误信息:解析导入文件错误,请检查导入文件内容 ...
- CSV格式的文件与EXCEL文件的区别
CSV格式的文件与EXCEL文件的区别 Excel CSV 这是一个二进制文件,它保存有关工作簿中所有工作表的信息 CSV代表Comma Separated Values .这是一个纯文本格式,用逗号 ...
- Java读取txt文件、excel文件的方法
Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...
- Java实现文件压缩与解压[zip格式,gzip格式]
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例. zip扮演着归档和压缩两个角色:gzip并 ...
- PHP 实时生成并下载超大数据量的 Excel 文件
//另外由于excel数据是从数据库里逐步读出然后写入输出流的所以需要将PHP的执行时间设长一点 //(默认30秒)set_time_limit(0)不对PHP执行时间做限制. set_time_li ...
- PHP实时生成并下载超大数据量的EXCEL文件
最近接到一个需求,通过选择的时间段导出对应的用户访问日志到excel中, 由于用户量较大,经常会有导出50万加数据的情况.而常用的PHPexcel包需要把所有数据拿到后才能生成excel, 在面对生成 ...
- C# conn.open() 外部表不是预期的格式( 读取EXCEL文件出错)
环境:win7+iis7+Office2007 在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一 ...
- 下载放在resource下面的excel文件
1.将excel文件放项目resources目录下 2.打包的时候排除指定后缀文件,否则打包时会出现文件损坏的情况 <configuration> <encoding>UTF- ...
- C#仪器数据文件解析-Excel文件(xls、xlsx)
不少仪器工作站可以将数据导出为Excel文件,包括97-2003版本的xls文件和2007+的xlsx文件. 采集Excel文件相比采集pdf文件更容易.程序更健壮,毕竟Excel中数据有明确的行.列 ...
随机推荐
- Python基础、collections补充
collections collections是Python数据类型的补充,可以实现Counter计数.可命名元组(namedtuple).默认字典.有序字典.双向队列等功能 参考:http://py ...
- c# 相对路径的一些资料
1.获取和设置当前目录的完全限定路径. string str = System.Environment.CurrentDirectory; Result: C:\xxx\xxx 2.获取启动了应用程序 ...
- 使用回车键代替TAB键 需jquery1.4.2版本
1 $(document).ready(function () { 2 $(':input:text:first').focus(); 3 $(':input:enabled').addClass(' ...
- SQLite Expert 删除表数据并重置自动增长列
用下面的语句肯定是行不通的,语句不支持 truncate table t_Records 方法:1.删除表数据 2.重置自动增长列 where name='t_Records' /*name :是表名 ...
- jvm基础笔记
名词解释: 三类参数:标准参数(可能不会变的,java -help列出来的就是这类的),X参数(非标准化参数),XX参数(扩展参数). 所有XX 参数都以-XX开始,但后面出现的+-就不同了.+代表激 ...
- AJAX-----14HTML5中新增的API---files
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【转】DPM--对象检测开山之作
本文非原创,原文转载自:http://blog.csdn.net/ttransposition/article/details/12966521 DPM(Deformable Parts Model) ...
- 《zw版·Halcon-delphi系列原创教程》 酸奶自动分类脚本(机器学习、人工智能)
<zw版·Halcon-delphi系列原创教程>酸奶自动分类脚本(机器学习.人工智能) Halcon强大的图像处理能力,令人往往会忽视其内核,是更加彪悍的机器学习.人工智能. ...
- 2016年4月1日下午,《java入门123》翻开了第一页,从此走上不归路。新手初来乍到,献上见面礼
package copyfile; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream ...
- Extract Stylish styles and save as JSON format
Introduction Stylish is a easy browser extension/plugin for users to customizing the web page stylin ...