日期:2014-05-16  浏览次数:20431 次

uploadfy导入Excel内容到数据库中4
public int ReadFromExcel(string fileName, string sheetName, System.Data.DataTable dataTable)
        {
            OleDbConnection connection = new System.Data.OleDb.OleDbConnection();
            try
            {
                string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
                string sql = "SELECT * FROM  [" + sheetName + "$]";

                connection = new OleDbConnection(connectionString);
                connection.Open();

                OleDbCommand command = new OleDbCommand(sql, connection);
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
                DataSet excelOriginalData = new DataSet();

                dataTable.Columns.Clear();
                dataTable.Clear();

                dataAdapter.Fill(dataTable);
            }
            catch (Exception e)
            {
                m_sMsg = e.Message;
                return -1;
            }
            finally
            {
                if (connection.State != System.Data.ConnectionState.Closed)
                {
                    connection.Close();
                }
                connection.Dispose();
            }
            return 1;
        }