Пример #1
0
        private async void addCat_Click(object sender, RoutedEventArgs e)
        {
            TextBox input = new TextBox()
            {
                Height          = (double)App.Current.Resources["TextControlThemeMinHeight"],
                PlaceholderText = "Display Text"
            };
            ContentDialog dialog = new ContentDialog()
            {
                Title               = "Change Category's Name",
                MaxWidth            = this.ActualWidth,
                PrimaryButtonText   = "OK",
                SecondaryButtonText = "Cancel",
                Content             = input
            };
            ContentDialogResult result = await dialog.ShowAsync();

            if (result == ContentDialogResult.Primary)
            {
                input = (TextBox)dialog.Content;
                if (input.Text != "")
                {
                    var cat = new Category()
                    {
                        Name = input.Text
                    };
                    QueryForSQLServer.InsertCategory(cat);
                    await new Windows.UI.Popups.MessageDialog("Success!").ShowAsync();
                    Refresh();
                }
                else
                {
                    await new Windows.UI.Popups.MessageDialog("Noting change!").ShowAsync();
                }
            }
        }
Пример #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);
            }
        }