Reading Excel file (.xls) in Java - java

I want to read text from excel file in J2SE using NetBeans. I found an article here about using Apache POI and JExcelApi, but I'd prefer not depending on external packages. if possible, using only Java built-in packages.
This almost worked, but it gives me an exception error:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Do I need to install some ODBC driver (if so, which driver and how to install)?
What's the simplest way to solve this?
please help, thanks in advance

To summarize what was said already, accessing excel data from Java can be done via a two-step-process:
1) Set up the excel file as an ODBC data source.
2) Use the JDBC-ODBC bridge to access the excel data, using the standard JDBC api.
It sounds like you may have two problems:
1) The ODBC data source may not be set up, hence the "data source not found" error.
2) The jar for the JDBC-ODBC bridge driver may not be in your classpath, hence the "no driver specified". Google around for it. It's not hard to find.

You need to setup an ODBC driver that points to your Excel spreadsheet as a datasource. You can either create one using the Windows ODBC manager and reference it, or create it on the spot using the code here, at your same tutorial site.

I made a small application which read in an Excel sheet in to a JTable model very quickly. I think you should reconsider your usage of external libraries.

If you want to just read in the data, converting the XLS file to a CSV file format would be the best way.

Related

How to copy a file from local desktop to a server file system

was Just Wondering Is there any way that we can Copy our Local Content Like XlS sheet to the file system on a remote server (e.g. a SQL Server) using Java?
I dont want Exact Code, just want some Headstart any help is appreciated:)
I agree with #beny23, reading a Microsoft format to upload info to Microsoft SQL Server does not sound like a problem where Java would be the first option.
In any case, you just have to consider the parts of your problem. Java can read Excel files (Look for Apache POI). From then, connecting Java to SQL Server is also a solved problem. (Although there several drivers and you might have to try more than one).

How to import an Excel file into MySQL using ODBC in Java?

I want to import an Excel file into a MySQL database using an ODBC driver. Does Java support this technique? If yes, please guide me on how to implement this.
You can read an Excel file in Java using the Apache POI library: http://poi.apache.org/
There are some code samples on their howto page which will tell you how to read an Excel file: http://poi.apache.org/spreadsheet/how-to.html
To store the data it would be better for you to use the JDBC driver for MySQL rather than ODBC since ODBC relies on native libraries. You can get the connector here: http://www.mysql.com/products/connector/
If you absolutely must use ODBC, then you can see how to do that here: http://download.oracle.com/javase/1.5.0/docs/guide/jdbc/bridge.html

How to migrate data from FoxPro to MySQL

I am having a database in .dbf (FoxPro) format.
How to retrieve data from FoxPro using Java?
If the data can be migrated to MySQL, How to do the conversion?
Taking the data to intermediate formats seems flawed as there are limitation with memo fields and CSV or Excel files.
If you are interested in a more direct approach you could consider something like "VFP2MySQL Data Upload program" or "Stru2MySQL_2", both written by Visual FoxPro developers. Search for them on this download page:
http://leafe.com/dls/vfp
DB-Convert (http://dbconvert.com/convert-foxpro-to-mysql-sync.php) is a commercial product that you might find helpful.
Rick Schummer, VFP MVP
You can use XBaseJ to access (and even modify write) data from FoxPro databases directly from Java with simple API.
This would allow you to have the two applications (the old FoxPro and the new Java one) side by side by constantly synchronizing the data until the new application is ready to replace the old one (e.g. many times the customers still hang on and trust more their old application for a while).
Do you have a copy of FoxPro? You can save the database as an HTML file, if you want. Then, from HTML, you can save to any format you want. I recently did this to save a FoxPro table as an Excel spreadsheet (not that I'd suggest using that for your Java code).
If you plan on using Java, once you have access to the data, why not use one of Java's native storage types?
I worked on the same project once long back where the project had be done with FoxPro and then we migrated that project to Java with MySQL.
We had the data in Excel sheets or .txt files, so we created tables as exact replica of the FoxPro data and transferred the data from the Excel/CSV /txt to MySQL using the Import data feature.
Once we did this, I think further you can take care from MySQL Data.
But remember work will take some time, and we need to be patient.
I suppose doing a CSV export of your FoxPro data and then writing a little Java programme that takes the CSV as input is your best bet. Writing a programme that both connects to FoxPro and MySQL in Java is needlessly complicated, you are doing a one time migration.
By the way PHP could do an excellent job at inserting the data into MySQL too. The main thing is that you get your data in the MySQL schema, so you can use it with your new application (which I assume is in Java.)
Two steps: DBF => CSV and the CSV => MySQL.
To convert DBF(Foxpro tables) to CSV the below link helps a lot
http://1stopit.blogspot.com/2009/06/dbf-to-mysql-conversion-on-windows.html
CSV => MySQL
MySQL itself supports CSV import option (or) to read csv file this link helps
http://www.csvreader.com/java_csv.php
I read the CSV file using Java CsvReader and inserted the records through program. For that i used PreparedSatement with Batch the below link gives samples for that
http://www.codeweblog.com/switch-to-jdbc-oracle-many-data-insert/

Determine installed Microsoft Excel version using Apache POI?

I need to determine which version of Microsoft Excel is installed on a Windows PC. I'm working with Apache POI but can't figure out an easy way to do this.
Anyone know if Apache POI has a built in functionality to do this? Any ideas how to do this using Java code?
POI provides a mechanism for writing Excel documents without using any of the Excel APIs. I doubt it has a mechanism to detect the installed version, as it has no dependency on an installed version of Excel.
Following a pattern similar to the one described in this question about detecting the Excel version in .NET, you could use JACOB to detect the installed Excel version like so:
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try {
xl.getProperty("Version");
} finally {
xl.invoke("Quit", new Variant[] {});
}
I really, really don't think POI has this functionality, as it's explicitly written to work cross-platform. It only deals with document files.
If I were faced with this task, I'd dig into the registry (via the Windows API) for the association of the .XLS file type. That will, err should give you a link to the most recently and completely installed Excel. Not reliably, because
there might not be an Excel installed;
the user might have changed the association; or
the Excel pointed at by the association might be broken.
But given the hint of (usually) the applicable executable, you could then examine either the .EXE file header of Excel or perhaps something in the surrounding files to determine Excel's version.
Ok it just occured to me that older Microsoft Excel versions have file extensions .xls and new ones have .xlsx.
I can just get reference to the file and get the file extensions... that easy.
If anyone has a better solution please let me know.

How can I read MS Office files in a server without installing MS Office and without using the Interop Library?

The interop library is slow and needs MS Office installed.
Many times you don't want to install MS Office on servers.
I'd like to use Apache POI, but I'm on .NET.
I need only to extract the text portion of the files, not creating nor "storing information" in Office files.
I need to tell you that I've got a very large document library, and I can't convert it to newer XML files.
I don't want to write a parser for the binaries files.
A library like Apache POI does this for us. Unfortunately, it is only for the Java platform. Maybe I should consider writing this application in Java.
I am still not finding an open source alternative to POI in .NET, I think I'll write my own application in Java.
For all MS Office versions:
You could use the third-party components like TX Text Controls for Word and TMS Flexcel Studio for Excel
For the new Office (2007):
You could do some basic stuff using .net functionality from system.io.packaging. See how at http://msdn.microsoft.com/en-us/library/bb332058.aspx
For the old Office (before 2007):
The old Office formats are now documented: http://www.microsoft.com/interop/docs/officebinaryformats.mspx. If you want to do something really easy you might consider trying it. But be aware that these formats are VERY complex.
Check out the Aspose components. They are designed to mimic the Interop functionality without requiring a full Office install on a server.
As the new docx formats are inherently XML based files, you can create and manipulate them programmatically with standard XML DOM techniques, once you know the structure.
The files are basically zip archives with an alternate file extension. Use the System.IO.Packaging namespace to get access to the internal elements of the file, then open them into a XmlDocument to perform the manipulation.
There are examples available for doing this, and the Office Open XML project on SourceForge may be worth looking at for inspiration.
As for the older binary formats, these were proprietary to MS, and the only way you're likely to get at the content from within is through the Office object model (requires an Office install), or a third party file converter/parser.
Unfortunately there's nothing first party and native to the .NET platform to work with these files.
What do you need to do with those file? If you just want to stream them to the user, then the basic file streams are fine. If you want to create new files (perhaps based on a template) to send to the user that the user can open in Office, there are a variety or work-arounds.
If you're actually keeping data in Office documents for use by your web site, you're doing it wrong. Office documents, even Excel spreadsheets and access databases, are not really an appropriate choice for use with an interactive web site.
If the document is in word 2007 format, you can use the system.io.packaging library to interact with it programatically.
RWendi
In Java world, there is also JExcelApi. It is very clearly written, from what I was able to see, much cleaner then POI. So maybe even a port of that code to .NET is not out of the question, depending of course you have enough of time on your hands.
OpenOffice.
You can program against it and have it do a lot for you, without spending the money on a license for the server, or have the vulnerability associated with it on your server.
Microsoft Excel workbooks can be read using an ODBC driver (or is it an OLE DB driver? can't remember) that makes the workbook look like a database table. But I don't know whether that driver is available without the Office Suite itself.
You can use OpenOffice. It has a command-line conversion tool:
Conversion Howto
In short, you define a macro in OpenOffice and you call that macro with a command-line
argument to OpenOffice. In that argument the name of the local file (the Office file) is
encoded.
It's not a great sollution, but it should be workable.

Categories

Resources