This is a simplified version of the demo app HSSFReadWrite of Apache POI. I found theirs to be buggy, returning null cells (causing NullPointerException), so I wrote the below using iterators for the rows and cells, which seemed to be more reliable.The source is HERE
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Iterator;
/**
* File for HSSF testing/examples
*
* THIS IS NOT THE MAIN HSSF FILE!! This is a utility for testing functionality.
* It does contain sample API usage that may be educational to regular API
* users.
*
* @see #main
* @author Andrew Oliver (acoliver at apache dot org)
* @author Charles Johnson
*/
public final class HssfDumper {
/**
* creates an {@link HSSFWorkbook} the specified OS filename.
*/
private static HSSFWorkbook readFile(String filename)
throws IOException {
return new HSSFWorkbook(new FileInputStream(filename));
}
public static void main(String[] args) {
boolean usingOutputFile = false;
PrintWriter out = null;
if (args.length < 1) {
System.err.println("java HssfDumper <file to dump> [utf-8 output file name]");
return;
}
String fileName = args[0];
try {
if (args.length > 1) {
// Use output file
out = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(args[1]), "UTF-8"));
usingOutputFile = true;
} else {
out = new PrintWriter(System.out);
}
HSSFWorkbook wb = HssfDumper.readFile(fileName);
for (int k = 0; k < wb.getNumberOfSheets(); k++) {
HSSFSheet sheet = wb.getSheetAt(k);
int numRows = sheet.getPhysicalNumberOfRows();
out.printf("Sheet %d '%s' has %d row(s)\n", k,
wb.getSheetName(k), numRows);
for (Iterator<Row> rows = sheet.rowIterator(); rows.hasNext();) {
HSSFRow row = (HSSFRow) rows.next();
if (row == null) {
continue;
}
int numCells = row.getPhysicalNumberOfCells();
out.printf("Row %d has %d cell(s)\n",
row.getRowNum(), numCells);
for (Iterator<Cell> cells = row.cellIterator();
cells.hasNext();) {
HSSFCell cell = (HSSFCell) cells.next();
String value = null;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
value = "FORMULA value=" +
cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" +
cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
value = "STRING value=" +
cell.getStringCellValue();
break;
default:
}
out.printf("Cell (%d,%d) %s\n",
cell.getRowIndex(), cell.getColumnIndex(), value);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (usingOutputFile) {
out.close();
}
}
}
}
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Iterator;
/**
* File for HSSF testing/examples
*
* THIS IS NOT THE MAIN HSSF FILE!! This is a utility for testing functionality.
* It does contain sample API usage that may be educational to regular API
* users.
*
* @see #main
* @author Andrew Oliver (acoliver at apache dot org)
* @author Charles Johnson
*/
public final class HssfDumper {
/**
* creates an {@link HSSFWorkbook} the specified OS filename.
*/
private static HSSFWorkbook readFile(String filename)
throws IOException {
return new HSSFWorkbook(new FileInputStream(filename));
}
public static void main(String[] args) {
boolean usingOutputFile = false;
PrintWriter out = null;
if (args.length < 1) {
System.err.println("java HssfDumper <file to dump> [utf-8 output file name]");
return;
}
String fileName = args[0];
try {
if (args.length > 1) {
// Use output file
out = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(args[1]), "UTF-8"));
usingOutputFile = true;
} else {
out = new PrintWriter(System.out);
}
HSSFWorkbook wb = HssfDumper.readFile(fileName);
for (int k = 0; k < wb.getNumberOfSheets(); k++) {
HSSFSheet sheet = wb.getSheetAt(k);
int numRows = sheet.getPhysicalNumberOfRows();
out.printf("Sheet %d '%s' has %d row(s)\n", k,
wb.getSheetName(k), numRows);
for (Iterator<Row> rows = sheet.rowIterator(); rows.hasNext();) {
HSSFRow row = (HSSFRow) rows.next();
if (row == null) {
continue;
}
int numCells = row.getPhysicalNumberOfCells();
out.printf("Row %d has %d cell(s)\n",
row.getRowNum(), numCells);
for (Iterator<Cell> cells = row.cellIterator();
cells.hasNext();) {
HSSFCell cell = (HSSFCell) cells.next();
String value = null;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
value = "FORMULA value=" +
cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" +
cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
value = "STRING value=" +
cell.getStringCellValue();
break;
default:
}
out.printf("Cell (%d,%d) %s\n",
cell.getRowIndex(), cell.getColumnIndex(), value);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (usingOutputFile) {
out.close();
}
}
}
}