2.复杂读取
2.1 单元格各类型数据读取
2.1.1 基本类型
在实际工作中,我们处理的Excel数据都不止限于字符型数据,更多的是数字、日期、甚至公式等。
下面是单元格类型说明:
类型 |
|
CELL_TYPE_BLANK | 空值(cell不为空) |
CELL_TYPE_BOOLEAN | 布尔 |
CELL_TYPE_ERROR | 错误 |
CELL_TYPE_FORMULA | 公式 |
CELL_TYPE_STRING | 字符串 |
CELL_TYPE_NUMERIC | 数值 |
以上单元格的类型,可以通过getCellType()方法获得,返回值为int。
下面读取一个多类型数据Excel文件:
图7中,数据文件格式包括字符、数字、公式、布尔。
图7
代码片段:
- public static void read(InputStream inputStream) throws IOException{
- HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
- //循环workbook中所有sheet
- for(int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++){
- HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
- //循环该sheet中的有数据的每一行
- for(int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++){
- HSSFRow row = sheet.getRow(rowIndex);
- if(row == null){
- continue;
- }
- //循环该行的每一个单元格
- for(int cellnum = 0; cellnum < row.getLastCellNum(); cellnum++){
- HSSFCell cell = row.getCell(cellnum);
- getCellValue(cell, rowIndex, cellnum);
- }
- }
- }
- }
- public static void getCellValue(HSSFCell cell, int rowIndex, int cellnum){
- if(cell == null){
- return;
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_BLANK");
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_STRING 值为:"+cell.getRichStringCellValue().getString());
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_NUMERIC 值为:"+cell.getNumericCellValue());
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_BOOLEAN 值为:"+cell.getBooleanCellValue());
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_FORMULA 值为:"+cell.getNumericCellValue()+" 公式为:"+cell.getCellFormula());
- }
- return;
- }
打印预览:
![](http://hi.csdn.net/attachment/201109/1/0_1314860753YIki.gif)
注:公式格式的单元格值为数字,所以取值的时候是通过cell.getNumericCellValue()方法,而取单元格的公式需要通过cell.getCellFormula()方法。
2.1.2 日期类型
Excel中的Date类型以Double型数字存储的,表示当前时间与1900年1月1日相隔的天数。所以在Excel中如果单元格格式为NUMERIC类型还需要进一步判断是否为日期类型。在读取日期单元格时需要调用HSSFDateUtil的isCellDateFormatted方法,来判断该Cell的数据格式是否是Date类型,然后通过HSSFCell的getDateCellValue方法获取Date。
如图9,在之前Excel数据后新加了Date格式的数据
图9
代码片段:
- public static void getCellValue(HSSFCell cell, int rowIndex, int cellnum){
- if(cell == null){
- return;
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_BLANK");
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_STRING 值为:"+cell.getRichStringCellValue().getString());
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
- if(HSSFDateUtil.isCellDateFormatted(cell)){
- Date date = cell.getDateCellValue();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:Date 值为:"+dateFormat.format(date));
- }else{
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_NUMERIC 值为:"+cell.getNumericCellValue());
- }
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_BOOLEAN 值为:"+cell.getBooleanCellValue());
- }else if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){
- System.out.println("第"+(rowIndex+1)+"行,第"+(cellnum+1)+"列 cellType为:CELL_TYPE_FORMULA 值为:"+cell.getNumericCellValue()+" 公式为:"+cell.getCellFormula());
- }
- return;
- }
打印预览:
图10
2.2 自定义类型
在Excel中有许多种数据格式,并且支持用户自定义格式。如图11。可以通过HSSFDataFormat类进行操作。getBuiltinFormat(short index)方法根据编号返回内置数据类型,getBuiltinFormat(java.lang.Stringformat)方法根据数据类型返回其编号,getBuiltinFormats()返回整个内置的数据格式列表。
图11
HSSFDataFormat的数据格式
内置数据类型 | 编号 |
"General" | 0 |
"0" | 1 |
"0.00" | 2 |
"#,##0" | 3 |
"#,##0.00" | 4 |
"($#,##0_);($#,##0)" | 5 |
"($#,##0_);[Red]($#,##0)" | 6 |
"($#,##0.00);($#,##0.00)" | 7 |
"($#,##0.00_);[Red]($#,##0.00)" | 8 |
"0%" | 9 |
"0.00%" | 0xa |
"0.00E+00" | 0xb |
"# ?/?" | 0xc |
"# ??/??" | 0xd |
"m/d/yy" | 0xe |
"d-mmm-yy" | 0xf |
"d-mmm" | 0x10 |
"mmm-yy" | 0x11 |
"h:mm AM/PM" | 0x12 |
"h:mm:ss AM/PM" | 0x13 |
"h:mm" | 0x14 |
"h:mm:ss" | 0x15 |
"m/d/yy h:mm" | 0x16 |
保留为过国际化用 | 0x17 - 0x24 |
"(#,##0_);(#,##0)" | 0x25 |
"(#,##0_);[Red](#,##0)" | 0x26 |
"(#,##0.00_);(#,##0.00)" | 0x27 |
"(#,##0.00_);[Red](#,##0.00)" | 0x28 |
"_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)" | 0x29 |
"_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)" | 0x2a |
"_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)" | 0x2b |
"_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)" | 0x2c |
"mm:ss" | 0x2d |
"[h]:mm:ss" | 0x2e |
"mm:ss.0" | 0x2f |
"##0.0E+0" | 0x30 |
"@" - This is text format | 0x31 |