示例#1
0
        private async void Import_Detail_Images_Click(object sender, RoutedEventArgs e)
        {
            ExcelEngine excelEngine = new ExcelEngine();

            IApplication application = excelEngine.Excel;

            //Instantiates the File Picker.
            try
            {
                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;

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

                //Initializes FileSavePicker.
                FileSavePicker        savePicker = new FileSavePicker();
                List <Product_Images> list       = new List <Product_Images>();
                foreach (var tab in tabs)
                {
                    Debug.WriteLine(tab.Name);
                    var row     = 3;
                    var product = new Product()
                    {
                        Name = tab.Name
                    };

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

                    while (cell.Value != null && !cell.IsBlank)
                    {
                        var image = tab.Range[$"B{row}"].Text;
                        var name  = tab.Range[$"C{row}"].Text;

                        var ImageId = (int)QueryForSQLServer.GetProductsByImage(image).Rows[0].ItemArray[0];

                        var product_image = new Product_Images()
                        {
                            ProductId = ImageId,
                            Name      = name
                        };


                        Debug.WriteLine($"{image}{name}");
                        list.Add(product_image);
                        // Đi qua dòng kế
                        row++;
                        cell = tab.Range[$"C{row}"];
                    }
                }
                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)
                {
                    var i          = QueryForSQLServer.GetProducts_ImageMaxId(list[0].ProductId) + 1;
                    var preProduct = list[0].ProductId;
                    foreach (var img in list)
                    {
                        if (img.ProductId != preProduct)
                        {
                            i = QueryForSQLServer.GetProducts_ImageMaxId(img.ProductId) + 1;
                        }
                        img.id     = i;
                        preProduct = img.ProductId;
                        QueryForSQLServer.InsertProduct_Image(img);
                        Debug.WriteLine(img.id + img.Name + img.ProductId);
                        i++;
                    }
                    var messageDialog2 = await new  MessageDialog("Success", "Confirm").ShowAsync();
                }

                else
                {
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
            }
        }