Beispiel #1
0
        public async Task <IActionResult> Excel(int Id)
        {
            // await Task.Yield ();
            string   sWebRootFolder = _hostingEnvironment.WebRootPath;
            string   sFileName      = @"demo.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));
            }
            using (ExcelPackage package = new ExcelPackage(file)) {
                // add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Voters");
                var            voters    = await _repo.GetAllVotersByType(Id);

                var votersForExport = _mapper.Map <IEnumerable <VotersExportDto> > (voters);
                worksheet.Cells.LoadFromCollection(votersForExport, true);

                package.Save();  //Save the workbook.
            }
            Export EportObj = new Export();

            EportObj.URL = URL;
            return(Ok(EportObj));
        }