Example #1
0
        private void BtnInsert_Click(object sender, EventArgs e)
        {
            ClsGood good = GetGoodFromInput();

            if (good != null)
            {
                if (DialogResult.OK == MsgBoxUtil.QuestionMsgBox("确认提交?"))
                {
                    FrmGoodIncome.Goods.Add(good);
                    Close();
                }
            }
        }
        private ClsGood ParseGood(IRow row)
        {
            if ("" == row.GetCell(0).ToString())
            {
                errInfo = "商品不存在";
                hasErr  = true;
                return(null);
            }
            int categoryID = int.Parse(row.GetCell(0).ToString());
            List <EtCategory> categories = CategoryDao.QueryByCategoryID(categoryID);

            if (0 == categories.Count)
            {
                errInfo = "商品不存在";
                hasErr  = true;
                return(null);
            }
            string productionDate = row.GetCell(1).ToString();

            if ("" == row.GetCell(2).ToString())
            {
                errInfo = "进价不能为空";
                hasErr  = true;
                return(null);
            }
            double cost  = double.Parse(row.GetCell(2).ToString());
            double price = "" != row.GetCell(3).ToString() ? double.Parse(row.GetCell(3).ToString()) : 0;
            int    count = "" != row.GetCell(4).ToString() ? int.Parse(row.GetCell(4).ToString()) : 0;

            if (0 == count)
            {
                errInfo = "数量不能为空";
                hasErr  = true;
                return(null);
            }
            EState  state = StringToEState(row.GetCell(5).ToString());
            ClsGood good  = new ClsGood {
                Good = new EtGood {
                    Category       = categories[0],
                    ProductionDate = productionDate,
                    Cost           = cost,
                    Price          = price,
                    State          = state
                },
                Count = count
            };

            return(good);
        }
Example #3
0
        private ClsGood GetGoodFromInput()
        {
            ClsGood good = null;

            try {
                int               categoryID     = int.Parse(CmbCategoryID.SelectedItem.ToString().Split(' ')[0]);
                string            productionDate = DtpProductionID.Value.ToString("yyyyMMdd");
                double            cost           = double.Parse(TxtCost.Text);
                double            price          = TxtPrice.Text != "" ? double.Parse(TxtPrice.Text) : 0;
                int               count          = TxtCount.Text != "" ? int.Parse(TxtCount.Text) : 0;
                EState            state          = (EState)CmbIsValid.SelectedIndex;
                List <EtCategory> categories     = CategoryDao.QueryByCategoryID(categoryID);
                if (0 == count)
                {
                    MsgBoxUtil.ErrMsgBox("商品数量不能为0!");
                }
                else
                {
                    if (0 == categories.Count)
                    {
                        MsgBoxUtil.ErrMsgBox("不存在该商品!");
                    }
                    else
                    {
                        good = new ClsGood {
                            Good = new EtGood {
                                Category       = categories[0],
                                ProductionDate = productionDate,
                                Cost           = cost,
                                Price          = price,
                                State          = state
                            },
                            Count = count
                        }
                    };
                }
            }
            catch (Exception) {
                MsgBoxUtil.ErrMsgBox("非法输入!");
            }
            return(good);
        }
        private void BtnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog {
                InitialDirectory = Path.GetFullPath("../../Excel模板"),
                Title            = "选择Excel文件",
                Filter           = "excel文件(*.xls,*.xlsx)|*.xls;*.xlsx|excel03文件(*.xls)|*.xls|excel07文件(*.xlsx)|*.xlsx",
                RestoreDirectory = true
            };
            string excelPath = null;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                excelPath         = openFileDialog.FileName;
                TxtExcelPath.Text = excelPath;
            }
            if (excelPath == null || excelPath == "")
            {
                MsgBoxUtil.ErrMsgBox("路径不能为空");
                return;
            }
            using (FileStream fs = File.OpenRead(excelPath)) {
                IWorkbook workbook = null;
                //判断Excel的格式,区分xls与xlsx
                if (Path.GetExtension(fs.Name) == ".xls")
                {
                    workbook = new HSSFWorkbook(fs);
                }
                else if (Path.GetExtension(fs.Name) == ".xlsx")
                {
                    workbook = new XSSFWorkbook(fs);
                }
                //获取工作表
                ISheet sheet = workbook.GetSheetAt(0);
                //遍历行
                for (int i = 1; i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    if (row != null)
                    {
                        ClsGood good = ParseGood(row);
                        if (good != null)
                        {
                            excelGoods.Add(good);
                            DgvGoodFromExcel.Rows.Add(new object[] {
                                "√  " + i,
                                good.Good.Category.CategoryID,
                                good.Good.ProductionDate,
                                good.Good.Cost,
                                good.Good.Price,
                                good.Count,
                                good.Good.State,
                                ""
                            });
                        }
                        else
                        {
                            DgvGoodFromExcel.Rows.Add(new object[] { "×  " + i, "", "", "", "", "", "", errInfo });
                            DgvGoodFromExcel.Rows[i - 1].DefaultCellStyle.ForeColor = Color.Red;
                        }
                    }
                }
                DgvGoodFromExcel.RowsDefaultCellStyle.BackColor            = Color.LightCyan;
                DgvGoodFromExcel.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
            }
        }