zoukankan      html  css  js  c++  java
  • 导入本地Excel到DataSet中

            /// <summary>
            /// 导入本地Excel到DataSet中
            /// </summary>
            /// <param name="strFileSourse">文件的路径和文件全名,含扩展名</param>
            /// <returns></returns>
            public DataSet ExcelToDataSet(string strFileSourse)
            {
                DataSet ds = new DataSet();
                //Excel数据源(兼容03/10)
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileSourse + ";Extended Properties="Excel 12.0;HDR=YES;IMEX=1"";
    
                //连接
                OleDbConnection conn = new OleDbConnection(strConn);
                try
                {
                    conn.Open();
    
                    //获取Excel中所有的sheet
                    DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    
                    //把Excel中所有的sheet数据读到一个Table中
                    for (int i = 0; i < dtSheet.Rows.Count; i++)
                    {
                        string strSheetName = dtSheet.Rows[i]["TABLE_NAME"].ToString();
                        OleDbDataAdapter OleDa = new OleDbDataAdapter("select * from [" + strSheetName + "]", conn);
                        OleDa.Fill(ds, "TableName");
                        conn.Close();
                    }
                }
                catch (Exception)
                {
                    //System.Windows.Forms.MessageBox.Show(e.ToString());
                    throw;
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
    
                return ds;
            }
  • 相关阅读:
    Leetcode Unique Binary Search Trees
    Leetcode Decode Ways
    Leetcode Range Sum Query 2D
    Leetcode Range Sum Query
    Leetcode Swap Nodes in Pairs
    Leetcode Rotate Image
    Leetcode Game of Life
    Leetcode Set Matrix Zeroes
    Leetcode Linked List Cycle II
    CF1321A
  • 原文地址:https://www.cnblogs.com/TTonly/p/10349932.html
Copyright © 2011-2022 走看看