My application needs to read data from an excel file and stocks it in MongoDB database. I am using .Net and c# for development.I am using Excel 2007 , MongoDB 3.2 and visual studio 2015 version. Any idea to access excel file, i need your help please.
This is my code
public void Open_readXLS()
{
Excel.Workbook workbook;
Excel.Worksheet worksheet;
Optioncontext ctx = new Optioncontext();
string filePath = @"C:\Users\user PC\Desktop\ finale\Euro_Dollar_Call_Options.xlsx";
workbook = new Excel.Workbook(filePath);
worksheet = workbook.Sheets.GetByName("Feuil1");
for (ushort i = 0; i <= worksheet.Rows.LastRow; i++)
{
option.type_option= worksheet.Rows[i].Cells[0].Value.ToString(),
option.type_currency= worksheet.Rows[i].Cells[1].Value.ToString();
}
ctx.Option.InsertOne(option);
}
There are many ways of achieving this. The simplest would be to save your Excel as a CSV-file for further processing; you can do this in Excel by selecting "Save As" in the "File" menu and then changing the file-ending to CSV. Once you have done this you can use mongoimport to import its contents - no need for C# code in this scenario. You may have to adjust the contents of your CSV so that it fits the structure that is expected by mongoimport; here is a SO post about just that How to use mongoimport to import csv.