• C#读取Excel数据


            /// <summary>
            
    /// 返回Excel数据源
            
    /// </summary>
            
    /// <param name="filePath">文件路径</param>
            
    /// <returns></returns>
            public static System.Data.DataTable ReadExcel(string filePath)
            {
                //strConn = "Provider=Microsoft.Jet.  OLEDB.4.0;Data Source=C:\\Erp1912.xls;Extended   Properties='Excel8.0;HDR=Yes;IMEX=1'";  
                
    //通过Imex=1来把混合型作为文本型读取,避免null值

                System.Data.DataTable tab = new System.Data.DataTable();
                string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + filePath;
                OleDbConnection myConn = new OleDbConnection(strCon);
                try
                {
                    myConn.Open();

                    //获取所有Sheet名称
                    DataTable sheetNames = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { nullnullnull"TABLE" });
                    string strCom = string.Format(" SELECT * FROM [{0}]", sheetNames.Rows[0]["TABLE_NAME"]);
                    OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
                    myCommand.Fill(tab);
                    return tab;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    myConn.Close();
                }
            }
            #endregion
        }
  • 相关阅读:
    Unity3d TweenPosition.Begin()的使用浅析
    Unity3D 代理的使用及获取两个碰撞器的碰撞点
    Unity3D 调用GPS位置服务实现代码
    在控制台中输入msqyl一直报ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost'错误
    eclipse 提示错误The method of type must override a superclass method 的解决办法
    Linux高级命令
    Centos7安装及配置
    Linux基本命令
    多线程
    java总结
  • 原文地址:https://www.cnblogs.com/beijia/p/readExcel.html
Copyright © 2020-2023  润新知