日期:2014-05-19  浏览次数:20494 次

[求助]100分求 excel到如sqlserver实例。谢谢。
要求:
现有excel文件E.xls。里面有字段a、b。
sqlserver数据库DB中有表T。字段有id(自动编号)、f、s。
求实例,谢谢。

------解决方案--------------------
insert into t (a,b) select a,b from OPENROWSET( 'MICROSOFT.JET.OLEDB.4.0 ', 'Excel 5.0;HDR=YES;DATABASE=1.xsl ',sheet1$)
------解决方案--------------------
(六)Excel --> SQL Server
http://community.csdn.net/Expert/topic/5002/5002120.xml?temp=.4375727
http://community.csdn.net/Expert/topic/5086/5086098.xml?temp=.6402094
http://community.csdn.net/Expert/topic/4940/4940738.xml?temp=.8665125
http://community.csdn.net/Expert/topic/4938/4938969.xml?temp=1.918972E-03
http://community.csdn.net/Expert/topic/4892/4892120.xml?temp=.1775324

------解决方案--------------------
#region 将上传Excel文件的数据导入到SQL Server数据库中
private bool TransitData(string FileName)
{
//EXCEL 的连接串
string strConn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "+FileName;//Server.MapPath(FileName);
strConn1+= ";Extended Properties=Excel 8.0; ";

//建立数据库操作必须对象
OleDbConnection objConn = new OleDbConnection(strConn1);
OleDbCommand objCmdSelect=new OleDbCommand();
DataSet objDataset1=new DataSet();
OleDbDataAdapter objAdapter1=new OleDbDataAdapter();

try
{
objConn.Open();

//将Excel数据导入到DataSet
objCmdSelect =new OleDbCommand( "SELECT * FROM [Sheet1$] ", objConn);
objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;
objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData ");

//通过DataSet,将数据导入到SQL Server
DataTable dt = objDataset1.Tables[0];
if(dt.Rows.Count> 0)
{
//加入时间是为了便于显示最新内容
string PubsDate=DateTime.Now.ToShortDateString()+ " "+DateTime.Now.Hour.ToString()+ ": ";
PubsDate+=DateTime.Now.Minute.ToString()+ ": "+DateTime.Now.Second.ToString();

for(int i=0;i <dt.Rows.Count;i++)
{
//只有在文章标题不为空的情况下,才能添加
if(dt.Rows[i].ItemArray[0].ToString().Length> 0)
{
Langzi.BookArticle myArticle=new Langzi.BookArticle();

myArticle.Title=dt.Rows[i].ItemArray[0].ToString();
myArticle.Year=dt.Rows[i].ItemArray[1].ToString();
myArticle.Qi=dt.Rows[i].ItemArray[2].ToString();
myArticle.LanMu=dt.Rows[i].ItemArray[3].ToString();
myArticle.Department=dt.Rows[i].ItemArray[4].ToString();
myArticle.Authors=dt.Rows[i].ItemArray[5].ToString();
myArticle.KeyWord=dt.Rows[i].ItemArray[6].ToString();
myArticle.Page=int.Parse(dt.Rows[i].ItemArray[7].ToString());
myArticle.ClickNum=0;
myArticle.PubsDate=PubsDate;

bool isAdd=myArticle.AddNew();
if(!isAdd)
{
return false;
}
}//end if
}//end for
}//end if
}//end try
catch(OleDbException ex)
{
string strMsg= "对不起,操作Excel文件时出现意外: "+ex.Message.Replace( " ' ", " ")+ "请关闭服务器上所有打开的Excel文件! ";
strMsg= "alert( ' "+strMsg+ " '); ";
strMsg= " <script language= 'javascript '> "+strMsg+ " </script> ";
Response.Write(strMsg);

return false;
}
catch(SqlException ex)
{
string strMsg= "对不起,生成信息时出现意外: