1.单元格各类型数据读取
1.1 基本类型
处理的Excel数据包括字符型数据,数字、日期、公式等。
下面是单元格类型说明:
2实例
解析excel中数据,要求转换为文本方式存储
2.1 写一个excel解析的抽象类
public abstract class ExcelParser {
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();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。