• Excel导入导出DataGridView


     1  /// <summary>
     2         /// excel表保存到dataTable中
     3         /// </summary>
     4         /// <param name="path">excel表路径</param>
     5         /// <returns>返回第一个sheet表</returns>
     6         private static DataTable ExcelToDataTable(string path)
     7         {
     8             DataTable dt = new DataTable();
     9             try
    10             {
    11                 //连接字符串     
    12                 string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意          
    13                 //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本     
    14                 using (OleDbConnection conn = new OleDbConnection(connstring))
    15                 {
    16                     conn.Open();
    17                     DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字   
    18                     string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字      
    19                     string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //查询字符串  
    20                     //string sql = string.Format("SELECT * FROM [{0}] WHERE [日期] is not null", firstSheetName); //查询字符串   
    21                     OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
    22                     DataSet set = new DataSet();
    23                     ada.Fill(set);
    24                     dt = set.Tables[0];
    25                 }
    26                 return dt;
    27             }
    28             catch (Exception ex)
    29             {
    30                 MessageBox.Show(ex.Message);
    31                 return null;
    32             }
    33         }
    View Code

     /// <summary>
            /// excel表保存到dataTable中
            /// </summary>
            /// <param name="path">excel表路径</param>
            /// <returns>返回第一个sheet表</returns>
            private static DataTable ExcelToDataTable(string path)
            {
                DataTable dt = new DataTable();
                try
                {
                    //连接字符串    
                    string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意         
                    //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本    
                    using (OleDbConnection conn = new OleDbConnection(connstring))
                    {
                        conn.Open();
                        DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字  
                        string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字     
                        string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //查询字符串 
                        //string sql = string.Format("SELECT * FROM [{0}] WHERE [日期] is not null", firstSheetName); //查询字符串  
                        OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
                        DataSet set = new DataSet();
                        ada.Fill(set);
                        dt = set.Tables[0];
                    }
                    return dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return null;
                }
            }

     1         /// <summary>
     2         /// excel表保存到dataTable中
     3         /// </summary>
     4         /// <param name="path">excel表路径</param>
     5         /// <returns>返回第一个sheet表</returns>
     6         private static DataTable ExcelToDataTable(string path)
     7         {
     8             DataTable dt = new DataTable();
     9             try
    10             {
    11                 //连接字符串     
    12                 string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意          
    13                 //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本     
    14                 using (OleDbConnection conn = new OleDbConnection(connstring))
    15                 {
    16                     conn.Open();
    17                     DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字   
    18                     string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字      
    19                     string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //查询字符串  
    20                     //string sql = string.Format("SELECT * FROM [{0}] WHERE [日期] is not null", firstSheetName); //查询字符串   
    21                     OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
    22                     DataSet set = new DataSet();
    23                     ada.Fill(set);
    24                     dt = set.Tables[0];
    25                 }
    26                 return dt;
    27             }
    28             catch (Exception ex)
    29             {
    30                 MessageBox.Show(ex.Message);
    31                 return null;
    32             }
    33         }
    View Code

    网上查找的方法,忘记在哪找的了,在这记录下:

     /// <summary>
            /// excel表保存到dataTable中
            /// </summary>
            /// <param name="path">excel表路径</param>
            /// <returns>返回第一个sheet表</returns>
            private static DataTable ExcelToDataTable(string path)
            {
                DataTable dt = new DataTable();
                try
                {
                    //连接字符串    
                    string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意         
                    //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本    
                    using (OleDbConnection conn = new OleDbConnection(connstring))
                    {
                        conn.Open();
                        DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" }); //得到所有sheet的名字  
                        string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字     
                        string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //查询字符串 
                        //string sql = string.Format("SELECT * FROM [{0}] WHERE [日期] is not null", firstSheetName); //查询字符串  
                        OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
                        DataSet set = new DataSet();
                        ada.Fill(set);
                        dt = set.Tables[0];
                    }
                    return dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return null;
                }
            }

  • 相关阅读:
    动态字节码技术Javassist
    自己实现简单版的注解Mybatis
    AOP实现事务和记录日志
    自己实现简单版SpringMVC
    静态变量
    docker安装nginx , 安装mysql5.6,安装redis3.2
    Worker Thread模式
    linux 安装jdk
    dockfile构建自己的tomcat
    docker使用2
  • 原文地址:https://www.cnblogs.com/20191204C/p/12016295.html
Copyright © 2020-2023  润新知