silence-blog

从Excel中读取数据,导入sqlserver

2016-07-05 17:56  silence_blog  阅读(365)  评论(0编辑  收藏  举报
class Program
    {
        static string connString = "Data Source=1.1.1.1;INITIAL CATALOG=dbname;USER ID=userid;PASSWORD=password;";
        static SqlConnection conn;
        static void Main(string[] args)
        {
            //连接excel数据库
            //读取数据到dataSet
            DataTable dt = ReadFromExcel();

            //连接sqlserver数据库
            //循环dataSet,拼接insert
            WriteToExcel(dt);

        }

        private static void WriteToExcel(DataTable dt)
        {
            conn = new SqlConnection(connString);
            conn.Open();
            DataRow dr = dt.NewRow();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                //拼接sql并执行
                InsertIntoSql(dr);
            }
        }

        private static void InsertIntoSql(DataRow dr)
        {           
            string DepartmentId = dr[0].ToString();
            string Name = dr[1].ToString();
            string Code = dr[2].ToString();
            string DeptStatus = dr[3].ToString();
            string ParentDeptCode = dr[4].ToString();
            string DeptLevel = dr[5].ToString();
            string DeptCOACode = DataRowValue(dr, 6);
            string DEPTSAPCode = DataRowValue(dr, 7);
            string DeptMngerID = DataRowValue(dr, 8);
            string DeptSecretaryID = DataRowValue(dr, 9);

            string sql = @"insert into xx(xxx,xx,xx,xxx,xx,xx,x,x,x,x) values(\'{0}\',\'{1}\',\'{2}\',{3},\'{4}\',{5},\'{6}\',\'{7}\',\'{8}\',\'{9}\')";
            string insertSql = string.Format(sql, DepartmentId, Name, Code, DeptStatus, ParentDeptCode, DeptLevel, DeptCOACode, DEPTSAPCode, DeptMngerID, DeptSecretaryID);
            SqlCommand cmd = new SqlCommand(insertSql, conn);
            cmd.ExecuteNonQuery();
        }

        private static string DataRowValue(DataRow dr, int p)
        {
            if (!dr.IsNull(p))
            {
                return dr[p].ToString();
            }
            else return "null";
        }


        private static DataTable ReadFromExcel()
        {
            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Users/xxx/Downloads/tmp.xlsx;Extended Properties=\'Excel 8.0;HDR=NO;IMEX=1\';"; //连接字符串

            OleDbConnection Conn = new OleDbConnection(ConnStr);
            Conn.Open();
            string sql = string.Format("select * from [{0}$]", "Sheet1");  //Sheet1为工作表的名称
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(sql, Conn);
            da.Fill(ds);   //读取数据填充到DataSet
            return ds.Tables[0];
        }
    }

 

 //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + Server.MapPath("ExcelFiles/MyExcelFile.xls") + ";Extended Properties=\'Excel 8.0; HDR=Yes; IMEX=1\'"; //此连接只能操作Excel2007之前(.xls)文件
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Server.MapPath("ExcelFiles/Mydata2007.xlsx") + ";Extended Properties=\'Excel 12.0; HDR=Yes; IMEX=1\'"; //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串)
//备注: "HDR=yes;"是说Excel文件的第一行是列名而不是数据,"HDR=No;"正好与前面的相反。
//      "IMEX=1 "如果列中的数据类型不一致,使用"IMEX=1"可必免数据类型冲突。 

            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]", conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "Book1");
            this.GridView1.DataSource = ds.Tables["Book1"].DefaultView;
            this.GridView1.DataBind(); 

 

分类:

技术点:

相关文章: