Example #1
0
        public void ExportToExcel()
        {
            var allContracts  = _contract.GetAll();
            var contractModel = allContracts.Select(c => new ContractIndexListingModel
            {
                Id            = c.Id,
                TipContract   = c.TipContract,
                NumarContract = c.NumarContract,
                DataInceput   = c.DataInceput,
                DataSfarsit   = c.DataSfarsit,
                PersonId      = c.Person.Id,
                PersonName    = c.Person.Nume,
                PersonSurname = c.Person.Prenume
            });

            var model = new ContractIndexModel()
            {
                Contracts = contractModel
            };

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            ExcelPackage   pck = new ExcelPackage();
            ExcelWorksheet ws  = pck.Workbook.Worksheets.Add("Raport contracte");

            ws.Cells["A1"].Value = "Tip contract";
            ws.Cells["B1"].Value = "Numar contract";
            ws.Cells["C1"].Value = "Persoana";
            ws.Cells["D1"].Value = "Data de inceput";
            ws.Cells["E1"].Value = "Data de sfarsit";

            int rowStart = 2;

            foreach (var contract in model.Contracts)
            {
                ws.Cells[string.Format("A{0}", rowStart)].Value = contract.TipContract;
                ws.Cells[string.Format("B{0}", rowStart)].Value = contract.NumarContract;
                ws.Cells[string.Format("C{0}", rowStart)].Value = contract.PersonName + " " + contract.PersonSurname;
                ws.Cells[string.Format("D{0}", rowStart)].Value = string.Format("{0:dd MMMM yyyy}", contract.DataInceput);
                ws.Cells[string.Format("E{0}", rowStart)].Value = string.Format("{0:dd MMMM yyyy}", contract.DataSfarsit);
                rowStart++;
            }

            ws.Cells["A:AZ"].AutoFitColumns();
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.Headers.Add("content-disposition", "attachment: filename=" + "ExcelReport.xlsx");
            Response.Body.Write(pck.GetAsByteArray());
        }
Example #2
0
        public IActionResult Index()
        {
            var allContracts  = _contract.GetAll();
            var contractModel = allContracts.Select(c => new ContractIndexListingModel
            {
                Id            = c.Id,
                TipContract   = c.TipContract,
                NumarContract = c.NumarContract,
                DataInceput   = c.DataInceput,
                DataSfarsit   = c.DataSfarsit,
                PersonId      = c.Person.Id,
                PersonName    = c.Person.Nume,
                PersonSurname = c.Person.Prenume
            });

            var model = new ContractIndexModel()
            {
                Contracts = contractModel
            };

            return(View(model));
        }