从 excel 文件 c# 读取数据到 mongodb 数据库

read data from excel file c# to mongodb database

我的应用程序需要从 excel 文件中读取数据并将其存储在 MongoDB 数据库中。我正在为 development.I 使用 .Net 和 c#,我正在使用 Excel 2007、MongoDB 3.2 和 visual studio 2015 版本。 任何访问 excel 文件的想法,我需要你的帮助。

这是我的代码

 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);
}

有很多方法可以实现这一点。最简单的方法是将 Excel 保存为 CSV 文件以供进一步处理;您可以在 Excel 中执行此操作,方法是在 "File" 菜单中选择 "Save As",然后将文件结尾更改为 CSV。完成此操作后,您可以使用 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.