コード例 #1
0
        private void btnImportByExcel_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlgDialog = new OpenFileDialog();
                dlgDialog.Filter           = "All File |*.*;|File Excel |*.xlsx;|File Excel |*.xls;";
                dlgDialog.FilterIndex      = 1;
                dlgDialog.AddExtension     = true;
                dlgDialog.RestoreDirectory = true;

                dlgDialog.InitialDirectory = Application.StartupPath + "\\Template";
                if (dlgDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        string sourcefile = dlgDialog.FileName;
                        string excelquery = string.Format("Select * from [{0}$]", SHEETNAME);
                        ExcelDataAccess.SourceFile = sourcefile;
                        ExcelDataAccess.OpenConnectionExcel();
                        DataSet    ds = ExcelDataAccess.GetDataSet(excelquery);
                        TinNhanSMS temp;
                        if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                if (row[0].ToString().Length > 3)
                                {
                                    temp              = new TinNhanSMS();
                                    temp.SoDienThoai  = row[0].ToString();
                                    temp.TenNhanVien  = row[1].ToString();
                                    temp.ThongTinThem = row[2].ToString();
                                    _listData.Add(temp);
                                }
                            }
                        }
                        ExcelDataAccess.CloseConnectionExcel();
                        gridMessage.DataSource = _listData;
                        gridViewMessage.RefreshData();
                    }
                    catch (Exception ex)
                    {
                        new MessageBoxBA().Show(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                LogError.WriteLogError("btnImportByExcel_Click: ", ex);
            }
        }