亲宝软件园·资讯

展开

excel数据转换 将excel数据转换成dataset示例

人气:0
想了解将excel数据转换成dataset示例的相关内容吗,在本文为您仔细讲解excel数据转换的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:excel数据转换,下面大家一起来学习吧。

复制代码 代码如下:

/// <summary>
        /// EXCEL数据转换DataSet
        /// </summary>
        /// <param name="filePath">文件全路径</param>
        /// <param name="search">表名</param>
        /// <returns></returns>      
        private DataSet GetDataSet(string fileName)
        {
            string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";
            OleDbConnection objConn = null;
            objConn = new OleDbConnection(strConn);
            objConn.Open();
            DataSet ds = new DataSet();
            //List<string> List = new List<string> { "收款金额", "代付关税", "垫付费用", "超期", "到账利润" };
            List<string> List = new List<string> { };
            DataTable dtSheetName = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            foreach (DataRow dr in dtSheetName.Rows)
            {
                if (dr["Table_Name"].ToString().Contains("$") && !dr[2].ToString().EndsWith("$"))
                {
                    continue;
                }
                string s = dr["Table_Name"].ToString();
                List.Add(s);
            }
            try
            {
                for (int i = 0; i < List.Count; i++)
                {
                    ds.Tables.Add(List[i]);
                    string SheetName = List[i];
                    string strSql = "select * from [" + SheetName + "]";
                    OleDbDataAdapter odbcCSVDataAdapter = new OleDbDataAdapter(strSql, objConn);
                    DataTable dt = ds.Tables[i];
                    odbcCSVDataAdapter.Fill(dt);
                }
                return ds;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                objConn.Close();
                objConn.Dispose();
            }
        }

加载全部内容

相关教程
猜你喜欢
用户评论