Beispiel #1
0
        public async Task <IActionResult> DownloadLocationExcel()
        {
            await Task.Yield();

            var location = _synchronized.ExportListLocation();

            var stream = new MemoryStream();

            using (var package = new ExcelPackage(stream))
            {
                var workSheet = package.Workbook.Worksheets.Add(Resources.SheetName);

                workSheet.Row(1).Height = 20;
                workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                workSheet.Row(1).Style.Font.Bold           = true;

                //Set độ rộng cho các cột
                workSheet.Column(1).Width = 10; //STT
                workSheet.Column(2).Width = 20; //ID
                workSheet.Column(3).Width = 20; //ID
                workSheet.Column(4).Width = 35; //Name
                workSheet.Column(5).Width = 20; //Kind
                workSheet.Column(6).Width = 20; //ParentID

                //Đặt tên cho header
                workSheet.Cells[1, 1].Value = Resources.STT;
                workSheet.Cells[1, 2].Value = Resources.Header_LocID;
                workSheet.Cells[1, 3].Value = Resources.Header_ID;
                workSheet.Cells[1, 4].Value = Resources.Header_Name;
                workSheet.Cells[1, 5].Value = Resources.Header_Kind;
                workSheet.Cells[1, 6].Value = Resources.Header_ParentID;

                //Điền các dữ liệu từ list vào các row
                int recordIndex = 2;
                foreach (var loc in location)
                {
                    workSheet.Cells[recordIndex, 1].Value = (recordIndex - 1).ToString();
                    workSheet.Cells[recordIndex, 2].Value = loc.LocationID;
                    workSheet.Cells[recordIndex, 3].Value = loc.ID;
                    workSheet.Cells[recordIndex, 4].Value = loc.LocationName;
                    workSheet.Cells[recordIndex, 5].Value = (loc.Kind == 0) ? Resources.Country
                        : (loc.Kind == 1) ? Resources.Privince
                        : (loc.Kind == 2) ? Resources.District
                        : Resources.Ward;
                    workSheet.Cells[recordIndex, 6].Value = loc.ParentID;
                    recordIndex++;
                }

                package.Save();
            }
            stream.Position = 0;
            string excelName = Resources.DownloadName + ".xlsx";

            //return File(stream, "application/octet-stream", excelName);
            return(File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelName));
        }