首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

如何从Excel中读取数据

2013-12-13 
怎么从Excel中读取数据怎么从Excel表中从第三行开始,读取第二列的数据到没有数据为止,然后从中取出最大和

怎么从Excel中读取数据
怎么从Excel表中从第三行开始,读取第二列的数据到没有数据为止,然后从中取出最大和最小值
[解决办法]
C# 有一套单独的访问EXCEL数据的方法,类似ADO.NET 的。你可以根据需要设定where条件


 /// <summary>
        /// Excel数据连接字符串
        /// </summary>
        private const string EXCELConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES;IMEX=1';Data Source=";

        /// <summary>
        /// 读取Excel到DataTable
        /// </summary>
        /// <param name="Path"></param>
        public static System.Data.DataTable ReadExcelToDataTable(string Path)
        {
            DataSet ds = new DataSet();
            string queryString = "SELECT * FROM [Sheet1$] where 学号 <> ''";
            string path = EXCELConnString + Path;
            using (OleDbConnection connection = new OleDbConnection(path))
            {
                try
                {
                    OleDbCommand oleCommand = new OleDbCommand(queryString, connection);
                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
                    oleAdapter.Fill(ds, "[Sheet1$]");
                }
                catch (System.Exception)
                {
                    return null;
                }
            }
            // 数据绑定 
            return ds.Tables[0];
        }


[解决办法]
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

C# 有一套单独的访问EXCEL数据的方法,类似ADO.NET 的。你可以根据需要设定where条件

 /// <summary>
        /// Excel数据连接字符串
        /// </summary>
        private const string EXCELConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES;IMEX=1';Data Source=";

        /// <summary>
        /// 读取Excel到DataTable
        /// </summary>


        /// <param name="Path"></param>
        public static System.Data.DataTable ReadExcelToDataTable(string Path)
        {
            DataSet ds = new DataSet();
            string queryString = "SELECT * FROM [Sheet1$] where 学号 <> ''";
            string path = EXCELConnString + Path;
            using (OleDbConnection connection = new OleDbConnection(path))
            {
                try
                {
                    OleDbCommand oleCommand = new OleDbCommand(queryString, connection);
                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
                    oleAdapter.Fill(ds, "[Sheet1$]");
                }
                catch (System.Exception)
                {
                    return null;
                }
            }
            // 数据绑定 
            return ds.Tables[0];
        }


你这句代码private const string EXCELConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES;IMEX=1';Data Source=";
是什么意思?是不是设置的Excel的地址的

相当于数据库连接字符串。

那不需要连接制定的Excel的吗,怎么读取制定Excel里的数据?


这个链接字符串要注意Excel 的版本  Microsoft.ACE.OLEDB.12.0
[解决办法]
引用:
未能找到类型或命名空间名称“OleDbCommand”(是否缺少 using 指令或程序集引用?)
这个怎么办??


Shift+Alt+ F10 快捷键添加引用

热点排行