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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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.....

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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.

735 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