我正在尝试使用java在mongodb中插入xlsx文件的内容,首先我解析了文件并将标头存储在地图中,然后我解析了其余部分并将其插入mongodb(document.put(加热器名称,值)) 这是我的代码:
public class CsvToBson { private static List map = new ArrayList();
public void parserResult(String outputs_reports) throws IOException {
try {
FileInputStream inputStream = new FileInputStream(new File(
outputs_reports));
// Create workbook instance holding reference to .xlsx file
Workbook workbook = new XSSFWorkbook(inputStream);
// get first/desired sheet from the workbook
Sheet sheet = workbook.getSheet("Feuil1");
int rownum = 0;
// Iterate through each row one by one
int i=0;
Row row = sheet.getRow(rownum++);
for (Cell cell : row)
{
map.add(row.getCell(i).getStringCellValue());
i++;
}
for ( String b: map){
System.out.println("entete : "+b);
}
} catch (IOException e) {
System.out.println("Error on read or open file !!!");
e.printStackTrace();
} catch (Exception e) {
System.out.println("Error in XSLSX Parser !!!");
e.printStackTrace();
}
}
public void parserResult2(String outputs_reports2, List<String> map, DBCollection collection) throws IOException {
try {
BasicDBObject document = new BasicDBObject();
FileInputStream inputStream = new FileInputStream(new File(
outputs_reports2));
// Create workbook instance holding reference to .xlsx file
Workbook workbook = new XSSFWorkbook(inputStream);
// get first/desired sheet from the workbook
Sheet sheet = workbook.getSheet("Feuil1");
int rownum = 1;
// Iterate through each row one by one
int i=1;
Row row2 = sheet.getRow(rownum++);
while (row2 != null) {
for (Cell cell : row2) {
DBObject object= new BasicDBObject();
switch ( cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
document.put(map.get(cell.getColumnIndex()), cell.getBooleanCellValue());
collection.insert(document);
break;
case Cell.CELL_TYPE_NUMERIC:
document.put(map.get(cell.getColumnIndex()), cell.getNumericCellValue());
collection.insert(document);
break;
case Cell.CELL_TYPE_STRING:
document.put(map.get(cell.getColumnIndex()), cell.getStringCellValue());
collection.insert(document);
break;
}
}
}
} catch (IOException e) {
System.out.println("Error on read or open file !!!");
e.printStackTrace();
} catch (Exception e) {
System.out.println("Error in XSLSX Parser !!!");
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
CsvToBson a = new CsvToBson();
String name= "Test1.xlsx";
a.parserResult(name);
DB db = (new MongoClient("localhost",27017)).getDB("mydb");
//get a single collection
DBCollection dbcollection = db.getCollection("Test");
a.parserResult2(name, map, dbcollection);
}
}
但我收到此错误消息:
Error in XSLSX Parser !!!
com.mongodb.DuplicateKeyException: Write failed with error code 11000 and error message 'E11000 duplicate key error collection: mydb.Test index: _id_ dup key: { : ObjectId('5733013034646e0b5030b9d2') }'
at com.mongodb.operation.BaseWriteOperation.convertBulkWriteException(BaseWriteOperation.java:236)
at com.mongodb.operation.BaseWriteOperation.access$300(BaseWriteOperation.java:60)
at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:146)
at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:133)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:133)
at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:60)
at com.mongodb.Mongo.execute(Mongo.java:781)
at com.mongodb.Mongo$2.execute(Mongo.java:764)
at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:333)
at com.mongodb.DBCollection.insert(DBCollection.java:328)
at com.mongodb.DBCollection.insert(DBCollection.java:319)
at com.mongodb.DBCollection.insert(DBCollection.java:289)
at com.mongodb.DBCollection.insert(DBCollection.java:255)
at com.mongodb.DBCollection.insert(DBCollection.java:192)
at Parser.CsvToBson.parserResult2(CsvToBson.java:93)
at Parser.CsvToBson.main(CsvToBson.java:128)