各位,请问谁做过将Excel中的数据通过C#程序导入到Sql server中呢?我现在遇到了一个问题,可是自己却解决不了了,希望给位能给点建议。。
下面是我的程序:
#region 该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径, sheetname为excel文件的表名   
        public void ExcelDataSource(string filepath)
        {
            ArrayList excelList=ExcelSheetName(filepath);
            for (int ii = 0; excelList != null && ii < excelList.Count;ii++ ) 
            {
                string sheetname = excelList[ii].ToString();
                if (!sheetname.Contains("Print_Titles"))
                {
                    string strConn;
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
                    OleDbConnection conn = new OleDbConnection(strConn);
                    OleDbDataAdapter oda = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);
                    DataSet ds = new DataSet();
                    oda.Fill(ds);
                    if (sheetname == "职务$")
                    {
                        xslTosql1(ds);
                    }
                    if (sheetname == "目录$")
                    {
                        xslTosql(ds);
                    }
                }
            }
        }
        #endregion#region 获得Excel中的所有sheetname(工作单名)
        public ArrayList ExcelSheetName(string filepath)
        {
            ArrayList excelList = new ArrayList();
            string strConn;
            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            DataTable dt = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            // (System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            conn.Close();            
            foreach (DataRow dr in dt.Rows)
            {
                excelList.Add(dr[2]);
            }
            return excelList;
        }
        #endregion#region Excel数据导入SQL数据库(职务工作表单)
        private void xslTosql1(DataSet ds)
        {
            for (int i = 2; i < ds.Tables[0].Rows.Count; i++)
            {
                string appointDate = ds.Tables[0].Rows[i]["任职时间"].ToString();
                string removeDate = ds.Tables[0].Rows[i]["免职时间"].ToString();
                string webSite = ds.Tables[0].Rows[i]["部门"].ToString();
                string registerNumber = ds.Tables[0].Rows[i]["职务"].ToString();
                string postNumber = ds.Tables[0].Rows[i]["批准文电号"].ToString();                //插入语句;
            }
        }
        #endregion#region Excel数据导入SQL数据库(目录工作表单)
        private void xslTosql(DataSet ds)
        {
            for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
            {
                string typeNum = ds.Tables[0].Rows[i]["类号"].ToString();
                string fileName = ds.Tables[0].Rows[i]["材料名称"].ToString();
                string webSite = ds.Tables[0].Rows[i]["年"].ToString();
                string registerNumber = ds.Tables[0].Rows[i]["月"].ToString();
                string postNumber = ds.Tables[0].Rows[i]["日"].ToString();
                string themeNumber = ds.Tables[0].Rows[i]["份数"].ToString();
                string grade = ds.Tables[0].Rows[i]["页数"].ToString();
                string forumName = ds.Tables[0].Rows[i]["备注"].ToString();
                //插入语句;
            }
        }
        #endregion我调试的时候,程序报错: 列“类号”不属于表 Table。 我知道是跟我Excel的结构有关系。