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格式)的更多相关文章

  1. 导入数据任务(id:373985)异常, 错误信息:解析导入文件错误,请检查导入文件内容,仅支持导入json格式数据及excel文件

    小程序导入,别人导出的数据库json文件,错误信息如下: 导入数据库失败, Error: Poll error, 导入数据任务(id:373985)异常,错误信息:解析导入文件错误,请检查导入文件内容 ...

  2. CSV格式的文件与EXCEL文件的区别

    CSV格式的文件与EXCEL文件的区别 Excel CSV 这是一个二进制文件,它保存有关工作簿中所有工作表的信息 CSV代表Comma Separated Values .这是一个纯文本格式,用逗号 ...

  3. Java读取txt文件、excel文件的方法

    Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...

  4. Java实现文件压缩与解压[zip格式,gzip格式]

    Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例. zip扮演着归档和压缩两个角色:gzip并 ...

  5. PHP 实时生成并下载超大数据量的 Excel 文件

    //另外由于excel数据是从数据库里逐步读出然后写入输出流的所以需要将PHP的执行时间设长一点 //(默认30秒)set_time_limit(0)不对PHP执行时间做限制. set_time_li ...

  6. PHP实时生成并下载超大数据量的EXCEL文件

    最近接到一个需求,通过选择的时间段导出对应的用户访问日志到excel中, 由于用户量较大,经常会有导出50万加数据的情况.而常用的PHPexcel包需要把所有数据拿到后才能生成excel, 在面对生成 ...

  7. C# conn.open() 外部表不是预期的格式( 读取EXCEL文件出错)

    环境:win7+iis7+Office2007 在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一 ...

  8. 下载放在resource下面的excel文件

    1.将excel文件放项目resources目录下 2.打包的时候排除指定后缀文件,否则打包时会出现文件损坏的情况 <configuration> <encoding>UTF- ...

  9. C#仪器数据文件解析-Excel文件(xls、xlsx)

    不少仪器工作站可以将数据导出为Excel文件,包括97-2003版本的xls文件和2007+的xlsx文件. 采集Excel文件相比采集pdf文件更容易.程序更健壮,毕竟Excel中数据有明确的行.列 ...

随机推荐

  1. prototype

  2. Python常用模块之sys

    Python常用模块之sys sys模块提供了一系列有关Python运行环境的变量和函数. 常见用法 sys.argv 可以用sys.argv获取当前正在执行的命令行参数的参数列表(list). 变量 ...

  3. c#中浅拷贝和深拷贝的理解

    c#中拷贝有浅拷贝和深拷贝之分. 例如对象A,其中有值类型字段和引用类型字段: 1.浅拷贝: 对于值类型字段,直接逐位复制到新拷贝的副本对象中,修改副本的字段的值,不会影响源对象中字段的值: 对于引用 ...

  4. LeetCode Burst Balloons

    原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...

  5. C语言 str2bin 和 bin2str 实现

    需求介绍 在编码或者调试过程中经常需要进行 字节码转换为 十六进制的字符串, 或者将 十六进制字符串 转换为 字节码的需求. 即:  字节码 (内存中存储的 01 串):    11111111 &l ...

  6. Linux的硬链接为何不能链接目录

    Linux中的目录文件是特殊的文件,其中的数据是一个关联列表的,像c++中的map,或者Python中的dict,保存每个文件名(包括子目录,Linux中一切皆文件!)到iNode的映射.iNode本 ...

  7. Sie sind das Essen und wir sind die Jaeger!

    WCF  http://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.html HTTP 数据库分库分表 读写分离 负载均衡 wind ...

  8. 夺命雷公狗-----React---10--组建嵌套进行数据遍历

    先写一个组建... 然后进行嵌套.. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  9. HAL驱动库学习-SPI

    如何使用SPI库1 声明SPI hanlde, 例如: SPI_HandleTypeDef hspi2 通过实现HAL_SPI_MspInit()函数初始化底层资源 以下两个必须进行初始化 a 使能s ...

  10. C#中把Datatable转换为Json的5个代码实例

    一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table" ...