You can go one better that and use POI:
http://jakarta.apache.org/poi/
I had a play with it a couple of nights ago. It's really cool:
Code:
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
String cellName = args[0];
short rowNumber = getRowNumber(cellName);
short cellNumber = getCellNumber(cellName);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("Test.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(rowNumber);
HSSFCell cell = row.getCell(cellNumber);
String value = cell.getStringCellValue();
System.out.println(cellName + " == \"" + value + "\"");
}
private static short getRowNumber(String cellName) {
String strNumber = cellName.substring(1);
short number = Short.parseShort(strNumber);
number--;
return number;
}
private static short getCellNumber(String cellName) {
char letter = cellName.charAt(0);
short number = (short)(letter - 'A');
return number;
}
}
To use this code, create a spreadsheet "Test.xls"with some text in each of the cells. Then invoke this program:
java Test A2
...and it will print out the contents of cell A2.
The program is particularly robust, so treat it carefully.
Bookmarks