Excel中 随机抽取n行 存储在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();
        }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。