ExcelDAO.java
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.rivercloud.dao;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.rivercloud.utils.ReadExcelUtils;
import org.bson.Document;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.*;
/**
* Created by lq on 2016/12/15/0015.
*/
public class ExcelDAO {
MongoClient client = new MongoClient( "localhost" , 27017 );
MongoDatabase db = client.getDatabase("test");
private MongoCollection collection = null;
public void insert(String filename,File file){
if (db.getCollection(filename) ==null ){
db.createCollection(filename);
}
collection = db.getCollection(filename);
ReadExcelUtils excelReader = new ReadExcelUtils(file);
Map<Integer, Object[]> map = null;
try {
map = excelReader.readExcelContent();
} catch (Exception e) {
e.printStackTrace();
}
Object[] title = map.get(0);//得到excel的列标题
for (int i = 0;i < map.size();i++ ){
Document document = new Document();
for (int j = 0;j < title.length;j++){
if (title[j].toString() != null){
document.append(title[j].toString(),map.get(i)[j]);
}
}
collection.insertOne(document);
}
}
public Object[] getCollectionNames(){
List list = new ArrayList();
MongoIterable<String> collectionNames = db.listCollectionNames();
Iterator it = collectionNames.iterator();
while (it.hasNext()){
list.add(it.next());
}
Object[] CollectionNames = new Object[list.size()];
for (int i = 0;i < list.size();i++){
CollectionNames[i] = list.get(i);
}
return CollectionNames;
}
public Map find(String filename) {
collection = db.getCollection(filename);
FindIterable iterable = collection.find();
Iterator it = iterable.iterator();
Set set = null;
List<Document> dou = new ArrayList();
while (it.hasNext()) {
Document document = (Document) it.next();
set = document.keySet();
dou.add(document);
}
List data = new ArrayList();
for (Object str : set) {
if (!"_id".equals(str)) {
data.add(str);
}
}
Object[] title = new Object[data.size()];
for (int i = 0; i < data.size(); i++) {
title[i] = data.get(i);
}
Map<Integer, Object[]> map = new HashMap<>();
for (int i = 0; i < dou.size(); i++) {
Object[] cellValue = new Object[title.length];
for (int j = 0; j < title.length; j++) {
//System.out.println(dou.get(i).get(title[j]));
cellValue[j] = dou.get(i).get(title[j]);
}
map.put(0, title);
map.put(i, cellValue);
}
return map;
}
public void updata(Map<Integer,String[]> map,String filename){
db.getCollection(filename).drop();
db.createCollection(filename);
collection = db.getCollection(filename);
String[] title = map.get(0);//得到excel的列标题
for (int i = 0;i < map.size();i++ ){
Document document = new Document();
for (int j = 0;j < title.length;j++){
if (title[j].toString() != null){
document.append(title[j],map.get(i)[j]);
System.out.println(title[j].toString()+"===="+map.get(i)[j]);
}
}
collection.insertOne(document);
}
}
public void delete(String filename){
collection = db.getCollection(filename);
collection.drop();
}
}