예제 #1
0
        private void btnFileName_Click(object sender, EventArgs e)
        {
            if (dlgOpen.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            cbbTable.Items.Clear();
            txtFileName.Text = dlgOpen.FileName;
            string ext     = Pub.GetFileNameExt(dlgOpen.FileName).ToLower();
            string ConnStr = "Provider={0};Data Source=\"" + txtFileName.Text + "\";Extended properties='Excel {1};HDR=YES'";

            if (ext == "xlsx")
            {
                ConnStr = string.Format(ConnStr, "Microsoft.Ace.OleDb.12.0", "12.0 Xml");
            }
            else
            {
                ConnStr = string.Format(ConnStr, "Microsoft.Jet.OLEDB.4.0", "8.0");
            }
            try
            {
                db.Close();
                db.Open(255, ConnStr);
                DataTable dt = db.GetDataTableList();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cbbTable.Items.Add(dt.Rows[i]["TABLE_NAME"].ToString());
                }
            }
            catch (Exception E)
            {
                Pub.ShowErrorMsg(E);
            }
            if (cbbTable.Items.Count > 0)
            {
                cbbTable.SelectedIndex = 0;
            }
        }