2010-04-07 66 views
2

我使用下面的代码将excel文件上传到服务器。现在我需要将它保存到数据库。任何人都可以指导显式的做到这一点?将excel内容保存到数据库,通过asp

Dim SQLStr 
SQLStr = "INSERT ALL INTO TABLENAME " 
for each file in filCollection 
    file_name = file.name 
    path = folderObj & "\" & file_name 
    Set objExcel_chk = CreateObject("Excel.Application") 
    Set ws1 = objExcel_chk.Workbooks.Open(path).Sheets(1) 
    row_cnt = 1 


     'for row_cnt = 6 to 7 
     ' if ws1.Cells(row_cnt,col_cnt).Value <> "" then 
     ' col = col_cnt 
     ' end if 
     'next  
     While (ws1.Cells(row_cnt, 1).Value <> "") 
     for col_cnt = 1 to 10  

     SQLStr = SQLStr & "VALUES('" & ws1.Cells(row_cnt, 1).Value & "')" 
     next 
     row_cnt = row_cnt + 1 
     WEnd 


    'objExcel_chk.Quit 
    objExcel_chk.Workbooks.Close() 
    set ws1 = nothing 
    objExcel_chk.Quit 

    Response.Write(SQLStr) 

     'set filobj = FSYSObj.GetFile (sub_fol_path & "\" & file_name) 
     'filobj.Delete 

    next 
End if 
End If 
+0

是否要将文件另存为二进制文件,或将值保存在Excel工作表中? – 2010-04-07 15:07:21

+0

我想将excel文件中的值保存到数据库中 – sushant 2010-04-08 06:01:30

+0

这段代码很容易被sql注入 – 2011-12-01 02:31:52

回答

1

Read From Excel

上面的链接说你必须在服务器上安装Excel,您必须对Excel文件设置从中读取正确的权限。在这个过程之外,我不确定你会怎么做。

+0

这看起来就像去找我的路。将Excel读入记录集,然后使用记录集进行插入。 – mikeY 2011-07-20 21:15:50

相关问题