zoukankan      html  css  js  c++  java
  • winfrom 导入Excel表到access数据库(来自小抽奖系统)

    网上有很多这种方法,本人只是针对自己的系统来实现的

     //导入excel表
            private void ImportTSMenu_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFD = new OpenFileDialog();
                openFD.Filter = "Excel文件|*.xls";
                if (openFD.ShowDialog(this.Owner) == DialogResult.OK)
                {
                    string strFilename = openFD.FileName;
                    if (!File.Exists(strFilename))
                    {
                        MessageBox.Show("文件不存在,请重新选择文件!");
                        return;
                    }
                    //string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + strFilename + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                    string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFilename + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
                    //关于OleDB连接Excel的Extended Properties(扩展属性)HDR=YES; IMEX=2
                    //HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES
                    //参数Excel 8.0 对于Excel 97以上到2003版本都用Excel 8.0,2007或2010的都用Extended Properties=Excel 12.0
                    //IMEX 有三种模式:
                    //当 IMEX=0 时为“汇出模式”,这个模式开启的 Excel 档案只能用来做“写入”用途。
                    //当 IMEX=1 时为“汇入模式”,这个模式开启的 Excel 档案只能用来做“读取”用途。
                    //当 IMEX=2 时为“连結模式”,这个模式开启的 Excel 档案可同时支援“读取”与“写入”用途。
                    OleDbDataAdapter oldAda = new OleDbDataAdapter("select * from [Sheet1$]", strConn);
                    DataSet ds = new DataSet();
                    oldAda.Fill(ds);                    //C#导入execl 找不到可安装的 ISAM,如果是Office2003版本
                    //string strConn = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + excelFile + ";Extended Properties=‘Excel 8.0; HDR=YES; IMEX=1’";
                    //如果是2007以上
                    //string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
                    DataTable dt = ds.Tables[0];
                    InsertData(dt);
                    //this.FTLucky_Load(sender, e);
                    GetData();
                }
            }
    View Code

    下面有for循环一条条插入到数据库可能效率不高,应该有批量导入的,好像sql是用SqlBulkCopy,至于access的话,由于时间紧,且要求不高,就暂时用for代替了

     #region 3、将excel数据插入到数据库
            /// <summary>
            /// 将DataTable的数据插入到数据库
            /// </summary>
            /// <param name="dt">excel数据</param>
            private void InsertData(DataTable dt)
            {
                try
                {
                    oledCon = sqlHelper.OledCon();
                    oledCon.Open();
                    string strInsert = "";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        strInsert = "insert into Information([UserName],[Phone])  values('" + dt.Rows[i][0] + "','" + dt.Rows[i][1] + "')";
                        oledCmd = new OleDbCommand(strInsert, oledCon);
                        oledCmd.ExecuteNonQuery();
                    }
                    MessageBox.Show("导入成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导入出错,原因:" + ex.Message.ToString());
                }
                finally
                {
                    oledCon.Dispose();
                    oledCon.Close();
                }
            } 
            #endregion
    View Code
  • 相关阅读:
    SEO之关键词选择
    我所了解的搜索引擎工作原理
    搜索引擎工作原理
    SEO定义目的,优化的好处
    今天开始我的博客旅程啦!
    [ABC209E] Shiritori
    Codeforces Global Round 12
    CF771E Bear and Rectangle Strips
    CF1392H ZS Shuffles Cards
    CF1439D INOI Final Contests
  • 原文地址:https://www.cnblogs.com/evan-success/p/4861690.html
Copyright © 2011-2022 走看看