/// <summary>
        /// 读取excel
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static DataSet getExcelData(string filePath, ref string error)
        {
            try
            {
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0'";

                //EXCEL表名验证
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                DataTable sheetNames = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                conn.Close();
                List<string> list1 = new List<string>();
                foreach (DataRow dr in sheetNames.Rows)
                {
                    list1.Add((String)dr["TABLE_NAME"]);
                }

                //excel数据读取
                DataSet ds = new DataSet();
                OleDbDataAdapter oada = new OleDbDataAdapter("select * from [Excel中表名字$]", strConn);
                oada.Fill(ds);

                //数据验证
                string mess = string.Empty;
                if (checkExcel(ds.Tables[0], ref mess))
                {
                    return ds;
                }
                else
                {
                    error = mess;
                    return null;
                }
            }
            catch (Exception ex)
            {
                error = "读取Excel错误:" + ex.Message;
                return null;
            }
        }

相关文章:

  • 2022-12-23
  • 2021-07-13
  • 2022-12-23
  • 2021-08-12
  • 2021-10-18
  • 2021-10-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2021-07-24
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
相关资源
相似解决方案