博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

读取Excel文件到DataTable中

Posted on 2010-08-06 10:13  moss_tan_jun  阅读(759)  评论(0编辑  收藏  举报

private static string[] GetExcelSheetNames(OleDbConnection conn)
        {
            DataTable dtbSheets = null;
            String[] arrExcelSheets = null;
            using (conn)
            {
                try
                {
                    conn.Open();

                    // Get the data table containing the schema
                    dtbSheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

                    if (dtbSheets == null)
                    {
                        return null;
                    }

                    arrExcelSheets = new String[dtbSheets.Rows.Count];
                    int intI = 0;

                    // Add the sheet name to the string array.
                    foreach (DataRow dr in dtbSheets.Rows)
                    {
                        arrExcelSheets[intI] = dr["TABLE_NAME"].ToString();
                        intI++;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    // Close the connection
                    conn.Close();
                }
                return arrExcelSheets;
            }
        }

        private static DataTable GetDataTableFromXls(OleDbConnection conn, string spreadSheetName)
        {
            DataTable datTemp = null;

            using (conn)
            {
                try
                {
                    string strComand = "select * from [" + spreadSheetName + "]";

                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strComand, conn);
                    datTemp = new DataTable(spreadSheetName);
                    adapter.Fill(datTemp);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    // Close the connection
                    conn.Close();
                }
            }
            return datTemp;
        }
        // Get the spreadsheet that contain data
        public static DataTable GetDataTableWithData(string serverLocation)
        {
            OleDbConnection xlsConn = null;
            DataTable datXls = null;

            try
            {
                string strConnStr = null;
                // Connection string for Excel
                strConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + serverLocation + ";Extended Properties=\"Excel 8.0; HDR=NO; IMEX=1\"";
                xlsConn = new OleDbConnection(strConnStr);
                // Get Sheet names from an Excel Book
                string[] arrXls = GetExcelSheetNames(xlsConn);

                try
                {
                    foreach (string strSheet in arrXls)
                    {
                        xlsConn = new OleDbConnection(strConnStr);
                        datXls = GetDataTableFromXls(xlsConn, strSheet);
                        if (datXls != null && datXls.Rows.Count > 0)
                        {
                            break;
                        }
                    }
                }
                catch// (SqlException se)
                {
                    //throw new Exception(se.Message);
                }

                return datXls;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (xlsConn.State == ConnectionState.Open)
                {
                    xlsConn.Close();
                }
                xlsConn.Dispose();
            }
        }