package org.asoware.budget.conv; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; public class WorksheetProcessor { public static final short DATE_IDX = 0; public static final short AMOUNT_IDX = 1; public static final short CATEGORY_IDX = 2; private EntityManager em; private HSSFSheet sheet; public WorksheetProcessor(HSSFSheet sheet) { this.sheet = sheet; } public void process() throws SheetProcessingException { int rowIdx = 0; while (true) { Date date = getDate(rowIdx, DATE_IDX); if (date == null) { break; } Double amount = getDouble(rowIdx, AMOUNT_IDX); String categoryCode = getString(rowIdx, CATEGORY_IDX); System.out.println(date + ", " + amount + ", " + categoryCode); // System.out.printf("%15tF %10:2f %s", date, amount, categoryCode); rowIdx++; } } @PersistenceContext private void setEntityManager(EntityManager em) { this.em = em; } private Date getDate(int rowIdx, short colIdx) { HSSFCell cell = getCell(rowIdx, colIdx); if (cell != null) { return cell.getDateCellValue(); } else { return null; } } private double getDouble(int rowIdx, short colIdx) { HSSFCell cell = getCell(rowIdx, colIdx); if (cell != null) { return cell.getNumericCellValue(); } else { return 0; } } private String getString(int rowIdx, short colIdx) { HSSFCell cell = getCell(rowIdx, colIdx); if (cell != null) { return cell.getStringCellValue(); } else { return null; } } private HSSFCell getCell(int rowIdx, short colIdx) { HSSFRow row = sheet.getRow(rowIdx); return row.getCell(colIdx); } }