public FileResult exportCSV() { //if (Session["activeSession"] != null && ((bool)Session["activeSession"]) == true) //{ var filename = Convert.ToString(Session["variableInfo"]); var results = (IEnumerable <FilteredData>)(Session["resultInfo"]); var myCSV = new Jitbit.Utils.CsvExport(); foreach (var iter in results) { myCSV.AddRow(); myCSV["Date"] = iter.key1.Date; myCSV["Value"] = iter.Key2; } return(File(myCSV.ExportToBytes(), "text/csv", filename + ".csv")); //} //else //{ // Session.Add("notification", "Please login for downloading data"); // Session["message"] = null; // Session["error"] = null; // IEnumerable<FilteredData> var = (IEnumerable<FilteredData>)(Session["results"]); // return View("ShowResults", var); //} }
static void Main(string[] args) { Console.WriteLine("Desafio 006\n"); List <Aluno> alunos = new List <Aluno>(); alunos.Add(new Aluno { Nome = "Adriana", Idade = 10, Nota = 8 }); alunos.Add(new Aluno { Nome = "Beatriz", Idade = 10, Nota = 7 }); alunos.Add(new Aluno { Nome = "Carlos", Idade = 10, Nota = 9 }); var export = new Jitbit.Utils.CsvExport(); foreach (var aluno in alunos) { export.AddRow(); export["Nome"] = aluno.Nome; export["Idade"] = aluno.Idade; export["Nota"] = aluno.Nota; } export.ExportToFile("alunos.csv"); }
public IActionResult GetCashInCSV(DateTime?StartDate, DateTime?EndDate) { try { var userId = this.help.GetCurrentUser(HttpContext); if (userId <= 0) { return(StatusCode(401, "الرجاء الـتأكد من أنك قمت بتسجيل الدخول")); } string sWebRootFolder = _hostingEnvironment.WebRootPath; string sFileName = @"CashInExport.xlsx"; string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName); FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); if (file.Exists) { file.Delete(); file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); } IQueryable <BanksysBankActions> BankActionsQuery; var UserType = this.help.GetCurrentUserType(HttpContext); // admin see every thing // if date null if (StartDate == null || EndDate == null) { if (UserType == 1) { BankActionsQuery = from t in db.BanksysBankActions where t.ActionType == 3 select t; } else if (UserType == 2) { var BranchId = this.help.GetCurrentBranche(HttpContext); var BankId = db.BanksysBranch.Where(u => u.BranchId == BranchId).Single().BankId; BankActionsQuery = from t in db.BanksysBankActions where t.Branch.BankId == BankId && t.ActionType == 3 select t; } else { var BranchId = this.help.GetCurrentBranche(HttpContext); var BankId = db.BanksysBranch.Where(u => u.BranchId == BranchId).Single().BankId; BankActionsQuery = from t in db.BanksysBankActions where t.BranchId == BranchId && t.ActionType == 3 select t; } } else { if (StartDate == EndDate) { EndDate = EndDate.Value.AddDays(1); } // if date not null if (UserType == 1) { BankActionsQuery = from t in db.BanksysBankActions where t.ActionType == 3 && t.ActionDate >= StartDate && t.ActionDate <= EndDate select t; } else if (UserType == 2) { var BranchId = this.help.GetCurrentBranche(HttpContext); var BankId = db.BanksysBranch.Where(u => u.BranchId == BranchId).Single().BankId; BankActionsQuery = from t in db.BanksysBankActions where t.Branch.BankId == BankId && t.ActionType == 3 && t.ActionDate >= StartDate && t.ActionDate <= EndDate select t; } else { var BranchId = this.help.GetCurrentBranche(HttpContext); var BankId = db.BanksysBranch.Where(u => u.BranchId == BranchId).Single().BankId; BankActionsQuery = from t in db.BanksysBankActions where t.BranchId == BranchId && t.ActionType == 3 && t.ActionDate >= StartDate && t.ActionDate <= EndDate select t; } } var CashInCount = (from t in BankActionsQuery select t).Count(); var CashInList = (from t in BankActionsQuery orderby t.ActionDate descending select new { t.CashIn.Valuedigits, t.CashIn.NumInvoiceDep, t.CashInId, t.CashIn.Description, t.Branch, BranchName = t.Branch.Name, t.Branch.Bank, BankName = t.Branch.Bank.Name, t.CashIn.Status, t.ActionDate, FName = t.CashIn.Personal.Name, t.CashIn.Personal.FatherName, t.CashIn.Personal.SurName, t.CashIn.Personal.GrandName, t.CashIn.Personal.Nid, t.CashIn.Personal.Phone }).ToList(); if (CashInList.Count <= 0) { return(StatusCode(404, "لا توجد بيانات لهذا التاريخ")); } using (ExcelPackage package = new ExcelPackage(file)) { // add a new worksheet to the empty workbook ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee"); //First add the headers worksheet.Cells[1, 1].Value = "القيمة"; worksheet.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 1].Style.Font.Bold = true; worksheet.Cells[1, 1].Style.Font.Size = 15; worksheet.Cells[1, 1].AutoFitColumns(); worksheet.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 2].Value = "رقم الايصال"; worksheet.Cells[1, 2].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 2].Style.Font.Bold = true; worksheet.Cells[1, 2].Style.Font.Size = 15; worksheet.Cells[1, 2].AutoFitColumns(); worksheet.Cells[1, 2].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 3].Value = "الهاتف"; worksheet.Cells[1, 3].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 3].Style.Font.Bold = true; worksheet.Cells[1, 3].Style.Font.Size = 15; worksheet.Cells[1, 3].AutoFitColumns(); worksheet.Cells[1, 3].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 4].Value = "الاسم"; worksheet.Cells[1, 4].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 4].Style.Font.Bold = true; worksheet.Cells[1, 4].Style.Font.Size = 15; worksheet.Cells[1, 4].AutoFitColumns(); worksheet.Cells[1, 4].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 5].Value = "الرقم الوطني"; worksheet.Cells[1, 5].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 5].Style.Font.Bold = true; worksheet.Cells[1, 5].Style.Font.Size = 15; worksheet.Cells[1, 5].AutoFitColumns(); worksheet.Cells[1, 5].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 6].Value = "اسم المصرف"; worksheet.Cells[1, 6].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 6].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 6].Style.Font.Bold = true; worksheet.Cells[1, 6].Style.Font.Size = 15; worksheet.Cells[1, 6].AutoFitColumns(); worksheet.Cells[1, 7].Value = "اسم الفرع"; worksheet.Cells[1, 7].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 7].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 7].Style.Font.Bold = true; worksheet.Cells[1, 7].Style.Font.Size = 15; worksheet.Cells[1, 7].AutoFitColumns(); worksheet.Cells[1, 8].Value = "معلومات اخري"; worksheet.Cells[1, 8].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 8].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 8].Style.Font.Bold = true; worksheet.Cells[1, 8].Style.Font.Size = 15; worksheet.Cells[1, 8].AutoFitColumns(); worksheet.Cells[1, 9].Value = "تاريخ العملية"; worksheet.Cells[1, 9].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 9].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 9].Style.Font.Bold = true; worksheet.Cells[1, 9].Style.Font.Size = 15; worksheet.Cells[1, 9].AutoFitColumns(); worksheet.Cells[1, 10].Value = "الحالة"; worksheet.Cells[1, 10].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells[1, 10].Style.Fill.BackgroundColor.SetColor(Color.Orange); worksheet.Cells[1, 10].Style.Font.Bold = true; worksheet.Cells[1, 10].Style.Font.Size = 15; worksheet.Cells[1, 10].AutoFitColumns(); int i = 2; foreach (var x in CashInList) { worksheet.Cells["A" + i].Value = x.Valuedigits; worksheet.Cells["A" + i].Style.Font.Size = 12; worksheet.Cells["A" + i].AutoFitColumns(); worksheet.Cells["A" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["A" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["B" + i].Value = x.NumInvoiceDep; worksheet.Cells["B" + i].Style.Font.Size = 12; worksheet.Cells["B" + i].AutoFitColumns(); worksheet.Cells["B" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["B" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["C" + i].Value = x.Phone; worksheet.Cells["C" + i].Style.Font.Size = 12; worksheet.Cells["C" + i].AutoFitColumns(); worksheet.Cells["C" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["C" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["D" + i].Value = x.FName + " " + x.FatherName + " " + x.SurName; worksheet.Cells["D" + i].Style.Font.Size = 12; worksheet.Cells["D" + i].AutoFitColumns(); worksheet.Cells["D" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["D" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["E" + i].Value = x.Nid; worksheet.Cells["E" + i].Style.Font.Size = 12; worksheet.Cells["E" + i].AutoFitColumns(); worksheet.Cells["E" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["E" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["F" + i].Value = x.BankName; worksheet.Cells["F" + i].Style.Font.Size = 12; worksheet.Cells["F" + i].AutoFitColumns(); worksheet.Cells["F" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["F" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["G" + i].Value = x.BranchName; worksheet.Cells["G" + i].Style.Font.Size = 12; worksheet.Cells["G" + i].AutoFitColumns(); worksheet.Cells["G" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["G" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["H" + i].Value = x.Description; worksheet.Cells["H" + i].Style.Font.Size = 12; worksheet.Cells["H" + i].AutoFitColumns(); worksheet.Cells["H" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["H" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["I" + i].Value = x.ActionDate.Date.ToString(); worksheet.Cells["I" + i].Style.Font.Size = 12; worksheet.Cells["I" + i].AutoFitColumns(); worksheet.Cells["I" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["I" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); worksheet.Cells["J" + i].Value = (x.Status == 1?"تأكيد مبدئي": (x.Status == 2 ? "تأكيد نهائي" : "مرفوض")); worksheet.Cells["J" + i].Style.Font.Size = 12; worksheet.Cells["J" + i].AutoFitColumns(); worksheet.Cells["J" + i].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["J" + i].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); i++; } //Add values //worksheet.Cells["B2"].Value = "Jon"; //worksheet.Cells["C2"].Value = "M"; //worksheet.Cells["D2"].Value = 5000; //worksheet.Cells["A3"].Value = 1001; //worksheet.Cells["B3"].Value = "Graham"; //worksheet.Cells["C3"].Value = "M"; //worksheet.Cells["D3"].Value = 10000; //worksheet.Cells["A4"].Value = 1002; //worksheet.Cells["B4"].Value = "Jenny"; //worksheet.Cells["C4"].Value = "F"; //worksheet.Cells["D4"].Value = 5000; package.Save(); //Save the workbook. } return(Ok(URL)); var myExport = new Jitbit.Utils.CsvExport(); myExport.AddRow(); myExport["Valuedigits"] = "القيمة"; myExport["NumInvoiceDep"] = "رقم الايصال"; myExport["Description"] = "معلومات اخري"; myExport["BankName"] = "اسم المصرف"; myExport["BranchName"] = "اسم الفرع"; myExport["ActionDate"] = "تاريخ العملية"; myExport["Nid"] = "Nid"; myExport["Phone"] = "الهاتف"; myExport["FName"] = "الاسم"; myExport["FatherName"] = "اسم الأب"; myExport["GrandName"] = "اسم الجد"; myExport["SurName"] = "اللقب"; myExport["Status"] = "الحالة"; foreach (var x in CashInList) { myExport.AddRow(); myExport["Valuedigits"] = x.Valuedigits; myExport["NumInvoiceDep"] = x.NumInvoiceDep; myExport["Description"] = x.Description; myExport["BankName"] = x.BankName; myExport["BranchName"] = x.BranchName; myExport["ActionDate"] = x.ActionDate; myExport["Nid"] = x.Nid; myExport["Phone"] = x.Phone; myExport["FName"] = x.FName; myExport["FatherName"] = x.FatherName; myExport["GrandName"] = x.GrandName; myExport["SurName"] = x.SurName; myExport["Status"] = (x.Status == 1 ? "تأكيد مبدئي" : (x.Status == 2 ? "تأكيد نهائي" : "مرفوض")); } byte[] bytes; bytes = myExport.ExportToBytes(); var result = new FileContentResult(bytes, "application/octet-stream"); result.FileDownloadName = "CashIn.csv"; return(result); } catch (Exception ex) { return(BadRequest(ex.Message)); } }