import java.io.*; import java.text.*; import org.apache.poi.hssf.usermodel.*; public class ExcelTest { // 01234567890123456789012345 static final String ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; static String makeName(int in) { StringBuffer sb = new StringBuffer(); int div=in/26; int rem = in % 26; String partial = ""; if (div>0) { if (div<26) { div--; } partial = makeName(div); sb = new StringBuffer(partial); } else { sb.append(ch.charAt(rem)); return sb.toString(); } sb.append(ch.charAt(rem)); return sb.toString(); } public boolean byteForm() { return true; } //cannot trace without a worker public static final void main(String[] args) { if (args==null || args.length==0) { System.out.println("enter name of excel file"); return; } String filename = args[0]; try { HSSFWorkbook w = new HSSFWorkbook(new FileInputStream(filename)); for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) { String sheetName; HSSFSheet s = w.getSheetAt(sheet); // get the sheet from the workbook sheetName = w.getSheetName(sheet); // get the name of the sheet int firstRowNumber= s.getFirstRowNum(); int lastRowNumber= s.getLastRowNum(); int rowCount = s.getPhysicalNumberOfRows(); System.out.println("sheet "+sheetName+" first row "+ firstRowNumber+", last row "+ lastRowNumber); System.out.println("sheet "+sheetName+" has "+ rowCount+" rows"); if (rowCount==0) { continue; } for (int row=firstRowNumber; row<=lastRowNumber; row++) { HSSFRow r = s.getRow(row); if (r==null) // if there is nothing in a row, it comes back as null { continue; } HSSFCell c = null; int firstCellNumber= r.getFirstCellNum(); int lastCellNumber= r.getLastCellNum(); int colCount= lastCellNumber - firstCellNumber+1; System.out.println("row "+row+": first cell number: "+firstCellNumber+ ", lastCellNumber: "+lastCellNumber + ", column count: "+colCount); String namesArray[] = new String[colCount]; for (int cell=firstCellNumber; cell<=lastCellNumber; cell++) { //System.out.println("row "+row+", cell "+cell); String cellValue = null; int nameIndex = lastCellNumber-cell; namesArray[nameIndex] = makeName(cell); String cTitle = namesArray[nameIndex]; c = r.getCell((short)cell); if (c!=null) // if there is nothing in a cell, it comes back as null { //trace(XDState.DEBUG,"Excel Preparser: row "+row+", cell "+cell+": "+c+",type "+c.getCellType()); switch (c.getCellType()) { case(HSSFCell.CELL_TYPE_NUMERIC): { double d = c.getNumericCellValue(); DecimalFormat formatter = new DecimalFormat(); // default rounds to 3 decimal digits cellValue = formatter.format(d); break; } case(HSSFCell.CELL_TYPE_BOOLEAN): { cellValue = new Boolean(c.getBooleanCellValue()).toString(); break; } case(HSSFCell.CELL_TYPE_STRING): { cellValue = c.getStringCellValue(); break; } case(HSSFCell.CELL_TYPE_FORMULA): { double d = c.getNumericCellValue(); DecimalFormat formatter = new DecimalFormat(); // default rounds to 3 decimal digits cellValue = formatter.format(d); break; } case(HSSFCell.CELL_TYPE_ERROR): { cellValue = Byte.toString(c.getErrorCellValue()); break; } case(HSSFCell.CELL_TYPE_BLANK): { cellValue = c.getStringCellValue(); break; } default: { cellValue = "Unknown cell type:"+c.getCellType(); } } System.out.println("cell at row "+row+", col "+cell+", value "+cellValue); } } } } } catch (Exception e) { System.out.println(e+"; "+e.getMessage()); e.printStackTrace(); } } }