How To Handle Excel Files in Java
How To Handle Excel Files in Java
There are many solutions to read or write Excel spreadsheets from Java. This HowTo is only about OpenSource (and
free) solutions.
JExcel
Java Excel API is a java API enabling developers to read, write, and modify Excel spreadsheets dynamically. Any
operating system which can run a Java virtual machine can both process and deliver Excel spreadsheets. One nice thing
about JExcelApi is that it has no dependencies on any third party libraries.
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
See http://jexcelapi.sourceforge.net/
POI
The POI project consists of APIs for manipulating various file formats based upon Microsoft's OLE 2 Compound
Document format using pure Java. POI is your Java Excel solution as well as your Java Word solution.
HSSF is the POI Project's pure Java implementation of the Excel '97(-2002) file format and it provides a way to read
spreadsheets create, modify, read and write XLS spreadsheets. Latest POI version seems to support the .XLSX format.
Since it's Jakarta project, POI has a dependencies with other JARs (commons,log4j,etc...).
The name was originally an acronym for "Poor Obfuscation Implementation" (ref: Wikipedia).
See http://jakarta.apache.org/poi/
JXLS
jXLS is a project that allows creation of extremely complex Excel reports just in several lines of code. It is based on
Jakarta POI.
With jXLS, all you need is to create XLS template file with all required formatting, formulas etc using specific notation to
indicate placement of data and then write a couple lines of code to invoke jXLS engine passing XLS template and the
exported data as parameters.
Example :
The XLS Template
Employees
Name Age Payment Bonus
${employee.name} ${employee.age} ${employee.payment} ${employee.bonus}
$[SUM(@employee.payment@)]
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
See http://jxls.sourceforge.net/
xlSQL
xlSQL is a JDBC Driver for Excel and CSV data sources. Documents can be read and written with SQL as if they were
tables in a database.
You can export XLS to XML or SQL INSERT statements. xlSQL includes its own "zero-admin" mySQL database. The
documentation is minimal at this time.
See http://xlsql.sourceforge.net/
JCOM
JCOM is a Java to COM bridge library. With JCOM you can call a COM object from Java as if it were a Java object
without having to deal with the internals of JNI. The documentation is minimal (in Japanese!).
Example :
import jp.ne.so_net.ga2.no_ji.jcom.excel8.*;
import jp.ne.so_net.ga2.no_ji.jcom.*;
import java.io.File;
import java.util.Date;
class TestExcel {
public static void main(String[] args) throws Exception {
ReleaseManager rm = new ReleaseManager();
try {
System.out.println("EXCEL startup...");
// if already started, open new window
ExcelApplication excel = new ExcelApplication(rm);
excel.Visible(true);
// display any information
System.out.println("Version="+excel.Version());
System.out.println("UserName="+excel.UserName());
System.out.println("Caption="+excel.Caption());
System.out.println("Value="+excel.Value());
xlRange.Item(1,1).Value("filename" );
xlRange.Item(2,1).Value("size" );
xlRange.Item(3,1).Value("last modified time");
xlRange.Item(4,1).Value("is directory");
xlRange.Item(5,1).Value("is file");
xlRange.Item(6,1).Value("can read");
xlRange.Item(7,1).Value("can write");
xlBook.Close(false,null,false);
excel.Quit();
See http://sourceforge.net/projects/jcom
See also this HowTo for an alternative package to access a COM package from Java.
See http://www.extentech.com/estore/product_detail.jsp?product_group_id=228
Example (extract 3 images from a workbook, create a new workbook with them) :
doit("testImages.xls","Sheet1");
...
try{
sheet = tbo.getWorkSheet(sheetname);
// read images from sheet 1 -- .gif, .png, .jpg
ImageHandle[] extracted = sheet.getImages();
// extract and output images
for(int t=0;t<extracted.length;t++) {
System.out.println("Successfully extracted: "
+ workingdir + "testImageOut_"
+ extracted[t].getName()+"."
+extracted[t].getType());
FileOutputStream outimg = new FileOutputStream
(workingdir + extracted[t].getName()+"."
+extracted[t].getType());
extracted[t].write(outimg);
outimg.flush();
outimg.close();
}
tbo = new WorkBookHandle();
sheet = tbo.getWorkSheet("Sheet1");
CellHandle a1 = sheet.add
("New workbook with 3 images: a gif, a jpg, and a png", "A1");
// add to sheet
ImageHandle giffy = new ImageHandle(fin, sheet);
// add to sheet
for(int x=0;x<100;x++) {
fin = new FileInputStream(workingdir + "testImages.png");
ImageHandle jpgy = new ImageHandle(fin, sheet);
jpgy.setName("heart" + x);
// set the random x/y coords of picture
int ix = Math.round((float)((x * (Math.random()*10))));
jpgy.setX(100 + ix);
ix = Math.round((float)((x * (Math.random()*10))));
jpgy.setY(100 + ix);
sheet.insertImage(jpgy);
}
// get png image input stream
fin = new FileInputStream(workingdir + "testImages.jpg");
// add to sheet
ImageHandle pngy = new ImageHandle(fin, sheet);
// set just the x/y coords of picture
pngy.setX(10);
pngy.setY(200);
sheet.insertImage(pngy);
}
catch(Exception e){
System.err.println("testImages failed: " + e.toString());
}
testWrite(tbo, workingdir + "testImagesOut.xls");
WorkBookHandle newbook = new WorkBookHandle
(workingdir + "testImagesOut.xls",0);
System.out.println("Successfully read: " + newbook);
}
See also this HowTo for a way to create a simple XLS without any additional library.