示例#1
0
        /// <summary>
        /// Xuất excel.
        /// </summary>
        /// <param name="input">Điều kiện xuất excel.</param>
        /// <returns>File excel.</returns>
        public async Task <FileDto> ExportToExcel(DemoGetAllInputDto input)
        {
            // Lấy danh sách cần xuất excel
            var list = await this.GetAllAsync(input);

            using (var package = new ExcelPackage())
            {
                // Add sheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Demo");

                var namedStyle = package.Workbook.Styles.CreateNamedStyle("HyperLink");
                namedStyle.Style.Font.UnderLine = true;
                namedStyle.Style.Font.Color.SetColor(Color.Blue);

                // set header
                worksheet.Cells[1, 1].Value = "Mã";
                worksheet.Cells[1, 2].Value = "Tên";

                // Bôi đậm header
                using (ExcelRange r = worksheet.Cells[1, 1, 1, 2])
                {
                    using (var f = new Font("Calibri", 12, FontStyle.Bold))
                    {
                        r.Style.Font.SetFromFont(f);
                        r.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    }
                }

                // Gan gia tri
                var rowNumber = 2;
                list.Items.ToList().ForEach(item =>
                {
                    worksheet.Cells[rowNumber, 1].Value = item.Demo.Ma;
                    worksheet.Cells[rowNumber, 2].Value = item.Demo.Ten;
                    rowNumber++;
                });

                // Cho các ô rộng theo dữ liệu
                worksheet.Cells.AutoFitColumns(0);

                worksheet.PrinterSettings.FitToHeight = 1;

                // Tên file
                var fileName = string.Join(".", new string[] { "Demo", "xlsx" });

                // Lưu file vào server
                using (var stream = new MemoryStream())
                {
                    package.SaveAs(stream);
                }

                var file     = new FileDto(fileName, MimeTypeNames.ApplicationVndOpenxmlformatsOfficedocumentSpreadsheetmlSheet);
                var filePath = Path.Combine(this.appFolders.TempFileDownloadFolder, file.FileToken);
                package.SaveAs(new FileInfo(filePath));
                return(file);
            }
        }
示例#2
0
        /// <summary>
        /// Lấy danh sách.
        /// </summary>
        /// <param name="input">Điều kiện tìm kiếm.</param>
        /// <returns>Danh sách.</returns>
        public async Task <PagedResultDto <DemoForView> > GetAllAsync(DemoGetAllInputDto input)
        {
            var filter = this.demoRepository.GetAll()
                         .WhereIf(input != null && !string.IsNullOrEmpty(input.Keyword), e => e.Ma.Contains(input.Keyword) || e.Ten.Contains(input.Keyword));
            var totalCount = await filter.CountAsync();

            var query = from o in filter
                        select new DemoForView()
            {
                Demo           = this.ObjectMapper.Map <DemoDto>(o),
                TrangThai      = GlobalModel.SortedTrangThaiHieuLuc.ContainsKey((int)o.DropdownSingle) ? GlobalModel.SortedTrangThaiHieuLuc[(int)o.DropdownSingle] : string.Empty,
                TrangThaiDuyet = GlobalModel.SortedTrangThaiDuyet.ContainsKey((int)o.AutoCompleteSingle) ? GlobalModel.SortedTrangThaiDuyet[(int)o.AutoCompleteSingle] : string.Empty,
            };
            var items = query.PageBy(input)
                        .ToList();

            return(new PagedResultDto <DemoForView>
            {
                TotalCount = totalCount,
                Items = items,
            });
        }