我想允许用户上传包含9列和无限行数的xls文件。 我将遍历每一行并将数据插入数据库
我如何阅读xls文件?
答案 0 :(得分:1)
您可以通过打开ADO记录集来读取XLS,该记录集会提取电子表格的数据。
此示例从名为“Billing Summary”的电子表格中读取数据,该电子表格包含第一行中的列名称。
Public Sub ReadSpreadsheet()
Const cstrFolder As String = "C:\Access\webforums"
Const cstrFile As String = "ExampleFinance.xls"
Dim strConnect As String
Dim strSql As String
Dim cn As Object
Dim rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
cstrFolder & Chr(92) & cstrFile & _
";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
cn.Open strConnect
strSql = "SELECT * FROM [Billing Summary$] WHERE SomeField Is Not Null;"
rs.Open strSql, cn
Do While Not rs.EOF
'* do something with each row of data *'
'Debug.Print rs!SomeField '
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
如果该特定连接字符串不适合您,请查看Connection strings for Excel
处的Excel连接字符串的其他示例 修改:该示例适用于Access。但你说ASP。如果你从变量和常量声明中删除数据类型,我认为它也可以在那里工作:Dim strSql
而不是Dim strSql As String
答案 1 :(得分:1)
使用SQL语句从Excel更新Access的示例。
Set cn = CreateObject("ADODB.Connection")
scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\docs\dbto.mdb"
cn.Open scn
sSQL = "SELECT * INTO NewTable FROM "
sSQL = sSQL & "[Excel 8.0;HDR=YES;IMEX=2;DATABASE=C:\Docs\From.xls].[Sheet1$]"
cn.Execute sSQL, recs
MsgBox recs
答案 2 :(得分:0)