I am very new to java. I am trying to fetch some data from a database and the result set is displayed in excel. I am able to inetract with database. But how should I go ahead for inserting data into excel sheet.Its simple Java program and in future I would like to generate files in other format say PDF, doc etc.
I am looking for an approach with lesser load on CPU, faster.
thanks in advance for help.
Just spit out a CSV file. It's lightweight and portable. You can grab a csv writer from Apache commons I think but spending the 10 min it would take to write one might be a good learning exercise as well.
If you want a real solution where you want different outputs (eg excel, pdf, rich text etc) then use a reporting tool. There are plenty of opensource tools like ireport which will let you create a template then write a simple java app that renders that to pdf, excel etc. Otherwise you will end up doing it by hand. It's a bit heavyweight but anything more than trival tabular output will be easier.
Apache POI is for you in this case, but you will find it a little bit overwhelmed if you just need to write/read data from an excel file.
Try jExcel instead, the API is simple and straightforward, you can also manipulate sheets within an excel workbook.
The easiest and standard way of doing this is to use POI library:
http://poi.apache.org/spreadsheet/quick-guide.html
The new xlsx format is based on Open-XML and would provide a method of generating these files without any dependency on Microsoft-office COM libraries -- the same could done for docx and pptx formats later, as well as other open-Xml formats like EPUB.
The Apache POI project looks like it might provide one possible solution. There's also an article on the MSDN interop blog that discusses this in some detail.
The key words you should google for are OLE and DDE.
Though, Java is not the best language for interface Microsoft's software.
for generating excel i think you should try simreport in jsimreport.com. in my opinion, it's quite simple to make an excel report, it uses the excel sheet to generate report so easy to config and visually
Related
I want to be able to generate .docx files through either Java or Python based off of a template .docx file. I need to be able to insert in simple text, some bullets and a table or two.
I would like suggestions on specific libraries/modules for either Python or Java that would allow me to load a template, insert basic text and tables and then save it.
I have been looking into JACOB for Java and docx for Python. Any alternatives? Or will one of these be able to do what I need?
Thanks in advance
If you want to generate a docx, than you might like docxtemplater, which is a library I maintain which does docx generation from a template (much like Mustache for HTML).
It runs on node but has a command line interface so you can use it from any language.
DocxTemplater Library
Demo Site
Give docx4j as a choice, it's based on Apache POI but with better documentation
Did you look at Aapache POI (the Java API for Microsoft Documents) project?
http://poi.apache.org/
Good luck!
I has a excel file with 4 excel sheets in it. Now i want to read or write to required excel sheets using java without using any third party lib.
I know i can read and write data using FileInputStream and FileOutputStream respecitvely. But i can handle the work sheets??
No, you can'not, There is numerous way in Java for reading/writing files, but there is no built-in support for MS Office/Excel spreadsheets. http://poi.apache.org/ - is a key to victory.
If your goal is to interface with data from an excel sheet from your Java application, I'd suggest to use the solutions suggested by other posters, it will save you a lot of work.
If, however, you want to be able to read excel files from Java (or any other programming language for that matter) 'just because you can' then you could take a look at this file and read the instructions on this web-page. I would warn you that it would take considerably more time to implement your own API if you base it only on the file-specs that are publicly available. You might want to check out the work done by the people from the Apache POI project to get an idea of how to approach it. Or (even better) contribute to the project. Here you can find out how to go about doing that
I have a directory where files are constantly updated. I need to read the latest excel file and convert it into tab delimited file. It is under windows. A batch + java solution will work for me. Or if I can use excel in command line programatically that also works
I would disagree with those people who recommend Apache POI. The best API that I know of for dealing with Excel is Andy Khan's JExcel.
It has already been widely suggested, POI is probably the most complete "pure Java" implementation of Excel.
In one API you get support for Excel 2003 and 2007.
However, you need to be weary of its memory footprint. It is a hog. If you use it, make sure you use the event-driven model it supports as this will reduce footprint and execute faster.
In Java you can use, for example, Apache POI library to read data from Excel files. And then use standard Java facilities to write data into tab delimited file.
You can read in the excel sheet using POI and then iterate through the cells, writing them out to a separate file with appropriate delimiters.
Have a look at other SQ question: convert Excel to csv either using shell script or jython . if so how
I answered with PyODConverter, but there is jodconverter: Java version of this tool. It uses OpenOffice working as a service. I use it to convert between various file formats.
You can use Apache POI API for reading the excel file and OpenCSV for writing the CSV file.
I've seen various means of reading (and writing) Excel spreadsheets. For example:
In .Net as an ADO.Net source (as per Reading Excel files from C#);
In Java with either JExcelAPI ("jxl") or Apache POI;
In PHP with the Excel Spreadsheet Writer (as per Format text in Excel file via PHP and PHP:Writing to excel file using PEAR).
But I'm interested in more than just reading/writing Excel files. I want something where I can evaluate arbitrary Excel formulas. So far in this vein I've seen:
C# Excel Interop, although this seems to require that you have Excel running; and
POI-HSSF: this formula support seems incomplete.
Are there better options or is that it?
SpreadsheetGear for .NET is compatible enough with Excel that Microsoft uses it to calculate Excel based calculators on their MSN Money web site.
If you're only targeting the windows platform, you really have no substitute for running Excel, and controlling it via a COM bridge.
As you are finding, any other solution is likely to be
incomplete, and/or
incompatible and/or
inconsistent
with Excel.
Unfortunately, this does add a dependency on Excel to your software.
If you want to evaluate Excel formulae, frankly Excel is the only reliable way to do this. So running Excel via a COM bridge is the way to go.
I've done this before in Java using JACOB and that works very nicely. Excel looks after all the functionality. You just have to populate your spreadsheet (in memory) and let Excel do the rest.
Anyone know if it is possible?
And got any sample code for this?
Or any other java API that can do this?
The Office 2007 format is based on XML and so can probably be written to using XML tools. However there is this library which claims to be able to write DocX format word documents.
The only other alternative is to use a Java-COM Bridge and use COM to manipulate word. This is probably not a good idea though - I would suggest finding a simpler way.
For example, Word can easily read RTF documents and you can generate .rtf documents from within Java. You don't have to use the Microsoft Word format!
As others have said POI isn't going to allow you to do anything really fancy - plus it doesn't support Office 2007+ formats. Treating MS Word as a component that provides this type of functionality via COM is most likely the best approach here (unless you are running on a non-Windows OS or just can't guarantee that Word will be installed on the machine).
If you do go the COM route, I recommend that you look into the JACOB project. You do need to be somewhat familiar with COM (which has a very steep learning curve), but the library works quite well and is easier than trying to do it in native code with a JNI wrapper.
If you are using docx, you could try docx4j.
See the AddImage sample
Surely:
Take a look at this: http://code.google.com/p/java2word
Word 2004+ is XML based. The above framework gets the image, convert to Base64 representation and adds it to the XML.
When you open your Word Document, there will be your image.
Simple like this:
IDocument myDoc = new Document2004();
myDoc.getBody().addEle("path/myImage.png"));
Java2Word is one API to generate Word Docs using obviously Java code. J2W takes care of all implementation and XML generation behind the scenes.
As far as can be gathered from the project website: no.
POI's HWPF can extract an MS Word document's text and perform simple modifications (basically deleting and inserting text).
AFAIK it can't do much more than that.
Also keep in mind that HWPF works only with the older MS Word (97) format, not the latest ones.
Not sure if Java out of the box can do it directly. But i've read about a component that can pretty much do anything in terms of automating word document generation without having Word. Aspose Words
JasperReports uses this API alternatively to POI, because it supports images:
JExcelAPI
I didn't try it yet and don't know how good/bad it is.