Translate

Hiển thị các bài đăng có nhãn excel. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn excel. Hiển thị tất cả bài đăng

Thứ Sáu, 26 tháng 3, 2021

Bài 6.a: Export dữ liệu ra file excel

 Bài 6.a: Export dữ liệu ra file excel

Thân chào anh em YAFers. Hôm nay, chúng ta sẽ làm nội dung sau:
  • Xuất dữ liệu hàng hóa (Product) ra file excel theo một mẫu định nghĩa trước.
Nội dung chi tiết các bạn có thể xem clip này nhé.

  1. Xuất dữ liệu hàng hóa (Product) ra file excel theo một mẫu định nghĩa trước

1.1 Định nghĩa mẫu file excel

Đầu tiên, các bạn tạ một file excel trong thư mục ExcelTemplates, có format như sau:

1.2 Tạo lớp Export

Sau đó tạo lớp export như bên dưới.

using DevExpress.ExpressApp;
using DevExpress.Spreadsheet;
using System;
using System.Linq;
using YouSoft.Vn.XAFCustom.Supports.Extensions;
using YouSoft.Vn.XAFCustom.Supports.Interfaces;

namespace App.Module.Modules.Inventory.Entities
{
    public class ProductExcelFiller : IFillDataToExcelTemplate
    {
        public string RelativeFilePath => @"ExcelTemplates\BaoGia.xlsx";

        public string DefaultSavingName => $"{DateTime.Now.ToString("ddMMyyy_HHmm")}_BaoGia.xlsx";

        public Type TargetType => typeof(Product);

        public Nesting TargetNesting => Nesting.Any;

        public ViewType TargetViewType => ViewType.Any;

        public bool FillData(Worksheet sheet, ViewController controller, out string expectedExportFileName)
        {
            expectedExportFileName = DefaultSavingName;
            var products = controller.View.GetSelectedObjects<Product>();
            if (products.Count <= 0)
            {
                products = controller.View.ObjectSpace.GetObjectsQuery<Product>()
                    .ToList();
            }
            var rowIndex = FillingInfo.StartingRow;
            var index = 1;
            foreach(var product in products)
            {
                
                sheet.SetCellValue($"{FillingInfo.STT}{rowIndex}", index.ToString());
                sheet.SetCellValue($"{FillingInfo.Name}{rowIndex}", product.Name);
                sheet.SetCellValue($"{FillingInfo.Price}{rowIndex}", product.Price.ToString("N0"));
                rowIndex++;
                index++;
            }
            return true;
        }
        public class FillingInfo
        {
            public const int StartingRow = 5;
            public const char STT = 'A';
            public const char Name = 'B';
            public const char Price = 'C';
        }
    }
}

1.3 Đăng ký


//file: App.Module.cs
        protected override void Register(IFillDataToExcelTemplateService service)
        {
            base.Register(service);
            service.Register<ProductExcelFiller>();
        }

1.4 Chạy phần mềm


Kết quả sau khi xuất.
Chúc các bạn thành công.



Thứ Năm, 25 tháng 3, 2021

Bài 5: Import hàng hóa từ file excel

 Bài 5: Import hàng hóa từ file excel

Chào các bạn.
Ở bài này chúng ta sẽ import hàng hóa từ một file excel.

1. Tạo file excel mẫu:




2. Tạo lớp ProductImporter


using DevExpress.ExpressApp;
using DevExpress.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Linq;
using YouSoft.Vn.Framework.Core.Extensions;
using YouSoft.Vn.XAFCustom.Supports.Base;
using YouSoft.Vn.XAFCustom.Supports.Extensions;

namespace App.Module.Modules.Inventory.Entities
{
    public class ProductImporter : ExcelImporterBase<Product>
    {
        public override int StaringRowIndex => 2;

        public override string SampleFilePath => @"ExcelTemplates\Products.xlsx";
        List<string> productNames;

        public override bool OnStart(IObjectSpace os, ViewController controller, Workbook workbook)
        {
            base.OnStart(os, controller, workbook);
            productNames = os.GetObjectsQuery<Product>()
                    .Select(p=>p.Name)
                    .ToList();
            return true;
        }

        public override bool OnFinish()
        {
            productNames.Dispose();
            return base.OnFinish();

        }

        public override bool OnRowStart(int rowIndex, ref string error)
        {
            //1. đọc tên hàng hóa
            var name=Sheet.GetCellValueString(Columns.Name, rowIndex);
            name = name?.Trim();
            //2. Nếu tên hàng hóa không rỗng/null và không có trong csdl
            if (!name.IsNullOrWhiteSpace()
                ||!productNames.Contains(name)
                )
            {
                //3. tạo mới hàng hóa
                var price = Sheet.GetCellValue<double>(Columns.Price, rowIndex, 0);
                new Product(Session)
                {
                    //với tên ở trên
                    Name=name,
                    Price=price
                };
                productNames.Add(name);
            }
                
                
                //đọc giá 
            return true;
        }
        public static class Columns
        {
            public const char Name = 'A';
            public const char Price = 'B';
        }
    }
}

Đăng ký lớp ở App.Module.cs

		protected override void Register(IExcelImportService service)
        {
            base.Register(service);
            service.Register<ProductImporter>();
        }
Lưu ý, ở project chạy chương trình, nhớ có file mẫu ở trên.

Chạy chương trình



Các bạn có thể tham khảo chi tiết ở Video hướng dẫn
Có khó khăn gì thì comment bên dưới nhé.