版本:Mongodb2.4.8

通过页面下载Excel文件

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/DownDemo"><h2>下载</h2></a>
</form>
</body>
</html>

  Mongodb配置文件dbconfig.properties:

url=localhost
port=27017
db=movie
table=mv

  连接Mongodb工具类:

package util;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.Properties;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class DBConn {
private static String url;
private static int port;
private static String db;
private static String table;
// 初始化加载
static{
//加载
Properties p = new Properties();
InputStream input = DBConn.class.getClassLoader().getResourceAsStream("dbconfig.properties");
try {
p.load(input);
url = p.getProperty("url");
port = Integer.valueOf(p.getProperty("port"));
db = p.getProperty("db");
table = p.getProperty("table");
} catch (IOException e) {
e.printStackTrace();
}finally{
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取连接mongodb
* @Description:
* @param @return
* @return DBCollection 返回类型
*/
public static DBCollection getConn(){
DBCollection conn = null;
try {
Mongo m = new Mongo(url,port);
DB d = m.getDB(db);
conn = d.getCollection(table);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
* @Description:
* @param @param m
* @return void 返回类型
*/
public static void getClose(Mongo m){
m.close();
}
}

  读取Mongodb数据写入到excel中:

public class WriteExcelUtils {
/**
* @Description:mongdb中读取数据写入到Excel
* @param title
* @param filename
* @param rownum
* @param cursor
* @return void 返回类型
*/
public static void Excel(DBCursor cursor,OutputStream out) {
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();
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);
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();
}
} }
}

  Servlet:

package servlet;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject; import util.DBConn;
import util.WriteExcelUtils; /**
* Servlet implementation class DownDemo
*/
@WebServlet("/DownDemo")
public class DownDemo extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public DownDemo() {
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
getMongodbExcle(request, response);
} /**
* 从mongdb中读取数据下载到客户端
* @Description:
* @param request
* @param response
* @return void 返回类型
*/
private void getMongodbExcle(HttpServletRequest request, HttpServletResponse response){
// 设置请求
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=data.xls");
// 从mongodb中读取数据
DBCollection conn = DBConn.getConn();
DBCursor cursor = conn.find();
OutputStream out = null;
try {
out = new BufferedOutputStream(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
WriteExcelUtils.Excel(cursor,out); }
}

  

在线读取Mongodb数据库下载EXCEL文件的更多相关文章

  1. SQLBulkCopy使用实例--读取Excel写入数据库/将 Excel 文件转成 DataTable

    MS SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表可以在不同服务器上). SqlBulkCopy 类允许编写提供类似功能的托管代码解决方 ...

  2. C# 之 下载EXCEL文件,自动用迅雷下载aspx

    在浏览器中导出 Excel 得时候,如果浏览器绑定了迅雷,则会下载aspx文件. 解决:下载EXCEL文件,自动用迅雷下载aspx if (Request.QueryString["id&q ...

  3. 前端下载excel文件功能的三种方法

    1 从后端接收json数据,前端处理生成excel下载 JsonExportExcel的github地址:https://github.com/cuikangjie/JsonExportExcel 这 ...

  4. 前端调用后端接口下载excel文件的几种方式

    今天有一个导出相应数据为excel表的需求.后端的接口返回一个数据流,一开始我用axios(ajax类库)调用接口,返回成功状态200,但是!但是浏览器没有自动下载excel表,当时觉得可能是ajax ...

  5. jsp下载excel文件

    jsp下载excel文件的的实现方法很多,今天也遇到这个问题,乱敲了一阵,终于搞定了,记下来和朋友们分享吧. 假设需要下载excel文件的jsp页面名为:down.jsp 对应的后台action名为: ...

  6. asp.net 下载EXCEL文件

    一.需要导入NPOI 库文件 打开VS2012 工具>>库程序包管理器>>管理解决方案的NuGet程序包,搜索NPOI,如下图 安装完成: 添加 using NPOI.HSSF ...

  7. c# 用OpenXmL读取.xlsx格式的Excel文件 返回DataTable

    1.须要引用的dll :  DocumentFormat.OpenXml.dll  ---须要安装一下OpenXml再引用 WindowsBase  ---直接在项目里加入引用 2.方法: /// & ...

  8. 读取FTP上的excel文件,并写入数据库

    今天遇到一些问题,需要从ftp上读取一些excel文件,并需要将excel中的数据写入到数据库,这样就可以通过管理页面查看这些数据. 我将相关工作分为三步,1.从ftp上读取相关文件,并将excel文 ...

  9. [转] 从数据库中读取图片并导入Excel文件,C#方式

    原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...

随机推荐

  1. map reduce filter

    三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str.   1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...

  2. swift + jj实践

    1,UIButton里面的字体和图片左对齐 button.imageEdgeInsets = UIEdgeInsetsMake(0,180/2,0.0,320/2) let btnRect = but ...

  3. mysql 常用操作指令

    (1)centos mysql数据库文件在哪? [root@localhost ~]# find / -name mysql (2)查找数据库备份工具 mysqldump [root@localhos ...

  4. Spark学习(一)--RDD操作

    标签(空格分隔): 学习笔记 Spark编程模型的两种抽象:RDD(Resilient Distributed Dataset)和两种共享变量(支持并行计算的广播变量和累加器). RDD RDD是一种 ...

  5. [CF355C]Vasya and Robot(思维,贪心)

    题目链接:http://codeforces.com/contest/355/problem/C 题意:1~n n个物品各重wi,现在有一个人可以从左边拿和从右边拿, 左边拿一个物品的花费是l*wi, ...

  6. 用iconv指令解决utf8和gb18030编码间转换

    Linux显示在Windows编辑过的中文就会显示乱码是由于两个操作系统使用的编码不同所致.Linux下使用的编码是utf8,而Windows使用的是gb18030.  解决方案:  在终端中,进入到 ...

  7. 加速chrome之Vimium快捷键

    使用Vimium一段时间,不能完全学习所有的快捷键.但是对这种简约,vim风格的设计还是非常敬佩. 下面是一些总结: Vimium快捷键 WIKI: 是一个开源的google chrome的扩展插件, ...

  8. .net 常用Response.ContentType

    来源:http://blog.csdn.net/navy235/article/details/5982319 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 ...

  9. ubuntu14.04LTS 下storm单机版安装配置

    1.下载storm 的安装文件 http://www.apache.org/dyn/closer.cgi/incubator/storm/apache-storm-0.9.2-incubating/a ...

  10. oracle的基本概念

    一·简介 1)数据库(DataBase) 用于存放数据,管理数据的存储仓库,是有效组织在一起的数据集合. 2)常用数据库软件 大型数据库:Oracle 中小型数据库:Mysql MySQL 3)RDB ...