Results 1 to 11 of 11

Thread: To convert an Excel file to CSV and then move that data from CSV to oracle database.

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    Exclamation To convert an Excel file to CSV and then move that data from CSV to oracle database.

    Hello All,

    I have an excel sheet which I want it to be converted to CSV and move that data from CSv to oracle databse. I have a Vbscript that converts excel to CSV but the way in which my excel sheet is formatted it is not coming up properly. There are some cells which I dont require and want them to be skipped and only the headings and their respective data should be coming up onto the CSV file. I am attaching the excel sheet as .txt extension please change it to .xls as the editor is not allowing me to upload a xls file and also the screenshot of that excel sheet showing the cells and the text needed to be skipped (highlighted in a red box). Please help me with this to have the CSV file by skipping these data and then load that data to oracle database in two tables where in one table will have data from some rows in CSV and another table will have a row of data from that CSV file.

    Here is the VBscript that I have been using.

    Code:
    WorkingDir = "C:\Users\vijaywp\Desktop"
    Extension = ".XLS"
    
    Dim fso, myFolder, fileColl, aFile, FileName, SaveName
    Dim objExcel,objWorkbook
    
    Set fso = CreateObject("Scripting.FilesystemObject")
    Set myFolder = fso.GetFolder(WorkingDir)
    Set fileColl = myFolder.Files
    
    Set objExcel = CreateObject("Excel.Application")
    
    objExcel.Visible = False
    objExcel.DisplayAlerts= False
    
    For Each aFile In fileColl
    ext = Right(aFile.Name,4)
    If UCase(ext) = UCase(extension) Then
    'open excel
    FileName = Left(aFile,InStrRev(aFile,"."))
    Set objWorkbook = objExcel.Workbooks.Open(aFile)
    SaveName = FileName & "csv"
    objWorkbook.SaveAs SaveName, 23
    objWorkbook.Close 
    End If  
    Next
    
    Set objWorkbook = Nothing
    Set objExcel = Nothing
    Set fso = Nothing
    Set myFolder = Nothing
    Set fileColl = Nothing
    I am attaching the generated CSV file (please change the extension from txt to csv) with this script.

    Please help me with this.

    Thanks in advance.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by jobs123; Mar 13th, 2013 at 03:05 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    as the editor is not allowing me to upload a xls file
    zip first

    as you only want to save specific ranges, not the entire sheet you need to write the data to a text (.csv) file as required, using file io
    rather than using excel saveAs method
    there are several methods to do this, the simplest being like

    Code:
    f = freefile
    open "fullpath\somfile.csv" for output as f
    ' print required data in csv format here
    close f
    or you can use FSO if you prefer
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    Hello westconn,

    Thanks for replying. Thing is I am not having sufficient knowledge on file system in VBscript. How should I include this code you mentioned above in my code which I have posted.

    I guess you might have checked the CSV file that I have atatched so what exactly needs to be given after the open statement in your code snippet.

    Please let me know.

    Thanks a lot in advance.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    based on your files, for numbers of rows and columns, try like
    Code:
    for rw = 1 to 55
      for col = 1 to 14
         write #f, cells(rw, col) & ",";
      next
      write #f, cells(rw, 15)
    next
    sorry, my mistake, i thought you were doing this within excel
    in VBS, you will need to use FSO, as file io is not available
    the above loop would be similar, but using FSO
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    Hi westconn,

    How can we have FSO object created within that loop. I am not having much idea on it. Just wanted to know where in my code I have to include this code snippet and use FSO.

    Please help.

    Thank you so much for your support.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    Just wanted to know where in my code I have to include this code snippet and use FSO.
    in place of
    objWorkbook.SaveAs SaveName, 23
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    Westconn,

    I am getting an error when I place your code in place of objWorkbook.SaveAs SaveName, 23. I guess it is because of not using FSO. How to do it using FSO? Here are the screenshots for the modified code and the error.

    Name:  code.jpg
Views: 775
Size:  66.0 KB

    Name:  error.jpg
Views: 615
Size:  7.0 KB

    Please check and let me know what to do about it.

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    I am still getting the same error can you please help.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    i do not generally use fso, but try like

    Code:
    Set f1 = fso.CreateTextFile(savename, True)
    For rw = 1 To 55
      For col = 1 To 14
         f1.Write objWorkbook.Sheets(1).Cells(rw, col) & ","
      Next
      f1.Write objWorkbook.Sheets(1).Cells(rw, 15) & vbNewLine
    Next
    f1.Close
    if the range in the csv is not correct, just change rw and col to suit
    col first col to last col -1, then the last col in the second write line
    if you need to quotes around your csv values, you will need to change to
    Code:
    f1.write """" & objWorkbook.Sheets(1).Cells(rw, col) & ""","
    you will need to do this it your cells could contain commas
    similar for the second write line
    Last edited by westconn1; Mar 15th, 2013 at 06:45 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2012
    Posts
    41

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    Hi,

    I have formatted the excel sheet in a more proper way. Please tell me how can I connect this to oracle database table and the load that data into it.

    Please help.
    Attached Files Attached Files

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: To convert an Excel file to CSV and then move that data from CSV to oracle databa

    Please tell me how can I connect this to oracle database table
    add a reference to ADO (microsoft axtivex data object)
    go to connectionstrings.com to find the correct connection for oracle
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width