zoukankan      html  css  js  c++  java
  • C# 读取Excel



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.IO;
    using System.Data;
    using System.Data.OleDb;

    namespace Collector.Helper
    {
        
    public static class ExcelHelper
        {

            
    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;
            }

            
    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;
            }

            
    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;
            }
        


        }
    }
  • 相关阅读:
    centos服务器上线第二个django项目方法。
    centos7服务器部署django项目。
    C# 修饰符
    PLSQL 12 安装、连接Oracle
    GAC 解释&路径
    WebService 创建、发布、调用
    区域性名称和标识符
    关键字
    运算符 &(与运算)、|(或运算)、^(异或运算)
    ASCII,Unicode,UTF-8,GBK 区别
  • 原文地址:https://www.cnblogs.com/zyip/p/1880570.html
Copyright © 2011-2022 走看看