Read XLSX file in Java
Apache POI 3.5 have added support to all the OOXML (docx, xlsx, etc.)
See the XSSF sub project
Try this:
- Unzip XLSX file
- Read XML files
- Compose and use data
Example code:
public Workbook getTemplateData(String xlsxFile) {
Workbook workbook = new Workbook();
parseSharedStrings(xlsxFile);
parseWorkesheet(xlsxFile, workbook);
parseComments(xlsxFile, workbook);
for (Worksheet worksheet : workbook.sheets) {
worksheet.dimension = manager.getDimension(worksheet);
}
return workbook;
}
private void parseComments(String tmpFile, Workbook workbook) {
try {
FileInputStream fin = new FileInputStream(tmpFile);
final ZipInputStream zin = new ZipInputStream(fin);
InputStream in = getInputStream(zin);
while (true) {
ZipEntry entry = zin.getNextEntry();
if (entry == null)
break;
String name = entry.getName();
if (name.endsWith(".xml")) { //$NON-NLS-1$
if (name.contains(COMMENTS)) {
parseComments(in, workbook);
}
}
zin.closeEntry();
}
in.close();
zin.close();
fin.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
}
private void parseComments(InputStream in, Workbook workbook) {
try {
DefaultHandler handler = getCommentHandler(workbook);
SAXParser saxParser = getSAXParser();
saxParser.parse(in, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
private DefaultHandler getCommentHandler(Workbook workbook) {
final Worksheet ws = workbook.sheets.get(0);
return new DefaultHandler() {
String lastTag = "";
private Cell ccell;
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
lastTag = qName;
if (lastTag.equals("comment")) {
String cellName = attributes.getValue("ref");
int r = manager.getRowIndex(cellName);
int c = manager.getColumnIndex(cellName);
Row row = ws.rows.get(r);
if (row == null) {
row = new Row();
row.index = r;
ws.rows.put(r, row);
}
ccell = row.cells.get(c);
if (ccell == null) {
ccell = new Cell();
ccell.cellName = cellName;
row.cells.put(c, ccell);
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String val = "";
if (ccell != null && lastTag.equals("t")) {
for (int i = start; i < start + length; i++) {
val += ch[i];
}
if (ccell.comment == null)
ccell.comment = val;
else {
ccell.comment += val;
}
}
}
};
}
private void parseSharedStrings(String tmpFile) {
try {
FileInputStream fin = new FileInputStream(tmpFile);
final ZipInputStream zin = new ZipInputStream(fin);
InputStream in = getInputStream(zin);
while (true) {
ZipEntry entry = zin.getNextEntry();
if (entry == null)
break;
String name = entry.getName();
if (name.endsWith(".xml")) { //$NON-NLS-1$
if (name.startsWith(SHARED_STRINGS)) {
parseStrings(in);
}
}
zin.closeEntry();
}
in.close();
zin.close();
fin.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
}
public void parseWorkesheet(String tmpFile, Workbook workbook) {
try {
FileInputStream fin = new FileInputStream(tmpFile);
final ZipInputStream zin = new ZipInputStream(fin);
InputStream in = getInputStream(zin);
while (true) {
ZipEntry entry = zin.getNextEntry();
if (entry == null)
break;
String name = entry.getName();
if (name.endsWith(".xml")) { //$NON-NLS-1$
if (name.contains("worksheets")) {
Worksheet worksheet = new Worksheet();
worksheet.name = name;
parseWorksheet(in, worksheet);
workbook.sheets.add(worksheet);
}
}
zin.closeEntry();
}
in.close();
zin.close();
fin.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
}
public void parseWorksheet(InputStream in, Worksheet worksheet)
throws IOException {
// read sheet1 sharedStrings
// styles, strings, formulas ...
try {
DefaultHandler handler = getDefaultHandler(worksheet);
SAXParser saxParser = getSAXParser();
saxParser.parse(in, handler);
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
where Workbook class:
public class Workbook {
Integer id = null;
public List<Worksheet> sheets = new ArrayList<Worksheet>();}
and Worksheet class:
public class Worksheet {
public Integer id = null;
public String name = null;
public String dimension = null;
public Map<Integer, Row> rows = new TreeMap<Integer, Row>();
public Map<Integer, Column> columns = new TreeMap<Integer, Column>();
public List<Span> spans = new ArrayList<Span>();}
and Row class:
public class Row {
public Integer id = null;
public Integer index = null;
public Row tmpRow = null;
public Style style = null;
public Double height = null;
public Map<Integer,Cell> cells = new TreeMap<Integer, Cell>();
public String spans = null;
public Integer customHeight = null;}
and Cell class:
public class Cell {
public Integer id = null;
public Integer rowIndex = null;
public Integer colIndex = null;
public String cellName = null;
public String text = null;
public String formula = null;
public String comment = null;
public Style style = null;
public Object value = null;
public Cell tmpCell = null;}
and Column class:
public class Column {
public Integer index = null;
public Style style = null;
public String width = null;
public Column tmpColumn = null;
}
and Span class:
public class Span {
Integer id = null;
String topLeft = null;
String bottomRight = null;
}
AFAIK there are no xlsx-libraries available yet. But there are some for old xls:
One library is jxls which internally uses the already mentioned POI.
2 other links: Handle Excel files, Java libraries to read and write Excel XLS document files.
I had to do this in .NET and I couldn't find any API's out there. My solution was to unzip the .xlsx, and dive right into manipulating the XML. It's not so bad once you create your helper classes and such.
There are some "gotchas" like the nodes all have to be sorted according to the way excel expects them, that I didn't find in the official docs. Excel has its own date timestamping, so you'll need to make a conversion formula.