导入excel,xls,xlsx



/// <summary>
        /// 文件.xlsx,获得xls内的DataTable
        /// </summary>
        /// <param name="filenameurl">文件保存地址</param>
        /// <param name="table">文件名称</param>
        /// <param name="IsXls">文件后缀名</param>
        /// <returns></returns>
        public DataSet ExecleDs(string filenameurl, string table, string IsXls)
        {
            try
            {
                string strConn;
                strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";
                //strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";

                //备注: "HDR=yes;"是说Excel文件的第一行是列名而不是数据,"HDR=No;"正好与前面的相反。
                //      "IMEX=1 "如果列中的数据类型不一致,使用"IMEX=1"可必免数据类型冲突。
                // string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Server.MapPath("ExcelFiles/Mydata2007.xlsx") + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                //DataTable FromExcel = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                动态获得表的名称
                //string tbname = FromExcel.Rows[4].ToString();
                DataSet ds = new DataSet();
                string strsql = "select * from [新增$]";
                OleDbDataAdapter odda = new OleDbDataAdapter(strsql, conn);
                odda.Fill(ds, table);
                return ds;
            }
            catch (Exception ee)
            {
                //Log.WriteErorrLog(ee);
                LogBLL.debug(ee.ToString());
                // Log.WriteLog("ERROR", ee.ToString());
                return null;
            }
        }
        /// <summary>
        /// 文件.xls,获得xls内的DataTable
        /// </summary>
        /// <param name="filenameurl">文件保存地址</param>
        /// <param name="table">文件名称</param>
        /// <param name="IsXls">文件后缀名</param>
        /// <returns></returns>
        public DataSet ExecleDs1(string filenameurl, string table, string IsXls)
        {
            try
            {
                string strConn;
                //strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";
                //strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
                strConn = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + filenameurl + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
                //备注: "HDR=yes;"是说Excel文件的第一行是列名而不是数据,"HDR=No;"正好与前面的相反。
                //      "IMEX=1 "如果列中的数据类型不一致,使用"IMEX=1"可必免数据类型冲突。
                // string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Server.MapPath("ExcelFiles/Mydata2007.xlsx") + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
                OleDbConnection conn = new OleDbConnection(strConn);

                conn.Open();
                // DataTable FromExcel = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                动态获得表的名称
                //  string tbname = FromExcel.Rows[4].ToString();
                DataSet ds = new DataSet();
                string strsql = "select * from [新增$]";
                OleDbDataAdapter odda = new OleDbDataAdapter(strsql, conn);
                odda.Fill(ds, table);
                return ds;
            }
            catch (Exception ee)
            {
                //Log.WriteErorrLog(ee);
                LogBLL.debug(ee.ToString());
                //Log.WriteLog("ERROR", ee.ToString());
                return null;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值