Esempio n. 1
0
        private void importExcelFile()
        {
            try
            {
                if (!File.Exists(filePathExcel))
                {
                    if (OnError != null)
                    {
                        OnError("Tập tin Excel không tồn tại", new CancelEventArgs());
                    }
                    return;
                }
                DataSet ds = ExcelDataProvider.GetDataSet(filePathExcel, sheetNameExcel);
                if (Memory.HasError())
                {
                    if (OnError != null)
                    {
                        OnError("Không thể lấy dữ liệu từ tập tin được chọn. Tập tin này phải là tập tin được xuất ra từ chương trình, không được chỉnh sửa dòng đầu tiên và tên sheet.\r\nXin vui lòng kiểm tra lại hoặc liên hệ tác giả để biết thêm thông tin chi tiết.", new CancelEventArgs());
                    }
                    return;
                }
                Dictionary <string, string> dicColumnText;
                Dictionary <string, int>    dicColumnWidth;
                ExportDataToExcel.GetColumnInfo(out dicColumnText, out dicColumnWidth);

                tblGiaoDan = Memory.GetData("SELECT * FROM GiaoDan");

                object oID   = tblGiaoDan.Compute("MAX(MaGiaoDan)", string.Empty);
                int    maxID = (oID != DBNull.Value ? Convert.ToInt32(oID) : 0);

                oID = ds.Tables[0].Compute("MAX([Mã GD])", string.Empty);
                int maxExcelID = (oID != DBNull.Value ? Convert.ToInt32(oID) : 0);

                maxID = Math.Max(maxID, maxExcelID);

                int duLieuHienTai = 0;
                Memory.Instance.SetMemory("TongDuLieu", ds.Tables[0].Rows.Count);
                if (OnStart != null)
                {
                    OnStart("started", EventArgs.Empty);
                }

                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    duLieuHienTai++;
                    Memory.Instance.SetMemory("DuLieuHienTai", duLieuHienTai);
                    if (OnExecuting != null)
                    {
                        OnExecuting(string.Concat("Đang nhập giáo dân ", r["Họ Tên"]), EventArgs.Empty);
                    }
                    //check user abort
                    bool userStop = (bool)Memory.Instance.GetMemory("UserAbort");
                    //if user abort progress, return
                    if (userStop)
                    {
                        return;
                    }

                    DataRow row     = tblGiaoDan.NewRow();
                    bool    isExist = false;
                    foreach (KeyValuePair <string, string> col in dicColumnText)
                    {
                        string colName = col.Key.Trim('_');
                        if (colName == GiaoDanConst.MaGiaoDan)
                        {
                            if (r[col.Value].ToString().Trim() != "")
                            {
                                DataRow[] rows = tblGiaoDan.Select(string.Format("MaGiaoDan={0}", r[col.Value]));
                                if (rows.Length > 0)
                                {
                                    row     = rows[0];
                                    isExist = true;
                                }
                                else
                                {
                                    row[GiaoDanConst.MaGiaoDan] = r[col.Value];
                                }
                            }
                            else
                            {
                                maxID++;
                                row[GiaoDanConst.MaGiaoDan] = maxID;
                            }
                        }
                        else if (colName == GiaoHoConst.TenGiaoHo)
                        {
                            row["MaGiaoHo"] = getMaGiaoHo(r[col.Value].ToString());
                        }
                        else if (col.Key.StartsWith("_"))
                        {
                            if (!string.IsNullOrEmpty(r[col.Value].ToString().Trim()))
                            {
                                row[colName] = true;
                            }
                            else
                            {
                                row[colName] = false;
                            }
                        }
                        else if (colName.ToLower().StartsWith("ngay") && (r[col.Value] is DateTime))
                        {
                            row[colName] = ((DateTime)r[col.Value]).ToString("dd/MM/yyyy");
                        }
                        else if (colName == GiaoDanConst.UpdateDate)
                        {
                            if (r[col.Value].ToString() == "")
                            {
                                row[colName] = DateTime.Now;
                            }
                        }
                        else
                        {
                            row[colName] = r[col.Value];
                            ConvertDB.vnConvert.FontIndex vnType = ConvertDB.vnConvert.FontIndex.iUNI;
                            if (convert.isVietnamese(row[colName].ToString(), ref vnType))
                            {
                                row[colName] = Memory.ConvertVNI2UNI(row[colName].ToString());
                            }
                        }
                    }
                    if (!isExist)
                    {
                        tblGiaoDan.Rows.Add(row);
                    }
                }
                ds = new DataSet();
                tblGiaoDan.TableName = GiaoDanConst.TableName;
                // Chuan hoa data
                for (int i = tblGiaoDan.Rows.Count - 1; i >= 0; i--)
                {
                    DataRow row = tblGiaoDan.Rows[i];

                    if (row.RowState == DataRowState.Unchanged)
                    {
                        tblGiaoDan.Rows.Remove(row);
                    }
                }
                Memory.AutoUpperCaseFirstCharGiaoDan(tblGiaoDan);
                if (OnExecuting != null)
                {
                    OnExecuting("Đang lưu thông tin giáo dân vào chương trình...", EventArgs.Empty);
                }
                ds.Tables.Add(tblGiaoDan);
                Memory.UpdateDataSet(ds);
                if (Memory.HasError())
                {
                    if (OnError != null)
                    {
                        OnError("Có lỗi khi cập nhật thông tin giáo dân. Thông tin lỗi: " + Memory.Instance.Error.Message, new CancelEventArgs());
                    }
                    return;
                }
                if (OnFinished != null)
                {
                    OnFinished("finished", EventArgs.Empty);
                }
            }
            catch (Exception ex)
            {
                if (OnError != null)
                {
                    OnError("Có lỗi không mong muốn xảy ra khi xem xét thông tin giáo dân. Thông tin lỗi: " + ex.Message, new CancelEventArgs());
                }
            }
            finally
            {
                Memory.ClearError();
                filePathExcel  = "";
                sheetNameExcel = "";
            }
        }