private DataSet ImportExcel(string strFileName)
        {
            if (strFileName == "")
                return null;
            string strConn = "";
            //截取文件名后缀,判断是03格式还是07格式
            string strFileExtension = strFileName.Substring(strFileName.IndexOf(".") + 1);
            //07格式
            if (strFileExtension == "xlsx")
            {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                    "Extended Properties='Excel 12.0;" +
                    "HDR=YES';data source=" + strFileName;
            }
            //03格式
            else if (strFileExtension == "xls")
            {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=" + strFileName + ";" +
                    "Extended Properties=Excel 8.0";
            }
            //非EXCEL格式
            else
            {
                lblInfo.Text = @"不支持的文件类型";
                return null;
            }
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            OleDbDataAdapter ExcelDA = new OleDbDataAdapter("SELECT StoreNumber FROM [Sheet1$]", conn);
            DataSet ExcelDs = new DataSet();
            try
            {
                ExcelDA.Fill(ExcelDs);
            }
            catch (Exception err)
            {
                lblInfo.Text = err.ToString();
            }
            conn.Close();
            return ExcelDs;
        }

 

posted on 2010-09-15 09:48  vibratea  阅读(208)  评论(0编辑  收藏  举报