public IHttpActionResult ExportExcel(int Id) { _db = new ERPContext(); var emp = _db.Employees; var dailydetails = _db.DailyFileDetailses.Where(x => x.DailyFile.DailyId == Id) .GroupBy(p => p.EmployeeId) .Select(g => new DailyDetailsInfoVM { Id = g.Key, Name = emp.FirstOrDefault(x => x.Id == g.Key).Name, Code = emp.FirstOrDefault(x => x.Id == g.Key).Code, Net = g.Sum(i => i.Net) }).ToList(); try { string filePath = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output.xls"); if (!IsFileLocked(new FileInfo(filePath)) && !IsFileLocked(new FileInfo(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output2.xls").ToString()))) { File.Copy(filePath, HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output2.xls").ToString(), true); } else { return(Ok(new { errorMsg = "الملف مفتوح من قبل برنامج اخر" })); } filePath = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output2.xls"); string filePath2 = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/ATM.xls"); BL bl = new BL(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + filePath2 + "; Extended Properties = Excel 12.0"); DataTable dt = new DataTable(); dt = bl.GetTable("select * from [Sheet1$]"); dt.PrimaryKey = new DataColumn[] { dt.Columns[4] }; BL b2 = new BL(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + filePath + "; Extended Properties = Excel 12.0"); DataTable dt2 = new DataTable(); foreach (var row in dailydetails) { // if(row.Code.ToString()== oSheet.Rows.Find(row.Code)) var found = dt.Rows.Find(row.Code).ItemArray; b2.Insert(string.Format("insert into [Sheet1$] values('{0}','{1}','{2}','{3}','{4}','{5}',{6})", found[0], found[1], found[2], found[3], found[4], found[5], row.Net)); } return(Ok(new { result = filePath })); } catch (Exception ex) { return(Ok(new { errorMsg = ex.Message })); } }
public IHttpActionResult UpdateEmpInfo(int DailyFileId, int EmployeeId, decimal Net) { var dailyfile = _db.DailyFiles.Find(DailyFileId); var empnum = _db.DailyFileDetailses.Count(x => x.DailyFileId == DailyFileId); if (dailyfile != null) { dailyfile.DailyFileDetailses = new List <DailyFileDetails>() { new DailyFileDetails() { DailyFileId = DailyFileId, EmployeeId = EmployeeId, Net = Net } }; dailyfile.FileTotalAmount += Net; dailyfile.EmployeesNumber = empnum + 1; _db.SaveChanges(); var root = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/ATM.xls"); string con1 = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + root + ";Extended Properties='Excel 12.0 Xml; HDR = YES; IMEX = 1';"); string con2 = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dailyfile.FilePath + ";Extended Properties='Excel 12.0 Xml; HDR = YES; IMEX = 1';"); //string con2 = // رقم الموظف بجهته الأصلية //string con2 = (@"provider=microsoft.ace.oledb.12.0;data source=c:\file\bank.xls;extended properties='excel 12.0 xml; hdr = yes; imex = 1';"); var code = _db.Employees.Find(EmployeeId).Code; BL bl = new BL(con1); DataTable dtw = bl.GetTable(String.Format("select * from [Sheet1$] where [{0}] = '" + code.ToString() + "'", "رقم الموظف بجهته الأصلية")); BL bl2 = new BL(con2); DataTable dtw2 = bl2.GetTable(String.Format("select * from [Sheet1$] where [{0}] = '" + code.ToString() + "'", "رقم الموظف بجهته الأصلية")); if (dtw2.Rows.Count == 0) { bl2.Insert(String.Format("insert into [Sheet1$] values ('{0}','{1}','{2}','{3}','{4}','{5}',{6})" , dtw.Rows[0].ItemArray[0] , dtw.Rows[0].ItemArray[1] , dtw.Rows[0].ItemArray[2] , dtw.Rows[0].ItemArray[3] , dtw.Rows[0].ItemArray[4] , dtw.Rows[0].ItemArray[5] , Net )); } else { bl2.update(String.Format("update [Sheet1$] set[{1}] =[{1}]+" + Net + " where [{0}] = '" + code.ToString() + "'", "رقم الموظف بجهته الأصلية", "المرتب")); } /// 49 23706.61--145 73,984.58 //if (EmpInfo != null) //{ // _db = new ERPContext(); // var model = _db.DailyFileDetailses.Find(EmpInfo.Id); // model.Net = EmpInfo.Net; // _db.SaveChanges(); //} return(Ok()); } return(BadRequest("Wrong Request")); }
public IHttpActionResult ExportExcel(int Id) { _db = new ERPContext(); var emp = _db.Employees; var dailydetails = _db.DailyFileDetailses.Where(x => x.DailyFile.DailyId == Id) .GroupBy(p => p.EmployeeId) .Select(g => new DailyDetailsInfoVM { Id = g.Key, Name = emp.FirstOrDefault(x => x.Id == g.Key).Name, Code = emp.FirstOrDefault(x => x.Id == g.Key).Code, Net = g.Sum(i => i.Net) }).ToList(); try { string filePath = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output.xls"); if(!IsFileLocked(new FileInfo(filePath)) && !IsFileLocked(new FileInfo(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output2.xls").ToString()))) { File.Copy(filePath, HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output2.xls").ToString(), true); } else { return Ok(new { errorMsg = "الملف مفتوح من قبل برنامج اخر" }); } filePath = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/output2.xls"); string filePath2 = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/ATM.xls"); BL bl = new BL(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + filePath2 + "; Extended Properties = Excel 12.0"); DataTable dt = new DataTable(); dt = bl.GetTable("select * from [Sheet1$]"); dt.PrimaryKey = new DataColumn[] { dt.Columns[4] }; BL b2 = new BL(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + filePath + "; Extended Properties = Excel 12.0"); DataTable dt2 = new DataTable(); foreach (var row in dailydetails) { // if(row.Code.ToString()== oSheet.Rows.Find(row.Code)) var found = dt.Rows.Find(row.Code).ItemArray; b2.Insert(string.Format("insert into [Sheet1$] values('{0}','{1}','{2}','{3}','{4}','{5}',{6})", found[0], found[1], found[2], found[3], found[4], found[5], row.Net)); } return Ok(new { result = filePath }); } catch (Exception ex) { return Ok(new {errorMsg = ex.Message}); } }
public IHttpActionResult UpdateEmpInfo(int DailyFileId, int EmployeeId, decimal Net) { var dailyfile = _db.DailyFiles.Find(DailyFileId); var empnum = _db.DailyFileDetailses.Count(x => x.DailyFileId == DailyFileId); if (dailyfile != null) { dailyfile.DailyFileDetailses = new List<DailyFileDetails>() { new DailyFileDetails() { DailyFileId = DailyFileId, EmployeeId = EmployeeId, Net = Net } }; dailyfile.FileTotalAmount += Net; dailyfile.EmployeesNumber = empnum + 1; _db.SaveChanges(); var root = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/ATM.xls"); string con1 = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + root + ";Extended Properties='Excel 12.0 Xml; HDR = YES; IMEX = 1';"); string con2 = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dailyfile.FilePath + ";Extended Properties='Excel 12.0 Xml; HDR = YES; IMEX = 1';"); //string con2 = // رقم الموظف بجهته الأصلية //string con2 = (@"provider=microsoft.ace.oledb.12.0;data source=c:\file\bank.xls;extended properties='excel 12.0 xml; hdr = yes; imex = 1';"); var code = _db.Employees.Find(EmployeeId).Code; BL bl = new BL(con1); DataTable dtw = bl.GetTable(String.Format("select * from [Sheet1$] where [{0}] = '" +code.ToString()+"'", "رقم الموظف بجهته الأصلية")); BL bl2 = new BL(con2); DataTable dtw2= bl2.GetTable(String.Format("select * from [Sheet1$] where [{0}] = '" + code.ToString() + "'", "رقم الموظف بجهته الأصلية")); if(dtw2.Rows.Count==0) { bl2.Insert(String.Format("insert into [Sheet1$] values ('{0}','{1}','{2}','{3}','{4}','{5}',{6})" , dtw.Rows[0].ItemArray[0] , dtw.Rows[0].ItemArray[1] , dtw.Rows[0].ItemArray[2] , dtw.Rows[0].ItemArray[3] , dtw.Rows[0].ItemArray[4] , dtw.Rows[0].ItemArray[5] ,Net )); } else { bl2.update(String.Format("update [Sheet1$] set[{1}] =[{1}]+" + Net + " where [{0}] = '" + code.ToString() + "'", "رقم الموظف بجهته الأصلية", "المرتب")); } /// 49 23706.61--145 73,984.58 //if (EmpInfo != null) //{ // _db = new ERPContext(); // var model = _db.DailyFileDetailses.Find(EmpInfo.Id); // model.Net = EmpInfo.Net; // _db.SaveChanges(); //} return Ok(); } return BadRequest("Wrong Request"); }