package com.lnt.mongodb;
import java.io.File;
import java.net.UnknownHostException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class ParseExcel
{
public static void main(String[] args) throws UnknownHostException, MongoException
{
File inputWorkbook = new File("Your Excel File Name");//Extension should be .xls
Workbook wrkBk;
//For local mongodb server you can mention both "localhost" and "127.0.0.1"
//For remote mongodb server you can mention directly remote system ipaddress
Mongo mongo = new Mongo( "127.0.0.1" );
DB db = mongo.getDB( "Your DB Name");
DBCollection collection = db.getCollection("Your Collection Name");
try
{
wrkBk = Workbook.getWorkbook(inputWorkbook);
for(int sheetno=0;sheetno<Number of sheets in excel ;sheetno++)
{
Sheet workSheet = wrkBk.getSheet(sheetno);
for(int i=1; i<workSheet.getRows(); i++)
{
//here you can get values from excel
Cell cell = null;
cell = workSheet.getCell(0, i);
String psno = cell.getContents();
BasicDBObject doc = new BasicDBObject();
//if you mention _id ,then it will generate user primary key
//otherwise it will automatically generate one primary key
doc.put("_id", id);
//insert value in collection
doc.put("collection field name",which value you what to insert);
For example :doc.put("username",mongo);
doc.put("userid",1);
collection.insert(doc);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.io.File;
import java.net.UnknownHostException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class ParseExcel
{
public static void main(String[] args) throws UnknownHostException, MongoException
{
File inputWorkbook = new File("Your Excel File Name");//Extension should be .xls
Workbook wrkBk;
//For local mongodb server you can mention both "localhost" and "127.0.0.1"
//For remote mongodb server you can mention directly remote system ipaddress
Mongo mongo = new Mongo( "127.0.0.1" );
DB db = mongo.getDB( "Your DB Name");
DBCollection collection = db.getCollection("Your Collection Name");
try
{
wrkBk = Workbook.getWorkbook(inputWorkbook);
for(int sheetno=0;sheetno<Number of sheets in excel ;sheetno++)
{
Sheet workSheet = wrkBk.getSheet(sheetno);
for(int i=1; i<workSheet.getRows(); i++)
{
//here you can get values from excel
Cell cell = null;
cell = workSheet.getCell(0, i);
String psno = cell.getContents();
BasicDBObject doc = new BasicDBObject();
//if you mention _id ,then it will generate user primary key
//otherwise it will automatically generate one primary key
doc.put("_id", id);
//insert value in collection
doc.put("collection field name",which value you what to insert);
For example :doc.put("username",mongo);
doc.put("userid",1);
collection.insert(doc);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment