Exemple #1
0
        private async void btnDone_Click(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("Add new produt", "Confirm");

            messageDialog.Commands.Add(new UICommand("Yes")
            {
                Id = 0
            });
            messageDialog.Commands.Add(new UICommand("No")
            {
                Id = 1
            });
            messageDialog.DefaultCommandIndex = 0;
            messageDialog.CancelCommandIndex  = 1;

            var result = await messageDialog.ShowAsync();

            if ((int)result.Id == 0)
            {
                Product.Price    = Decimal.Parse(addGia.Text);
                Product.Quantity = int.Parse(addSoLuong.Text);
                var productid = QueryForSQLServer.InsertProduct(Product);
                int id        = 1;
                foreach (var item in Product.Product_Images)
                {
                    item.id        = id;
                    item.ProductId = productid;
                    QueryForSQLServer.InsertProduct_Image(item);
                    id++;
                }
                Frame.GoBack();
            }
            else
            {
                Frame.GoBack();
            }
        }
Exemple #2
0
        private async void ImportButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ExcelEngine excelEngine = new ExcelEngine();

                IApplication application = excelEngine.Excel;

                //Instantiates the File Picker.



                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
                openPicker.FileTypeFilter.Add(".xlsx");
                openPicker.FileTypeFilter.Add(".xls");
                StorageFile openFile = await openPicker.PickSingleFileAsync();

                //Opens the workbook.
                IWorkbook workbook = await application.Workbooks.OpenAsync(openFile);

                //Access first worksheet from the workbook.
                var tabs = workbook.Worksheets;

                //Set Text in cell A3.


                //Sets workbook version.
                workbook.Version = ExcelVersion.Excel2016;

                //Initializes FileSavePicker.
                FileSavePicker savePicker = new FileSavePicker();

                List <Category> list = new List <Category>();

                foreach (var tab in tabs)
                {
                    Debug.WriteLine(tab.Name);
                    var row      = 3;
                    var category = new Category()
                    {
                        Name = tab.Name
                    };
                    category.Id = QueryForSQLServer.InsertCategory(category);

                    tab.UsedRangeIncludesFormatting = false;
                    var cell = tab.Range[$"C3"];

                    while (cell.Value != null && !cell.IsBlank)
                    {
                        var author      = tab.Range[$"J{row}"].Text;
                        var name        = tab.Range[$"C{row}"].Text;
                        var price       = Convert.ToDecimal(tab.Range[$"D{row}"].Number);
                        var quantity    = (int)(tab.Range[$"E{row}"].Number);
                        var description = tab.Range[$"F{row}"].Text;
                        var image       = tab.Range[$"I{row}"].Text;

                        var product = new Product()
                        {
                            Author      = author,
                            Name        = name,
                            CatId       = category.Id,
                            Price       = price,
                            Quantity    = quantity,
                            Description = description,
                            Image       = image
                        };

                        category.Products.Add(product);


                        Debug.WriteLine($"{author}{name}{price}{quantity}{description}");

                        // Đi qua dòng kế
                        row++;
                        cell = tab.Range[$"C{row}"];
                    }
                    list.Add(category);
                }
                var tes = list;

                workbook.Close();
                excelEngine.Dispose();

                var messageDialog = new MessageDialog("Import", "Confirm");

                messageDialog.Commands.Add(new UICommand("Yes")
                {
                    Id = 0
                });
                messageDialog.Commands.Add(new UICommand("No")
                {
                    Id = 1
                });
                messageDialog.DefaultCommandIndex = 0;
                messageDialog.CancelCommandIndex  = 1;

                var result = await messageDialog.ShowAsync();

                if ((int)result.Id == 0)
                {
                    foreach (var cat in list)
                    {
                        cat.Id = QueryForSQLServer.InsertCategory(cat);
                        foreach (var product in cat.Products)
                        {
                            var index = QueryForSQLServer.InsertProduct(product);
                        }
                    }
                    var messageDialog2 = await new MessageDialog("Success", "Confirm").ShowAsync();
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }