Example #1
0
        public void ImportData()
        {
            ExcelEngine excelEngine = new ExcelEngine();

            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2013;

            Stream stream = System.IO.File.Open(System.IO.Directory.GetCurrentDirectory() +
                                                "\\App_Data\\Template\\HealthFacilities.xlsx", FileMode.OpenOrCreate);

            IWorkbook workbook = application.Workbooks.Open(stream);

            IWorksheet           sheet    = workbook.Worksheets[0];
            int                  firstRow = sheet.UsedRange.Row;
            int                  lastRow  = sheet.UsedRange.LastRow + 1;
            int                  rows     = lastRow - firstRow;
            IList <TempFacility> data     = ExportDataFromExcelSheet(sheet, 2, 1, rows);

            _context.AddRange(data);
            //Saving the updated file
            _context.SaveChanges();

            workbook.Close();
            excelEngine.Dispose();
        }