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.
Last edited by jobs123; Mar 13th, 2013 at 03:05 AM.
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
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.
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
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.
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
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.
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
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
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