POI读取excel单元格,获取单元格各类型值,返回字符串类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package a; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import landprice.util.DateUtil; public class TestReadFormula { private static FormulaEvaluator evaluator; public static void main(String[] args) throws IOException { InputStream is= new FileInputStream( "E:/temp/tempfile/1.xls" ); HSSFWorkbook wb= new HSSFWorkbook(is); Sheet sheet=wb.getSheetAt( 0 ); evaluator=wb.getCreationHelper().createFormulaEvaluator(); for ( int i = 0 ; i < 1 ; i++) { Row row=sheet.getRow(i); for (Cell cell : row) { System.out.println(getCellValueByCell(cell)); } } is.close(); } //获取单元格各类型值,返回字符串类型 private static String getCellValueByCell(Cell cell) { //判断是否为null或空串 if (cell== null || cell.toString().trim().equals( "" )) { return "" ; } String cellValue = "" ; int cellType=cell.getCellType(); if (cellType==Cell.CELL_TYPE_FORMULA){ //表达式类型 cellType=evaluator.evaluate(cell).getCellType(); } switch (cellType) { case Cell.CELL_TYPE_STRING: //字符串类型 cellValue= cell.getStringCellValue().trim(); cellValue=StringUtils.isEmpty(cellValue) ? "" : cellValue; break ; case Cell.CELL_TYPE_BOOLEAN: //布尔类型 cellValue = String.valueOf(cell.getBooleanCellValue()); break ; case Cell.CELL_TYPE_NUMERIC: //数值类型 if (HSSFDateUtil.isCellDateFormatted(cell)) { //判断日期类型 cellValue = DateUtil.formatDateByFormat(cell.getDateCellValue(), "yyyy-MM-dd" ); } else { //否 cellValue = new DecimalFormat( "#.######" ).format(cell.getNumericCellValue()); } break ; default : //其它类型,取空串吧 cellValue = "" ; break ; } return cellValue; } } |