本文主要功能是将Mongo数据库中的数据导入Excel文件。
1、导入依赖包
mongodb-driver-3.4.2.jar
mongodb-driver-core-3.4.2.jar
bson-3.4.2.jar
poi-3.10.1-20140818.jar
2、直接上代码
public class MongoToExcel {
private static Integer PORT = 27017; //端口号
private static String IP = "localhost"; //IP
private static String DATABASE = "database"; //数据库名
private static String USERNAME = "username"; //用户名
private static String PASSWORD = "password"; //密码
private static String COLLECTION = "test"; //文档
private static String ADDRESS = "d:\\workbook.xls"; //导出Excel文件的路径
public static void main(String[] args) {
try {
// IP,端口
ServerAddress serverAddress = new ServerAddress(IP, PORT);
List<ServerAddress> address = new ArrayList<ServerAddress>();
address.add(serverAddress);
// 用户名,数据库,密码
MongoCredential credential = MongoCredential.createCredential(USERNAME, DATABASE, PASSWORD.toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
// 通过验证获取连接
MongoClient mongoClient = new MongoClient(address, credentials);
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase(DATABASE);
// 连接文档
MongoCollection<Document> collection = mongoDatabase.getCollection(COLLECTION);
// 检索所有文档
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
List<Document> documents = new ArrayList<>();
while (mongoCursor.hasNext()) {
documents.add(mongoCursor.next());
}
// 表头
List<String> stringList = new ArrayList<>();
for (Entry<String, Object> entry : documents.get(0).entrySet()) {
stringList.add(entry.getKey());
}
// 创建HSSFWorkbook对象
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建HSSFSheet对象
HSSFSheet sheet = workbook.createSheet("sheet");
// Excel表头
HSSFRow row0 = sheet.createRow(0);
for (int i = 0; i < stringList.size(); i++) {
HSSFCell cell0 = row0.createCell(i);
cell0.setCellValue(stringList.get(i));
}
// Excel数据
for (int i = 0; i < documents.size(); i++) {
HSSFRow rows = sheet.createRow(i+1);
for (int j = 0; j < stringList.size(); j++) {
HSSFCell cells = rows.createCell(j);
cells.setCellValue(documents.get(i).get(stringList.get(j)).toString());
}
}
// 输出文件
FileOutputStream outputStream = new FileOutputStream(ADDRESS);
workbook.write(outputStream);
outputStream.flush();
System.out.println("导出成功");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}