首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么将EXCEL文件中的数据直接导入sql中的表格里

2012-02-10 
如何将EXCEL文件中的数据直接导入sql中的表格里?如题 我找的一个代码在导入的时候时将文件先上传到服务器,

如何将EXCEL文件中的数据直接导入sql中的表格里?
如题 我找的一个代码在导入的时候时将文件先上传到服务器,然后在传到数据库。但在传到数据库的时候就失败了。求高手正解!求源码

[解决办法]

C# code
public DataSet ds(){    string newName = Server.MapPath("..//App_Data//Uploads//") + DateTime.Now.ToString("hhmmss") + ".xls";                FileUpload1.SaveAs(newName);                // string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newName + ";Extended Properties=Excel 8.0;";                string connStr = string.Empty;                if (type3.Equals(".xlsx"))                {                    connStr = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + newName +                              ";Extended Properties='Excel 12.0 Xml; HDR=YES; IMEX=1'";                }                else if (type3.Equals(".xls"))                {                    connStr = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + newName +                              ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";                }                OleDbConnection conn = new OleDbConnection(connStr);                if (conn.State.ToString() == "Closed")                {                    conn.Open();                }                DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);                var tableName = dt.Rows[0][2].ToString().Trim();                var str1 = string.Format("Select * from [{0}]", tableName);                OleDbDataAdapter oda = new OleDbDataAdapter(str1, conn);                DataSet ds = new DataSet();                oda.Fill(ds);                conn.Close();                File.Delete( newName );                return ds} 

热点排行