码迷,mamicode.com
首页 > 数据库 > 详细

Excel中 随机抽取n行 存储在access中

时间:2014-10-25 02:08:25      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:excel 转换 部分 数据 到 access中

string[] re = new string [50];
        Random w = new Random();
        /// <summary>
        /// 产生50个不同的数 以随机抽题
        /// </summary>
        /// <returns></returns>
        private string[] Random()
        {
            int[] res = new int[50];
            int c = 0;
            do
            {
                int temp = w.Next(1, 200);
                if (!isa(temp, res)) res[c++] = temp;
            } while (c < 50);
            for (int i=0;i <res.Length ;i++)
            {
                re[i] = res[i].ToString();
            }
            return re;
        }
        private bool isa(int x, int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
                if (x == arr[i]) return true;
            return false;
        }
string strGetDataFromExcel = "";
        string strInsertIntoAccess = "";
        OleDbConnection oleDbConnAccess;
        OleDbConnection oleDbConnExcel;
        OleDbCommand oleDbCmdAccess;
        OleDbCommand oleDbCmdExcel;
        OleDbDataReader oleDbDataReaderExcel;
        //test.accdb 为 目标文件  www.xlsx为源文件
        private void ss()
        {
            string[] s = Random();
            oleDbConnAccess = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath.Trim() + "\\test.accdb");
            oleDbConnExcel = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source=" + Application.StartupPath.Trim() + "\\www.xlsx");
            oleDbConnExcel.Open();
            strGetDataFromExcel = "SELECT * FROM [Sheet1$]";
            oleDbCmdExcel = new OleDbCommand(strGetDataFromExcel, oleDbConnExcel);
            oleDbDataReaderExcel = oleDbCmdExcel.ExecuteReader();
            if (oleDbDataReaderExcel.HasRows == true)
            {
                oleDbConnAccess.Open();

                for (; ; )
                {
                    if (oleDbDataReaderExcel.Read() == true)
                    {
                        for (int i = 0; i < 31; i++)
                        {
                            if (oleDbDataReaderExcel.GetValue(0).ToString().Equals(s[i]))
                            {
                                strInsertIntoAccess = "insert into w(Q,A,B,C,D) values(‘" + oleDbDataReaderExcel.GetValue(1).ToString() + "‘,‘" + oleDbDataReaderExcel.GetValue(2).ToString() + "‘,‘" + oleDbDataReaderExcel.GetValue(3).ToString() + "‘,‘" + oleDbDataReaderExcel.GetValue(4).ToString() + "‘,‘" + oleDbDataReaderExcel.GetValue(5).ToString() + "‘) ";
                                oleDbCmdAccess = new OleDbCommand(strInsertIntoAccess, oleDbConnAccess);
                                oleDbCmdAccess.ExecuteNonQuery();
                                oleDbCmdAccess.Dispose();
                            }
                        }
                    }
                    else
                        break;
                }
                oleDbConnAccess.Close();
            }
            oleDbDataReaderExcel.Close();
            oleDbCmdExcel.Dispose();
            oleDbConnExcel.Close();
        }

Excel中 随机抽取n行 存储在access中

标签:excel 转换 部分 数据 到 access中

原文地址:http://8743025.blog.51cto.com/8733025/1567737

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!