using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using iExcel = Microsoft.Office.Interop.Excel;
namespace Excel.Readxls
{
    class readxls
    {
        public string xlscon = null;
        public string strCom = "SELECT * FROM [Sheet1$]";
        public string xlsFileName(string strFileName)
        {   xlscon=@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source='"+strFileName+"'; Extended Properties=\'Excel 8.0;HDR=Yes;IMEX=1;\'";
            //MessageBox.Show(xlscon);
            return xlscon;
        }


        public DataGridView Readxls(ref DataGridView dgv)
        {
            if (xlscon == null)
            {
                MessageBox.Show("请选择文件");
                return null;
            }
            OleDbConnection conn = new OleDbConnection(xlscon);
            conn.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(strCom, conn);
            DataTable dt = new DataTable();
            oda.Fill(dt);
            dgv.DataSource = dt;
            return dgv;
        }
        public DataGrid dgReadxls(ref DataGrid dg)
        {
            if (xlscon == null)
            {
                MessageBox.Show("请选择文件");
                return null;
            }
            OleDbConnection conn = new OleDbConnection(xlscon);
            conn.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(strCom, conn);
            DataSet ds = new DataSet();
            oda.Fill(ds);
            dg.DataSource = ds;
            return dg;
        }