1 public static Collection importExcelByIs(InputStream inputstream,
2 List<CgFormFieldEntity> lists) {
3 Map<String, CgFormFieldEntity> fieldMap = ConvertDate(lists);
4 //返回的数据类型
5 List<Map<String, Object>> tObject = new ArrayList<Map<String,Object>>();
6 try {
7 // 将传入的File构造为FileInputStream;
8 // // 得到工作表
9 HSSFWorkbook book = new HSSFWorkbook(inputstream);
10 // // 得到第一页
11 HSSFSheet sheet = book.getSheetAt(0);
12 // // 得到第一面的所有行
13 Iterator<Row> row = sheet.rowIterator();
14 // 得到第一行,也就是标题行
15 Row title = row.next();
16 // 得到第一行的所有列
17 Iterator<Cell> cellTitle = title.cellIterator();
18 // 将标题的文字内容放入到一个map中。
19 Map titlemap = new HashMap();
20 // 从标题第一列开始
21 int i = 0;
22 // 循环标题所有的列
23 while (cellTitle.hasNext()) {
24 Cell cell = cellTitle.next();
25 String value = cell.getStringCellValue();
26 if (fieldMap.get(value)==null) {
27 throw new BusinessException("导入数据excel列名有不能识别的列");
28 }
29 titlemap.put(i, value);
30 i = i + 1;
31 }
32 // 用来格式化日期的DateFormat
33 Map<String, Object> retMap=null;
34 while (row.hasNext()) {
35 retMap= new HashMap<String, Object>();
36 // 标题下的第一行
37 Row rown = row.next();
38 // 行的所有列
39 Iterator<Cell> cellbody = rown.cellIterator();
40 int k = 0;
41 // 遍历一行的列
42 while (cellbody.hasNext()) {
43 Cell cell = cellbody.next();
44 // 这里得到此列的对应的标题
45 String titleString = (String) titlemap.get(k);
46 if (fieldMap.containsKey(titleString)) {
47 retMap.put(fieldMap.get(titleString).getFieldName(), getCellValueString(cell));
48 }
49 // 下一列
50 k = k + 1;
51 }
52 tObject.add(retMap);
53 }
54 } catch (Exception e) {
55 e.printStackTrace();
56 return null;
57 }
58 return tObject;
59 }
60 //TODO huiyong excel日期格式处理默认如此,需要从数据库或者配置文件读取
61 public final static DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
62 /**
63 * 得到某个格子的值 已经对过时方法进行更新
64 *
65 * @param cell
66 * 格子对象
67 * @return 格子的值
68 */
69 public static String getCellValueString(Cell cell) {
70 if (cell == null) {
71 return null;
72 }
73 // 时间对象 特殊处理
74 int dataFormat = cell.getCellStyle().getDataFormat();
75
76 if (dataFormat == 14 || dataFormat == 178 || dataFormat == 180 || dataFormat == 181
77 || dataFormat == 182) {
78 return getDateValue(cell);
79 }
80 String value = null;
81 switch (cell.getCellType()) {
82 case Cell.CELL_TYPE_NUMERIC :
83 value = new DecimalFormat("0.##########").format(cell.getNumericCellValue());
84 break;
85 case Cell.CELL_TYPE_STRING :
86 // value = cell.getStringCellValue();
87 value = cell.getRichStringCellValue().toString();
88 break;
89 case Cell.CELL_TYPE_FORMULA :
90 value = String.valueOf(cell.getCellFormula());
91 break;
92 case Cell.CELL_TYPE_BLANK :
93 // value = String.valueOf(cell.getStringCellValue());
94 value = String.valueOf(cell.getRichStringCellValue().toString());
95 break;
96 case Cell.CELL_TYPE_BOOLEAN :
97 value = String.valueOf(cell.getBooleanCellValue());
98 break;
99 case Cell.CELL_TYPE_ERROR :
100 value = String.valueOf(cell.getErrorCellValue());
101 break;
102 }
103 return value;
104 }
105 /**
106 * 返回时间内的特殊时间格式 OFFICE2003
107 * @param cell
108 * @return
109 */
110 private static String getDateValue(Cell cell){
111 return DEFAULT_DATE_FORMAT.format(cell.getDateCellValue());
112 }
113 /**
114 * 数据处理
115 */
116 private static Map<String,CgFormFieldEntity> ConvertDate(List<CgFormFieldEntity> lists){
117 Map<String,CgFormFieldEntity> maps = new HashMap<String, CgFormFieldEntity>();
118
119 for (CgFormFieldEntity cgFormFieldEntity : lists) {
120 maps.put(cgFormFieldEntity.getContent(), cgFormFieldEntity);
121 }
122 return maps;
123 }