asp and ms-access db - how to import data from xls file

孤街醉人 提交于 2019-12-12 01:11:53

问题


i want to allow the user to upload xls file with 9 columns and unlimited number of rows. i will run over everyline and insert the data to the db

how do i read the xls file?


回答1:


You can read the XLS by opening an ADO recordset which pulls in the spreadsheet's data.

This example reads data from a spreadsheet named Billing Summary which includes column names in the first row..

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 

If that particular connection string doesn't work for you, look at other examples of Excel connection strings at Connection strings for Excel

Edit: That example works in Access. But you said ASP. I think it will work there, too, if you drop the data types from the variable and constant declarations: Dim strSql instead of Dim strSql As String




回答2:


Example of using an SQL statement to update Access from Excel.

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



回答3:


In C#, I had to load an excel spreadsheet to a DataSet - this got me there...

Code Project Example

I used Option 1 - the Preferred method! Hope this helps...

Mike



来源:https://stackoverflow.com/questions/4157575/asp-and-ms-access-db-how-to-import-data-from-xls-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!