1.單元格各種型數據讀取this
1.1 基本類型spa
處理的Excel數據包括字符型數據,數字、日期、公式等。excel
下面是單元格類型說明:
2實例
解析excel中數據,要求轉換爲文本方式存儲
2.1 寫一個excel解析的抽象類code
public abstract class ExcelParser {orm
private String fileName; private InputStream inputStream; private final static String excel2003L = ".xls";//2003- 版本的excel private final static String excel2007U = ".xlsx";//2007+ 版本的excel protected final static DecimalFormat decimalFormat = new DecimalFormat("0"); protected final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd"); protected final static String[] columnStr = new String[]{ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; protected OrderExcelParser(String fileName, InputStream inputStream) { this.fileName = fileName; this.inputStream = inputStream; } protected Workbook getWorkbook() throws Exception { try { String fileType = fileName.substring(fileName.lastIndexOf(".")); if (excel2003L.equals(fileType)) { return new HSSFWorkbook(inputStream); //2003- } else if (excel2007U.equals(fileType)) { return new XSSFWorkbook(inputStream); //2007+ } else { throw new Exception(); } } catch (Exception e) { throw new Exception("解析的文件格式有誤!"); } } protected abstract void parseTitles(int rowIndex) throws Exception; protected String getCellStringValue(Cell cell) throws Exception { String cellValue = ""; if (cell == null) { return cellValue; } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { double d = cell.getNumericCellValue(); Date date = HSSFDateUtil.getJavaDate(d); cellValue = simpleDateFormat.format(date); } else { cellValue = decimalFormat.format((cell.getNumericCellValue())); } } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { cellValue = ""; } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { cellValue = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) { cellValue = ""; } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { cellValue = cell.getCellFormula(); } return cellValue.trim(); } }