ReadExcelUtils.java
4.4 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.rivercloud.utils;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelUtils {
private Workbook wb;
private Sheet sheet;
private Row row;
public ReadExcelUtils(File file) {
String ext = file.getName().substring(file.getName().lastIndexOf("."));
try {
FileInputStream is = new FileInputStream(file);
if(".xls".equals(ext)){
wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(ext)){
wb = new XSSFWorkbook(is);
}else{
wb=null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取Excel数据内容
*
*/
public Map<Integer, Object[]> readExcelContent() throws Exception{
if(wb==null){
throw new Exception("Workbook对象为空!");
}
Map<Integer, Object[]> content = new HashMap<Integer, Object[]>();
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
//得到标题
Object[] title = new Object[colNum];
//Map title = new HashMap();
for (int i = 0; i <= colNum-1; i++) {
Object obj = getCellFormatValue(row.getCell(i));
title[i] = obj;
}
//得到正文内容
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
Object[] cellValue = new Object[colNum];
Map cellvalue = new HashMap();
for (int j = 0;j <= colNum-1;j++){
Object obj = getCellFormatValue(row.getCell(j));
cellValue[j] = obj;
}
content.put(0,title);
content.put(i, cellValue);
}
return content;
}
/**
*
* 根据Cell类型设置数据
*
*/
private Object getCellFormatValue(Cell cell) {
Object result = "";
/*if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
cell.setCellType(1);
result = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
break;
case Cell.CELL_TYPE_ERROR:
result = cell.getErrorCellValue();
break;
case Cell.CELL_TYPE_BLANK:
break;
default:
break;
}
}*/
if (cell !=null&&cell.getCellType() == Cell.CELL_TYPE_STRING){//字符串类型
result = cell.getStringCellValue();
}
if (cell !=null&&cell.getCellType() == Cell.CELL_TYPE_NUMERIC){//数值类型
if (DateUtil.isCellDateFormatted(cell)) {
result = cell.getDateCellValue();
}else {
Long longVal = Math.round(cell.getNumericCellValue());
Double doubleVal = cell.getNumericCellValue();
if(Double.parseDouble(longVal + ".0") == doubleVal){ //判断是否含有小数位.0
result = longVal;
}
else{
result = doubleVal;
}
}
}
if (cell !=null&&cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){//布尔类型
result = cell.getBooleanCellValue();
}
if (cell !=null&&cell.getCellType() == Cell.CELL_TYPE_FORMULA){//表达式类型
result = cell.getCellFormula();
}
if (cell !=null&&cell.getCellType() == Cell.CELL_TYPE_ERROR){//错误类型
result = cell.getErrorCellValue();
}
if (cell !=null&&cell.getCellType() == Cell.CELL_TYPE_BLANK){//空
}
return result.toString();
}
}