//存入mongdb中的实体类,用实体类赋值存储。
public class IDCard {
private String _id;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
private String card;
private String nativeplace;
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getNativeplace() {
return nativeplace;
}
public void setNativeplace(String nativeplace) {
this.nativeplace = nativeplace;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.dom4j.DocumentHelper;
import com.ctc.wstx.util.StringUtil;
import com.kingdeehit.mobile.his.utils.MongoDBHelper;
import com.mongodb.client.model.Filters;
import net.sf.json.JSONObject;
import net.sf.json.groovy.GJson;
public class readExcel {
MongoDBHelper smdbh = new MongoDBHelper("表名字");
public void readExcel(String filePath) throws Exception {
Map<String, String> map = new HashMap<String, String>();
String value = "";
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));
HSSFSheet sheet = wookbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();// hang
int max_cells = 0;// 列
// 获取最长的列,在实践中发现如果列中间有空值的话,那么读到空值的地方就停止了。所以我们需要取得最长的列
for (int i = 0; i < rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
if (max_cells < cells) {
max_cells = cells;
}
}
}
for (int i = 1; i < rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row != null) {
// 遍历列
for (int j = 0; j < max_cells; j++) {
HSSFCell cell = row.getCell(j);
if (cell == null) {
value += "NULL,";
}
if (cell != null) { // 如果不为空再设置值
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//假设单元格的值都为文本值,假如不全是文本值,这里需要使用switch()case: 来判断。
}
System.out.println(cell.getStringCellValue());
value = value + cell.getStringCellValue() + ",";
}
}
}
List<IDCard> list = new ArrayList();
String[] k = value.split(",");
for (int i = 0; i < k.length - 1; i += 2) {
IDCard idc = new IDCard();
idc.set_id(com.mobile.utils.StringUtil.generateUuid());//这个地方随机的设置一个ID,本人采用UUID的方法设置。
idc.setCard(k[i]);
idc.setNativeplace(k[i + 1]);
JSONObject json = JSONObject.fromObject(idc);//把生成的对象转为json格式
Document doc = Document.parse(json.toString());//使用DOM解析生成的json格式的字符串
smdbh.insert(doc);//mongdb中插入,如需看懂还需看到上一篇关于mongdbHelper类的描述。
//list.add(idc);
}
// for (IDCard s : list) {
// System.out.println(s.getCard()+";"+s.getNativeplace());
// }
}
//此方法是用来查询
public String selectCard(String card) {
Bson bson = Filters.and(Filters.eq("card", card));
List<Document> list = smdbh.query(bson);
IDCard idc2 = null;
for (Document doc : list) {
String json = doc.toJson();
JSONObject jsono = JSONObject.fromObject(json);
idc2 = (IDCard) JSONObject.toBean(jsono, IDCard.class);
}
return idc2.getNativeplace();
}
//本代码只考虑到单元格都为文本值的形式
public static void main(String[] args) throws Exception {
readExcel re = new readExcel();
re.readExcel("C:\\Users\\Administrator.WINDOWS-VK1LA9F\\Desktop\\身份证号码前6位行政区划与籍贯对应表.xls");
// sSystem.out.println(re.selectCard("131025"));
}
}