读取Excel

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

using System.Collections;
using System.Data;
using System.Data.OleDb;

namespace ExcelTest1
{
    class ExcelToData
    {
        public string FullPath { get; set; }
        string StrConn { get; set; }
        OleDbConnection conn;
        public ExcelToData(string fullPath)
        {
            this.FullPath = fullPath;
            this.StrConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + fullPath + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
        }

        /// <summary>
        /// 读取excel的sheet()
        /// </summary>
        /// <returns></returns>
        public ArrayList ReadExcelFileToSheets()
        {
            conn = new OleDbConnection(StrConn);
            ArrayList array = new ArrayList();
            try
            {
                conn.Open();
                DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    array.Add(dt.Rows[i]["TABLE_NAME"].ToString().Trim());
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return array;
        }

        /// <summary>
        /// 读取指定sheet的excel数据
        /// </summary>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public List<DataTable> ReadExcelFileToTable(ArrayList sheetName)
        {
            List<DataTable> dtList = new List<DataTable>();          
            foreach (var item in sheetName)
            {
                DataTable dt = new DataTable();
                OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [" + item + "]", conn);
                adapter.Fill(dt);
                dtList.Add(dt);
            }
            conn.Close();
            return dtList;
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;

using System.Data;
using System.Data.OleDb;
using System.Collections;


namespace ExcelTest1
{
    class Program
    {
        //public static OleDbConnection conn;
        static void Main(string[] args)
        {
            string tmpFilePath = @"F:\wenbin\工作文件\KPI\KPI导出\2016.11\新建 Microsoft Excel 工作表.xlsx";
            ExcelToData excelToData = new ExcelToData(tmpFilePath);
            ArrayList sheets = excelToData.ReadExcelFileToSheets();
            List<System.Data.DataTable> dtList = excelToData.ReadExcelFileToTable(sheets);
            //Application ThisApplication = new Application();
            //Workbook ThisWorkBook;
            //object missing = System.Reflection.Missing.Value;
            //ThisWorkBook = ThisApplication.Workbooks.Open(tmpFilePath);
            //Worksheet ThisWorksheet = ThisWorkBook.Sheets[1];
            //ThisApplication.Visible = false;
            Console.ReadKey();
        }
    }

}

 

posted @ 2017-02-13 12:16  花生打代码会头痛  阅读(95)  评论(0)    收藏  举报