public static bool ReadAndWriteDataToExcel(this string filePath, bool isBC_QuanTrac) { bool response = true; using (SpreadsheetDocument spreadDocument = SpreadsheetDocument.Open(filePath, true)) { WorkbookPart workBookPart = spreadDocument.WorkbookPart; Workbook workbook = workBookPart.Workbook; using (var db = new Models.DbEntities()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { workBookPart.Workbook.Descendants <Sheet>().ForEach(sheet => { if (response) { WorksheetPart wsPart = (WorksheetPart)workBookPart.GetPartById(sheet.Id); SheetData sheetData = wsPart.Worksheet.GetFirstChild <SheetData>(); bool res = sheetData.WriteDatabase(sheet.Name, workBookPart, db, isBC_QuanTrac); if (!res) { response = false; } } }); if (response) { transaction.Commit(); } } catch (Exception ex) { transaction.Rollback(); response = false; } } } } return(response); }