读取Excel文件到DataTable中

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();
            }
        }

原文地址:https://www.cnblogs.com/moss_tan_jun/p/1793747.html