zoukankan      html  css  js  c++  java
  • ado读取excel

        public static class ExcelHelper
        {
            
    /// <summary>
            
    /// 读取所有的表(每个sheet映射为一个表)
            
    /// </summary>
            
    /// <param name="filepath"></param>
            
    /// <returns></returns>
            public static System.Collections.Generic.List<string> GetTables(string filepath)
            { 
                List
    <string> r=new List<string>();
                
    if (File.Exists(filepath))
                {
                    
    //string connStr="Provider=Microsoft.Jet." + "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + filepath;
                    string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filepath + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
                    
    using (OleDbConnection conn = new OleDbConnection(connStr))
                    {
                        conn.Open();
                        DataTable dt 
    = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        
    for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            r.Add(dt.Rows[i][
    2].ToString().Trim());
                        }
                    }
                }
                
    return r;
            }

            
    /// <summary>
            
    /// 读取表中的数据
            
    /// </summary>
            
    /// <param name="filepath"></param>
            
    /// <param name="tableName">表名</param>
            
    /// <returns></returns>
            public static DataTable GetTableData(string filepath,string tableName)
            {
                DataTable r 
    = new DataTable();
                
    if (File.Exists(filepath))
                {
                    
    //string connStr = "Provider=Microsoft.Jet." + "OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=FALSE;IMEX=1\";Data Source=" + filepath;
                    string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filepath + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
                    
    using (OleDbConnection conn = new OleDbConnection(connStr))
                    {
                        conn.Open();
                        
    string sql = "select * from [" + tableName+ "]";
                        OleDbCommand oc 
    = new OleDbCommand(sql, conn);
                        OleDbDataAdapter oda 
    = new OleDbDataAdapter(oc);
                        oda.Fill(r);
                    }
                }
                
    return r;
            }

            
    /// <summary>
            
    /// 读取表中的数据
            
    /// </summary>
            
    /// <param name="filepath"></param>
            
    /// <param name="tableIndex">表索引,几第n个表</param>
            
    /// <returns></returns>
            public static DataTable GetTableData(string filepath, int tableIndex)
            {
                var ts
    = GetTables(filepath);
                
    if (ts.Count > tableIndex)
                {
                    
    string tableName = ts[tableIndex];
                    DataTable r 
    = new DataTable();
                    
    if (File.Exists(filepath))
                    {
                        
    //string connStr = "Provider=Microsoft.Jet." + "OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=FALSE;IMEX=1\";Data Source=" + filepath;
                        string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filepath + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
                        
    using (OleDbConnection conn = new OleDbConnection(connStr))
                        {
                            conn.Open();
                            
    string sql = "select * from [" + tableName + "]";
                            OleDbCommand oc 
    = new OleDbCommand(sql, conn);
                            OleDbDataAdapter oda 
    = new OleDbDataAdapter(oc);
                            oda.Fill(r);
                        }
                    }
                    
    return r;
                }
                
    else
                {
                    
    return null;
                }
            }

            
    /// <summary>
            
    /// 获取表结构(表的字段)
            
    /// </summary>
            
    /// <param name="filepath"></param>
            
    /// <param name="tableName"></param>
            
    /// <returns></returns>
            public static System.Collections.Generic.List<string> GetTableFields(string filepath, string tableName)
            {
                List
    <string> r=new List<string>();
                
    if (File.Exists(filepath))
                {
                    
    //string connStr = "Provider=Microsoft.Jet." + "OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=FALSE;IMEX=1\";Data Source=" + filepath;
                    string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filepath + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
                    
    using (OleDbConnection conn = new OleDbConnection(connStr))
                    {
                        conn.Open();
                        
    string sql = "select * from [" + tableName + "] where 1=2";
                        OleDbCommand oc 
    = new OleDbCommand(sql, conn);
                        OleDbDataAdapter oda 
    = new OleDbDataAdapter(oc);
                        DataTable dt 
    = new DataTable();
                        oda.Fill(dt);
                        
    for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            r.Add(dt.Columns[i].Caption);
                        }
                    }
                }
                
    return r;
            }
        


        }
  • 相关阅读:
    haproxy 2.5 发布
    cube.js sql 支持简单说明
    基于graalvm 开发一个cube.js jdbc driver 的思路
    apache kyuubi Frontend 支持mysql 协议
    oceanbase 资源池删除说明
    基于obd 的oceanbase 扩容说明
    jfilter一个方便的spring rest 响应过滤扩展
    cube.js schema 定义多datasource 说明
    typescript 编写自定义定义文件
    meow 辅助开发cli 应用的工具
  • 原文地址:https://www.cnblogs.com/zyip/p/1959795.html
Copyright © 2011-2022 走看看