Read data from excel spreadsheet into an Oracle Table

Posted on 2002-04-11
Last Modified: 2012-05-04
How could I write a PLSQL procedure or a simple sql statement so that I could read data in from an Excel spread sheet into an oracle table??? Any examples appreciated!!!

Question by:luinzi

Accepted Solution

Bhatti earned 90 total points
ID: 6933908
Loading Excel files into Oracle

There are two ways to load Excel into Oracle:

   1.Through a Comma-Separated Values file. This method gives more control over the Oracle table created.
   2.Through a dBASE file. This method is a bit quicker, IF your data is very orderly.

Excel -> CSV -> Oracle

   1.Save the Excel spreadsheet as file type 'CSV' (Comma-Separated Values).
   2.Transfer the .csv file to the Oracle server (usually HP Unix).
   3.Create the Oracle table, using the SQL CREATE TABLE statement to define the table's column lengths and types. Here's an example of an sqlplus 'CREATE
     TABLE' statement:

      (SPECIES VARCHAR2(10),
       RATING VARCHARC2(1));

   4.Use sqlload to load the .csv file into the Oracle table. Create a sqlload control file like this:

     load data
     infile spec_rat.csv
     into table species_ratings
     fields terminated by ','

   5.Invoke sqlload to read the .csv file into the new table, creating one row in the table for each line in the .csv file. This is done as a Unix command:

      % sqlload userid=username/password control=<filename.ctl> log=<filename>.log

     This will create a log file <filename>.log. Check it for loading errors.
   6.Use these sqlplus commands to check the Oracle table:


   7.You're done.

Excel -> dBASE -> Oracle

dbf2ora can also be used to load an Excel spreadsheet into Oracle, by getting Excel to export it as a .dbf (dBASE) file first. This procedure is tricky, as Excel does not
carry explicit data type (eg char/numeric) or format (eg column width) information on its columns. If you don't do it just right, the dBASE file will not have the appropriate
column definitions, and neither will the Oracle table.

Therefore, to create a dBASE file, Excel must use some rules of thumb to make up the column definitions in dBASE :

     The first row of the spreadsheet must contain names for the column (field) names. Avoid unusual punctuation.
     The second row must start the data. DO NOT leave a blank row.
     The cells in the second row should have the character type you want. (eg, don't put alpha strings in a numeric field). If a cell in the second row contains an alpha
     character, Excel will create that column in dBASE as alphanumeric.
     Do not use the string "NULL" in numeric fields. "NULL" is an alpha string, so if it was in the second row of the table, it would force the column to alphanumeric. If it
     was in another row, it would generate an error.
     If a cell in a numeric field is empty in the second row (that is, the first data row), enter an "impossible" value , say -9999, to force the column to numeric. I would
     assume you would remove this -9999 value later on in Oracle. If you leave the cell blank, it will come across as character data type.

(I am not 100% sure you need to do the following column manipulations -- you could skip at first and then start over if you don't get the right field widths and number of
decimal places)

     Save each column to the desired width. This will define the field width later in Oracle. Use the "Format - column width" button.
     Set the numeric columns to the desired number of decimal places with the "Format -- number -- 0.00" button.
     Set the column allignment to general with the "Format -alignment" button. Choose general (avoid centered, left or right).

After these manipulations, save the file as DBF3 (Dbase 3). The file is now ready to use in 'dbf2ora' in UNIX.

I collect this information, may this will help you.

Best regards


Author Comment

ID: 6933949
ok......... The table already exists and has values in it! I'm running SQL plus and this is what I have got so far:

fhandle utl_file.file_type;
fbuffer varchar2(1000);
fhandle := utl_file.fopen (' C:\', 'test.xls', 'w');
while true loop
utl_file.get_line (fhandle, fbuffer);
insert into cntct_prod_desc (PRODUCT_CODE, PRODUCT_DESC)
select (substr(fbuffer,1,instr(fbuffer,','))),
from dual;
end loop;


but it doesn't seem to put anything into the table!!!!

why is that??


Expert Comment

ID: 6933971
The problem maybe in this line:

fhandle := utl_file.fopen (' C:\', 'test.xls', 'w');

You have specified to open the file in write mode with the 'w'.  Try it with 'r', which opens it for read mode.


Author Comment

ID: 6934019
Made the chgange to 'r' still nothing happens and I added some exception handling....... still nothing.....

Author Comment

ID: 6934043
Made the chgange to 'r' still nothing happens and I added some exception handling....... still nothing.....
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Author Comment

ID: 6934048
trying the SQL load but trying the following:

% sqlload userid=usr/pswrd control=<test.ctl>log=<e.log>

returns this ksh error:

ksh: syntax error: `newline or ;' unexpected

why is that???

Author Comment

ID: 6934069
trying the SQL load but trying the following:

% sqlload userid=usr/pswrd control=<test.ctl>log=<e.log>

returns this ksh error:

ksh: syntax error: `newline or ;' unexpected

why is that???

Author Comment

ID: 6934116
It finally worked!!!!

It was a privledges issue!!! got it sorted now!

Expert Comment

ID: 7309247
Hi Luinzi

Could you please post your entire code. I have a similar situation where I have to read multiple records of different formats from an excel file into an Oracle table and do some processing in PL/SQL based on those records.



Expert Comment

ID: 14059253
Any one pls Give me sample codes

I want insert datas from Excel spread sheet without converting CSV.I need pl/sql procedure.

Thanks for advance

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now