我现在想把数据从excel中全取出来然后放到一个集合中,以后将取出来的数据拿来用时就不用再次连接数据库了,现在没有想到一个好的方法还请各位提示一下。

解决方案 »

  1.   

                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + 110 + ";Extended Properties= 'Excel 8.0;HDR=YES;IMEX=1'"; 
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();            DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                DataSet ds = new DataSet();
                //一个EXCEL文件可能有多个工作表,遍历之
                foreach (DataRow dr in dtSchema.Rows)
                {
                    string table = dr["TABLE_NAME"].ToString();
                    string strExcel = "SELECT * FROM [" + table + "]";
                    ds.Tables.Add(table);
                    OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, conn);
                    myCommand.Fill(ds, table);   //()
                }
      

  2.   

    /// <summary>
            /// 导入EXCEL到DataSet
            /// </summary>
            /// <param name="fileName">Excel全路径文件名</param>
            /// <returns>导入成功的DataSet</returns>
            public DataSet doImport(string fileName)
            {
                //判断是否安装EXCEL
                Microsoft.Office.Interop.Excel.Application xlApp=new Microsoft.Office.Interop.Excel.Application(); 
                if(xlApp==null)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel","消息提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Error);
                    return null;
                } 
                //判断文件是否被其他进程使用 
                Microsoft.Office.Interop.Excel.Workbook workbook; 
                try
                {
                    //workbook = xlApp.Workbooks.Open(fileName,0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
                    workbook = xlApp.Workbooks.Open(fileName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, missing, missing, missing, missing, missing, missing, missing);
                }
                catch
                { 
                    MessageBox.Show("Excel文件处于打开状态,请保存关闭","消息提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Error);
                    return null;
                }             //获得所有Sheet名称
                int n = workbook.Worksheets.Count;
                string[] SheetSet = new string[n];
                System.Collections.ArrayList al = new System.Collections.ArrayList();
                for(int i=1; i<=n; i++)
                {
                    SheetSet[i-1] = ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i]).Name;
                }            //释放Excel相关对象
                workbook.Close(null,null,null); 
                xlApp.Quit();
                if(workbook != null)
                { 
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    workbook = null;
                }
                if(xlApp != null)
                { 
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    xlApp = null;
                } 
                GC.Collect();            //把EXCEL导入到DataSet
                DataSet ds = new DataSet();
                string ext = Path.GetExtension(fileName).ToLower();
                string strConn="";
                if (ext == ".xls")
                {
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
                }
                else if (ext == ".xlsx")
                {
                    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES' ";
                }
                using (OleDbConnection conn = new OleDbConnection(strConn))
                {
                    try
                    {
                        conn.Open();
                        OleDbDataAdapter da;
                        for (int i = 1; i <= n; i++)
                        {
                            string sql = "select * from [" + SheetSet[i - 1] + "$] ";
                            da = new OleDbDataAdapter(sql, conn);
                            da.Fill(ds, SheetSet[i - 1]);
                            da.Dispose();
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Excel文件不是预期的版本", "消息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        conn.Close();
                        conn.Dispose();
                    }   
                } 
                return ds;
            }
      

  3.   

    楼主还是没明白我的意思,就是不需要读取excel数据,只将excel中的多条记录放到数组中如何放。
      

  4.   

    用数组存 貌似很烦 
    貌似有很多容器可以解决类似问题.List Dictionary 等等