C# oledb读取Excel问题

2025-09-29 02:16:08

1、oledb方法读取代码为

using System.Data.OleDb;

using System.Data.SqlClient;

using System.Data;

/// <summary>

        /// 读取Excel文件到DataSet中

        /// </summary>

        /// <param name="filePath">文件路径</param>

        /// <returns>Excel数据DataSet</returns>

        private DataSet ExcelToDS(string filePath)

        {

            string connStr = "";

            string fileType = System.IO.Path.GetExtension(filePath);

            if (string.IsNullOrEmpty(fileType)) return null;

            //判断Excel类型xls \\ xlsx

            if (fileType == ".xls")

                connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";

            else

                connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";

            string sql_F = "Select * FROM [{0}]";

            OleDbConnection conn = null;

            OleDbDataAdapter da = null;

            DataTable dtSheetName = null;

            DataSet ds = new DataSet();

            try

            {

                // 初始化连接,并打开

                conn = new OleDbConnection(connStr);

                conn.Open();

                // 获取数据源的表定义元数据                        

                string SheetName = "";

                dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

                // 初始化适配器

                da = new OleDbDataAdapter();

                SheetName = (string)dtSheetName.Rows[0]["TABLE_NAME"];

                //将Excel中的数据复制到DataSet中

                if (!(SheetName.Contains("$") && !SheetName.Replace("'", "").EndsWith("$")))

                {

                    da.SelectCommand = new OleDbCommand(String.Format(sql_F, SheetName), conn);

                    DataSet dsItem = new DataSet();

                    da.Fill(dsItem);

                    ds.Tables.Add(dsItem.Tables[0].Copy());                    

                }

            }

            catch (Exception ex)

            {

                //this.WriteErrorXML(ex);//错误日志

            }

            finally

            {

                // 关闭连接

                if (conn.State == ConnectionState.Open)

                {

                    conn.Close();

                    da.Dispose();

                    conn.Dispose();

                }

            }

            return ds;

        }

2、问题:

若打开Excel文件中出现一下提示,则 conn.Open();这行代码会出错....

错误提示为:外部表不是预期的格式。

C# oledb读取Excel问题

3、暂时解决方案,只能将上传的Excel 进行另存为操作,这样提示就不会出现,可以正常上传

4、这种方法读取Excel,可能会出现读取了很多的空行数据,请注意判断删除,如

//DataSet ExcelConsert数据集

for (int rowIndex = 0; rowIndex < ExcelConsert.Tables[0].Rows.Count; rowIndex++)

            {

                if (string.IsNullOrEmpty(ExcelConsert.Tables[0].Rows[rowIndex][0].ToString()))

                {

                   ///某关键字为空,就停止

                   break;

                }

                else{

                  //........................................

                 }

            }

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢