程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 將excel數據轉換成dataset示例

將excel數據轉換成dataset示例

編輯:C#入門知識

將excel數據轉換成dataset示例。本站提示廣大學習愛好者:(將excel數據轉換成dataset示例)文章只能為提供參考,不一定能成為您想要的結果。以下是將excel數據轉換成dataset示例正文



/// <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();
            }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved