tags:

views:

368

answers:

4

Is there any way to import data from xls files to database(postgreSQL in my case) using java?

+2  A: 

Assuming that by xls you mean Microsoft Excel files:

Look into Apache POI. They have readers and writers for many kinds of Microsoft formats. You could make a java program that uses the POI library to read the excel and then write the contents to the database. Here is a short tutorial how to read Excels with POI.

You could also try to export data from the Excel file to a CSV (Comma Separated Values) file. Then you could use COPY command in PostGreSQL to do the import. Here is a short tutorial how to do this.

Juha Syrjälä
+1  A: 

Take a look at the Apache POI library, which is the Java API for Microsoft documents. It may also help to take a look at some POI Excel tutorials, of which there are many.

Kaleb Brasee
+1  A: 

Others are suggesting Apache POI, personally I find the API in JExcelApi a lot easier to work with.

As to actually do what it is you are asking, your question is too vague to give a good answer to.

  • Are you trying to import the raw binary of the file to the database?
  • Do you want to parse the XLS file and extract some meaning from it and insert that data into the database?
  • Etc.
matt b
Thank You for suggestions. :) I've implemented that poi api already! Actually I have one xls file containing some data for eg. Customer info like their names,phone numbers,DOB,address etc. and I just want to insert them into database.
Supereme
+2  A: 

Another option (if on Windows) would be to create an ODBC link to the excel file, and then use JDBC-ODBC to query the spreadsheet using SQL.

This method is only useful if the spreadsheet file and structure are not going to change much.

Personally, I'd use Apache POI as mentioned by other posters, but this method has a smaller learning curve if you're looking for a quick solution.

Paul Rayner
Thank You! I've already implemented that poi api but I wanted something direct method rather than having an intermediate level of reading and then calling some function to insert the records(in xls file) into database.
Supereme