Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it L'avatar di fraude
    Registrato dal
    Feb 2004
    Messaggi
    2,870

    Importazione file excel in DB Access [ASP]

    Ciao a tutti.

    Ho messo a punto una procedura che tramite ASP, importa i dati di un file excel direttamente all'interno del db access ed esegue l'upload dello stesso file excel sul server.

    La procedura funziona correttamente, ma ha questi "difetti":

    - per importare 13 righe di un file excel ci impiega 1 minuto e 10 secondi (un pò troppo );

    - non funziona la barra che indica la percentuale progressiva dell'upload del file.

    Vorrei una mano ad ottimizzarlo, in maniera tale da successivamente renderlo disponibile per la community o ai forummisti interessati.

    Ecco il codice, grazie:
    codice:
    <%
    Function GetConnection()
      dim Conn: Set Conn = CreateObject("ADODB.Connection")
      Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
      Conn.open "Data Source=" & Server.MapPath("mdb-database/upload.mdb") 
      set GetConnection = Conn
    end function
    
    Function SplitFileName(FullPath)
      Dim Pos, PosF
      PosF = 0
      For Pos = Len(FullPath) To 1 Step -1
        Select Case Mid(FullPath, Pos, 1)
          Case ":", "/", "\": PosF = Pos + 1: Pos = 0
        End Select
      Next
      If PosF = 0 Then PosF = 1
     SplitFileName = PosF
    End Function
    
    Function GetPath(FullPath)
      GetPath = left(FullPath, SplitFileName(FullPath)-1)
    End Function
    
    Function GetUniqueFileName(FileName, DestPath)
      if isempty(gFS) then Set gFS = CreateObject("Scripting.FileSystemObject")
      Dim Counter, FullPath, NewFileName
      Counter = 1
      NewFileName = FileName
      
      if gFS.FileExists(DestPath & "\" & NewFileName) then
    		Do 
    			Counter = Counter + 1
    			NewFileName = Counter & "_" & FileName
    		Loop while gFS.FileExists(DestPath & "\" & NewFileName)
      end if
      GetUniqueFileName = NewFileName
    End Function
    
    
    Dim DestinationPath
    DestinationPath = Server.mapPath(VirtualFolder) & "\"
    
    Dim Form: Set Form = New ASPForm %><% 
    
    Server.ScriptTimeout = 2000
    Form.SizeLimit = &HA00000
    
    if len(Request.QueryString("UploadID"))>0 then
    	Form.UploadID = Request.QueryString("UploadID")'{/b}
    end if
    
    
    If Form.State = 0 Then
    
      Const VirtualFolder = "UploadFolder"
    
      Dim File, DestFileName
      For Each File In Form.Files.Items
    		If Len(File.FileName) > 0 Then
            DestFileName = GetUniqueFileName(File.FileName, DestinationPath)
    
            File.SaveAs DestinationPath & "\" & DestFileName
    
    
    	  End If
    
      Dim virtDestinationPath
      virtDestinationPath = "http://" + Request.ServerVariables("SERVER_NAME") _
       + GetPath(Request.ServerVariables("SCRIPT_NAME") ) _
       + "/" + VirtualFolder + "/"
      
    
      Dim FileField
      For Each FileField in Form.Files.Items
       
        if len(FileField.FileName)> 0 then 
              Dim DestinationFileName
    
          DestinationFileName = DestinationPath & FileField.FileName
    
          FileField.SaveAs DestinationFileName
                
          Dim Conn, RS, oRs, intCount, contRighe, bolErrorCAT, ErrorField
         
          DestinationFileName = "UploadFolder/" & Form("File1").FileName
          Set Conn = GetConnection
           
          Set RS = Server.CreateObject("ADODB.Recordset")
          Rs.Open "SELECT * from [DATI$]", "DRIVER=Microsoft Excel Driver (*.xls); DBQ=" & Server.MapPath(DestinationFileName)
          
          Set oRs = Server.CreateObject("ADODB.Recordset")
          oRS.Open "ListFiles", GetConnection, 2, 2
           
       intCount = 0  
       contRighe = 1
       
    Do Until Rs.Eof 
    
    If Rs("CAT") <> "" and Rs("ID_CAT") <> "" and ... Then
    
    bolErrorCAT = true
    if bolErrorCAT = true then
    oRS.AddNew
    
        
        oRs("CAT") = Rs("CAT")  
        oRs("ID_CAT") = Rs("ID_CAT")                       
        ...
        
    if IsNull(Rs("CAT")) or IsEmpty(Rs("CAT")) or Len(Rs("CAT"))="" then 
     oRs("CAT")= NULL
     else
     oRs("CAT")= Rs("CAT")
    end if
                           
       
        
        oRS("ContentType") = Form("File1").ContentType
        oRS("UploadDT") = Now()
        oRS("Description") = Form("Desc-" & FileField.Name)
        oRS("SourceFileName") = virtDestinationPath & DestFileName
        oRS("DestFileName") = DestinationFileName
        oRS("DataSize") = FileField.Length
        oRS("Description") = Form("Description")
        oRS("Title") = Form("Title")
        oRS("Email") = Form("Email")
              
        oRs.Update
        
      intCount = intCount + 1
      contRighe = contRighe + 1 
        
    else
    
    bolErrorCAT = false
    break
    
    end if 
    end if
    
    Rs.MoveNext
    
    Loop 
    
    if bolErrorCAT = false then  
    response.write "Errore!"
    end if 
    
    Rs.close    
      set Rs=nothing    
      oRs.Close
      Set oRs = Nothing   
      Conn.Close
      Set Conn = Nothing    
    
    end if
    
    Next 
       
    Next
      
    ElseIf Form.State > 10 then
      Const fsSizeLimit = &HD
      Select case Form.State
    		case fsSizeLimit: response.write  "
    <Font Color=red>Source form size (" & Form.TotalBytes & "B) exceeds form limit (" & Form.SizeLimit & "B)</Font>
    "
    		case else response.write "
    <Font Color=red>Some form error.</Font>
    "
      end Select
    End If'Form.State = 0 then
    
    Dim UploadID, PostURL
    UploadID = Form.NewUploadID
    
    PostURL = Request.ServerVariables("SCRIPT_NAME") & "?UploadID=" & UploadID
    %>
    
    <SCRIPT>
    function ProgressBar(){
      var ProgressURL
      ProgressURL = 'progress.asp?UploadID=<%=UploadID%>'
    
      var v = window.open(ProgressURL,'_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=350,height=200')
      
      return true;
    }
    </SCRIPT>

  2. #2
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    Ma i passaggi come sono?

    Maschera di upload.
    Selezione del file excel
    Upload
    Connessione e caricamento

    Giusto?

  3. #3
    Utente di HTML.it L'avatar di fraude
    Registrato dal
    Feb 2004
    Messaggi
    2,870
    Giustissimo.

  4. #4
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    Mi dici che dimensione ha il file?

  5. #5
    Utente di HTML.it L'avatar di fraude
    Registrato dal
    Feb 2004
    Messaggi
    2,870
    Il file excel in questione (13 righe compilate) ha una dimensione di 240 kB.

  6. #6
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    Prima cosa: la lentezza è dovuta all'upload
    Secondo : La progress bar non è possibile realizzarla in nessun liguaggio Web (tranne net ma non è reale è una simulazione)
    Terzo: Metti option explicit e dichiara tutte le variabili

  7. #7
    Utente di HTML.it L'avatar di fraude
    Registrato dal
    Feb 2004
    Messaggi
    2,870
    Grazie Baol, ma esiste un rimedio per la lentezza dell'upload?
    Lavoro in rete intranet locale, perchè per 13 righe ci mette tanto?

  8. #8
    Utente di HTML.it L'avatar di Baol74
    Registrato dal
    Jul 2002
    Messaggi
    2,004
    Devi Usare un upload decente. O fare una modifica al tuo
    Ripeto, la lentezza è dovuta all'upload

  9. #9
    perchè non utilizzare la mitica classe di baol?

    quando ho 2 minuti testo cmq quel codice
    grazie per avermi interpellato
    san imente saluta

    * http://simonecingano.it *

  10. #10
    Utente di HTML.it L'avatar di fraude
    Registrato dal
    Feb 2004
    Messaggi
    2,870
    Vi ringrazio per i costruttivi interventi; per completezza d'informazione Vi posto anche la classe che utilizzo per questo upload: scarica Vi prego di testare il suo funzionamento....

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.