ExcelDataHelper 帮助类,可将Excel转成DataTable

ExcelDataHelper 帮助类,可将Excel转成DataTable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace ToJson
{
   public class ExcelDataHelper
    {
       public static DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName, bool isBeforeExcel2007 = false)
        {
            string strConn = GetConStr(FileFullPath, isBeforeExcel2007);
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn);
            odda.Fill(ds, SheetName.Replace("$",""));
            conn.Close();
           if (ds.Tables.Count>=1)
           {
                dt= ds.Tables[0];
           }
           return dt;
          
        }
        public static DataTable GetExcelSheets(string FileFullPath, bool isBeforeExcel2007 = false)
        {
            string strConn = GetConStr(FileFullPath, isBeforeExcel2007);
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            // 得到包含数据架构的数据表
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            conn.Close();
            return dt;
        }
        public static DataTable GetExcelColumns(string FileFullPath, string tableName, bool isBeforeExcel2007 = false)
        {
            string strConn = GetConStr(FileFullPath, isBeforeExcel2007);
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, tableName, null });
            conn.Close();
            return dt;
        }
        public static string GetConStr(string FileFullPath, bool isBeforeExcel2007=false)
        {
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'"; //此連接可以操作.xls與.xlsx文件HDR=YES第一行为列名
            if (isBeforeExcel2007)
            {
                strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'"; //此連接只能操作Excel2007之前(.xls)文件
            }
            return strConn;
        }

    }
}