Ejemplo n.º 1
0
        private void ImportDataSet(string fileName)
        {
            var biz = new ProductLDBiz();

            using (var document = SpreadsheetDocument.Open(fileName, false))
            {
                var wbPart   = document.WorkbookPart;
                var theSheet = wbPart.Workbook.Descendants <Sheet>().FirstOrDefault();

                var wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
                var count  = wsPart.Worksheet.Descendants <Row>().Count();
                for (var i = 2; i <= count; i++)
                {
                    var product = new ProductLD
                    {
                        Code = IOReader.GetCellValue(document, theSheet, "A" + i),
                        Name = IOReader.GetCellValue(document, theSheet, "B" + i),
                    };

                    decimal number = 0;
                    var     price  = IOReader.GetCellValue(document, theSheet, "C" + i);
                    if (!string.IsNullOrEmpty(price) && decimal.TryParse(price, out number))
                    {
                        product.Price = number;
                    }

                    int number1       = 0;
                    var productTypeId = IOReader.GetCellValue(document, theSheet, "D" + i);
                    if (!string.IsNullOrEmpty(productTypeId) && int.TryParse(productTypeId, out number1))
                    {
                        product.ProductTypeId = number1;
                    }

                    var item = biz.LoadItems().FirstOrDefault(p => p.Code == product.Code);

                    if (string.IsNullOrEmpty(product.Code))
                    {
                        break;
                    }

                    if (item != null)
                    {
                        product.Id               = item.Id;
                        product.Code             = item.Code;
                        product.Name             = item.Name;
                        product.ProductTypeId    = item.ProductTypeId;
                        product.Created          = item.Created;
                        product.CreatedByUserId  = item.CreatedByUserId;
                        product.Modified         = item.Modified;
                        product.ModifiedByUserId = item.ModifiedByUserId;

                        try
                        {
                            biz.UpdateItem(product);
                        }
                        catch (Exception ex)
                        {
                            logger.LogInfoMessage("Import Update Product");
                            string log = string.Format("ID: {0}, Code: {1}, Name: {2}, TypeId: {3}", item.Id, item.Code, item.Name, item.ProductTypeId);
                            logger.LogInfoMessage(log);
                            logger.LogException(ex);
                        }
                    }
                    else
                    {
                        product.Id               = 0;
                        product.Created          = DateTime.Now;
                        product.CreatedByUserId  = currentUser.Id;
                        product.Modified         = DateTime.Now;
                        product.ModifiedByUserId = currentUser.Id;

                        try
                        {
                            biz.SaveItem(product);
                        }
                        catch (Exception ex)
                        {
                            logger.LogInfoMessage("Import New Product");
                            string log = string.Format("ID: {0}, Code: {1}, Name: {2}, TypeId: {3}", 0, product.Code, product.Name, product.ProductTypeId);
                            logger.LogInfoMessage(log);
                            logger.LogException(ex);
                        }
                    }
                }

                MessageBox.Show("Cập nhật thành công.");
            }
        }