aspexcel導入sql資料庫
① 請問如何用asp實現將Excel文件導入到access資料庫中( 急! 急!急!)
在ASP中怎樣把EXCEL導入ACCESS
demo.asp
<%
Dim cn,oConn,connstr
'打開XLS.
Set cn = Server.CreateObject("ADODB.Connection")
cn.Provider = "Microsoft.Jet.OLEDB.4.0 "
cn.ConnectionString = "Data Source=" & Server.MapPath("data.xls") & ";" & _
"Extended Properties=Excel 8.0;"
cn.Open
'打開MDB.
connstr="DBQ="+server.MapPath("data.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set oConn=server.CreateObject("ADODB.CONNECTION")
oConn.open connstr
'讀取數據.
set rsRead=server.CreateObject("ADODB.Recordset")
rsRead.Open "select * from [Sheet1$]",cn,1,1
do until rsRead.EOF
'寫入資料庫.
oConn.Execute("Insert into users(userid,password)Values('"& rsRead.Fields("userid")&"','" &rsRead.Fields("password") & "')" )
rsRead.MoveNext
loop
%>
資料庫data.mdb
表users
id,uesrid,password
Excel文件data.xls
數據
userid password
wang w521
zhange z254
hong h112
'================!!!!ASP讀取EXCEL注事項!!!!======================
'i)將Excel97或Excel2000生成的XLS文件(book)看成一個資料庫,其中的每一個工作表(sheet)看成資料庫表
'ii)ADO假設Excel中的第一行為欄位名.所以你定義的范圍中必須要包括第一行的內容
'iii)Excel中的行標題(即欄位名)不能夠包含數字. Excel的驅動在遇到這種問題時就會出錯的。例如你的行標題名為「F1」
'iiii)如果你的Excel電子表格中某一列同時包含了文本和數字的話,那麼Excel的ODBC驅動將不能夠正常, 處理這一行的數據類型,你必須要保證該列的數據類型一致
'====================================================
其他:
http://bbs.dvbbs.net/dispbbs.asp?boardid=1&id=1124887
② 如何通過asp將excel表導入access資料庫中
先導出,再導入
Excel導出函數
<%
Sub ExportToExcel
Response.ContentType = "application/vnd.ms-Excel"
Response.AddHeader "Content-Disposition", "attachment;Filename=Results.xls"
Response.Write "<body>"
Response.Write "<table border=1>"
Call WriteTableData
Response.Write "</table>"
Response.Write "</body>"
Response.Write "</html>"
End Sub
%>
Excel導入資料庫
<%
dim FileName
FileName="Excel.xls" '取得文件名,來自項目經理的指定,路徑固定在某個虛擬路徑中
Dim conn, rs
set conn=CreateObject("ADODB.connection")
conn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=" & Server.mappath(""&FileName&"") & ";" & _
"DefaultDir=G:\"
set rs=createobject("ADODB.recordset")
rs.Open "Select * from [Sheet1$]",conn, 2, 2
if rs.eof then
response.write "Excel表中無紀錄"
else
set connDB = Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("Excel.mdb")
'RESPONSE.WRITE DBpath
connDB.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
Set RsDB = Server.CreateObject("ADODB.Recordset")
sqlDB="Select * from Excel"
RsDB.open SQLDB,connDB,1,3
do while not rs.eof '利用循環讀出數據
RsDB.addnew
RsDB("filename")=rs(0)
RsDB("id1")=rs(1)
RsDB("id2")=rs(2)
RsDB("id3")=rs(3)
RsDB("id4")=rs(4)
Rs.update
RsDB.movenext
rs.movenext
loop
'response.redirect FileName
end if
RsDB.movefirst
if RsDB.eof then
response.write "資料庫中無記錄"
else
do while not RsDB.EOF
response.write RsDB("filename")&" "
response.write RsDB("id1")&" "
response.write RsDB("id2")&" "
response.write RsDB("id3")&" "
response.write RsDB("id4")&" "
response.write "<br>"
RsDB.movenext
loop
end if
rs.close
set rs=nothing
set conn=nothing
%>
③ ASP代碼中如何把EXCEL數據導入到SQL SERVER資料庫中
'定義打開Excel表格的函數
Function OpenExcel(path)
dim excel,rs,strsql
On Error Resume Next
Set rs = Server.CreateObject("ADODB.RecordSet")
Set excel = Server.CreateObject("ADODB.Connection")
excel.Open "Driver={Microsoft Excel Driver (*.xls)};DBQ=" & path
If Err.number<> 0 Then
Response.Write "請檢查上傳的Excel文件內部格式,文件無法打開,導入失敗!"
Response.End
End If
strsql = "SELECT * FROM [Sheet1$]" '在這里指定工作薄名稱,默認是Sheet1$
Set rs = excel.Execute(strsql)
Set OpenExcel = rs
End Function
'讀取文件中的內容
Dim rsInfo
Set rsInfo = Server.CreateObject("ADODB.RecordSet")
Set rsInfo = OpenExcel("E:/a.xls") '這里的文件路徑請用Server.Path來獲取
'檢查讀取結果
If rsInfo.State<> 1 Then
Response.Write "請檢查Excel文件中的工作表命名是否為Sheet1,導入失敗!"
Response.End
End If
If rsInfo.EOF And rsInfo.BOF Then
Response.Write "沒有找到Excel表中的數據,導入失敗!"
Response.End
End If
If IsNull(rsInfo.Fields(0)) or Trim(rsInfo.Fields(0))="" Then
Response.Write "沒有找到Excel表中的數據,導入失敗!"
Response.End
End If
'這里指定導入數據的列數,列數少了退出
If rsInfo.Fields.Count< 7 Then
Response.Write "Excel表中的數據列數不正確,導入失敗!"
Response.End
End If
'創建資料庫連接
dim dbrs,conn,sql
Set conn = Server.CreateObject("ADODB.Connection")
Set dbrs = Server.CreateObject("ADODB.Recordset")
'注: G_DB_ConnectString是連接資料庫的字元串,自己定義
conn.ConnectionString = G_DB_ConnectString
conn.Open '打開資料庫連接
'創建臨時表
sql = "IF EXISTS (SELECT * FROM sysobjects WHERE xtype='U' and name='tmp_PartRes') "
sql = sql & "BEGIN Drop table tmp_PartRes END "
sql = sql & "Create table tmp_PartRes([ID] int identity(1,1),"
sql = sql & "PartID varchar(100),Brand varchar(100),[Package] varchar(100),"
sql = sql & "BatchNo varchar(100),[Price] varchar(100),[Stock] varchar(100) default('0'),"
sql = sql & "Brief varchar(100),StockFlag int default(1),"
sql = sql & "SuperFlag int default(1),SaleFlag int default(1))"
conn.execute sql
'取表結構 注意: 只取表的結構, 不要數據, 因為我這個是剛創建的臨時表, 沒有數據,
'如果表中存在數據, 要注意加上條件句, 防止取到數據 如: where ID = -1
sql = "SELECT * FROM tmp_PartRes"
dbrs.CursorLocation = 3 '這一定要設置為3
dbrs.Open sql,conn, 3, 4 '這里的參數必須是3和4
'取到表結構後, 必須要把活動連接及資料庫連接關閉,這個很重要, 否則導入速度特慢.
Set dbrs.ActiveConnection = Nothing
conn.close
'提取Excel中的數據, 將excel中的數據放入到資料庫表中.
While Not rsInfo.EOF
If Trim(rsInfo.Fields(0))<> "" Then
dbrs.AddNew
dbrs("PartID") = Ucase(Trim(rsInfo.Fields(0)))
dbrs("Brand") = Trim(rsInfo.Fields(1))
dbrs("Package") = Trim(rsInfo.Fields(2))
dbrs("BatchNo") = Trim(rsInfo.Fields(3))
dbrs("Price") = Trim(rsInfo.Fields(4))
If Trim(rsInfo.Fields(5))<>"" Then
dbrs("Stock") = Trim(rsInfo.Fields(5))
Else
dbrs("Stock") = "0"
End If
dbrs("Brief") = Trim(rsInfo.Fields(6))
End If
rsInfo.MoveNext
Wend
'更新記錄集到資料庫臨時表
conn.Open '打開連接
dbrs.ActiveConnection = conn
dbrs.UpdateBatch '批量更新函數
'更新完成後, 關閉連接
dbrs.Close
Set dbrs = Nothing
rsInfo.Close
Set rsInfo = Nothing
④ asp中把excel中數據導入SQL資料庫
private void add()
{
OleDbConnection connread = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=D:\\Book1.xls; extended properties=excel 8.0");
connread.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", connread);
DataSet ds = new DataSet();
da.Fill(ds);
connread.Close();
SqlConnection conn = new SqlConnection("server=.; integrated security=true; database=demo");
conn.Open();
SqlCommand cmd = new SqlCommand("create table name(name varchar(100),namea varchar(100),nameb varchar(100))", conn);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string p=string.Format("insert into name values('{0}','{1}','{2}')",ds.Tables[0].Rows[i]["name"].ToString().Trim(),ds.Tables[0].Rows[i]["namea"].ToString().Trim(),ds.Tables[0].Rows[i]["nameb"].ToString().Trim());
cmd = new System.Data.SqlClient.SqlCommand(p, conn);
cmd.ExecuteNonQuery();
}
conn.Close();
}