ITPub博客

首页 > Linux操作系统 > Linux操作系统 > excel 数据读取

excel 数据读取

原创 Linux操作系统 作者:taogchan 时间:2012-12-24 14:15:40 0 删除 编辑
        ///
        /// 读取Excel文档
        ///

        /// 文件名称
        /// 返回一个数据集
        public DataSet ExcelToDS(string Path)
        {
            string strConn = string.Empty;
            //= "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
            if (Path.ToLower().IndexOf(".xlsx") > 0)
            {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Path + "';Extended Properties='Excel 12.0;HDR=YES'";
            }
            if (Path.ToLower().IndexOf(".xls") > 0 && Path.EndsWith("xls"))
            {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Path + "';Extended Properties='Excel 8.0;HDR=YES;'";
            }

            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;
            strExcel = "select * from [sheet1$]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            ds = new DataSet();
            myCommand.Fill(ds, "table1");
            conn.Close();
            return ds;
        }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22392018/viewspace-751570/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-08-28

  • 博文量
    196
  • 访问量
    1242412