Commit 053a45fd5dd33676b657d1619cdc75f0fba68bd6

Authored by 孙向锦
1 parent a7c57a42

解析excel bug,去掉空行

src/com/fh/controller/sunvote/student/StudentController.java
@@ -61,7 +61,7 @@ public class StudentController extends BaseController { @@ -61,7 +61,7 @@ public class StudentController extends BaseController {
61 if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限 61 if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
62 ModelAndView mv = this.getModelAndView(); 62 ModelAndView mv = this.getModelAndView();
63 PageData pd = new PageData(); 63 PageData pd = new PageData();
64 - pd = this.getPageData(); 64 + pd = this.getPageData();
65 studentService.save(pd); 65 studentService.save(pd);
66 mv.addObject("msg","success"); 66 mv.addObject("msg","success");
67 mv.setViewName("save_result"); 67 mv.setViewName("save_result");
@@ -108,6 +108,8 @@ public class StudentController extends BaseController { @@ -108,6 +108,8 @@ public class StudentController extends BaseController {
108 public ModelAndView goUploadExcel()throws Exception{ 108 public ModelAndView goUploadExcel()throws Exception{
109 ModelAndView mv = this.getModelAndView(); 109 ModelAndView mv = this.getModelAndView();
110 mv.addObject("pd", getPageData()); 110 mv.addObject("pd", getPageData());
  111 +
  112 +
111 mv.setViewName("sunvote/student/uploadexcel"); 113 mv.setViewName("sunvote/student/uploadexcel");
112 return mv; 114 return mv;
113 } 115 }
@@ -141,20 +143,23 @@ public class StudentController extends BaseController { @@ -141,20 +143,23 @@ public class StudentController extends BaseController {
141 if(temp.get("var" + j) == null){ 143 if(temp.get("var" + j) == null){
142 break ; 144 break ;
143 } 145 }
144 - savePd.put(pd.getString("var" + j).toUpperCase(), temp.get("var" + j)); 146 + if(temp.get("var" + j) != null && !"".equals(temp.get("var" + j).toString())){
  147 + savePd.put(pd.getString("var" + j).toUpperCase(), temp.get("var" + j));
  148 + }
145 } 149 }
146 if(!savePd.containsKey("ID")){ 150 if(!savePd.containsKey("ID")){
147 - String studentId = this.get32UUID();  
148 - savePd.put("ID", studentId);  
149 - studentService.save(savePd);  
150 -  
151 -  
152 - if (classID != null && termID != null) {  
153 - savePd.put("STUDENT_ID", studentId);  
154 - savePd.put("CLASSROSTER_ID", get32UUID());  
155 - savePd.put("TEAMID", termID);  
156 - savePd.put("SCLASS_ID", classID);  
157 - classrosterService.save(savePd); 151 + if (savePd.values().size() > 0) {
  152 + String studentId = this.get32UUID();
  153 + savePd.put("ID", studentId);
  154 + studentService.save(savePd);
  155 +
  156 + if (classID != null && termID != null) {
  157 + savePd.put("STUDENT_ID", studentId);
  158 + savePd.put("CLASSROSTER_ID", get32UUID());
  159 + savePd.put("TEAMID", termID);
  160 + savePd.put("SCLASS_ID", classID);
  161 + classrosterService.save(savePd);
  162 + }
158 } 163 }
159 164
160 }else{ 165 }else{
src/com/fh/util/ObjectExcelRead.java
@@ -10,65 +10,69 @@ import org.apache.poi.hssf.usermodel.HSSFRow; @@ -10,65 +10,69 @@ import org.apache.poi.hssf.usermodel.HSSFRow;
10 import org.apache.poi.hssf.usermodel.HSSFSheet; 10 import org.apache.poi.hssf.usermodel.HSSFSheet;
11 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 11 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
12 12
13 - 13 +/**
  14 + * @version
  15 + */
14 public class ObjectExcelRead { 16 public class ObjectExcelRead {
15 17
16 /** 18 /**
17 - * @param filepath //文件路径  
18 - * @param filename //文件名  
19 - * @param startrow //开始行号  
20 - * @param startcol //开始列号  
21 - * @param sheetnum //sheet 19 + * @param filepath
  20 + * //文件路径
  21 + * @param filename
  22 + * //文件名
  23 + * @param startrow
  24 + * //开始行号
  25 + * @param startcol
  26 + * //开始列号
  27 + * @param sheetnum
  28 + * //sheet
22 * @return list 29 * @return list
23 */ 30 */
24 - public static List<Object> readExcel(String filepath, String filename, int startrow, int startcol, int sheetnum) { 31 + public static List<Object> readExcel(String filepath, String filename,
  32 + int startrow, int startcol, int sheetnum) {
25 List<Object> varList = new ArrayList<Object>(); 33 List<Object> varList = new ArrayList<Object>();
26 34
27 try { 35 try {
28 File target = new File(filepath, filename); 36 File target = new File(filepath, filename);
29 FileInputStream fi = new FileInputStream(target); 37 FileInputStream fi = new FileInputStream(target);
30 HSSFWorkbook wb = new HSSFWorkbook(fi); 38 HSSFWorkbook wb = new HSSFWorkbook(fi);
31 - HSSFSheet sheet = wb.getSheetAt(sheetnum); //sheet 从0开始  
32 - int rowNum = sheet.getLastRowNum() + 1; //取得最后一行的行号  
33 -  
34 - for (int i = startrow; i < rowNum; i++) { //行循环开始  
35 - 39 + HSSFSheet sheet = wb.getSheetAt(sheetnum); // sheet 从0开始
  40 + int rowNum = sheet.getLastRowNum() + 1; // 取得最后一行的行号
  41 + for (int i = startrow; i < rowNum; i++) { // 行循环开始
36 PageData varpd = new PageData(); 42 PageData varpd = new PageData();
37 - HSSFRow row = sheet.getRow(i); //行  
38 - int cellNum = row.getLastCellNum(); //每行的最后一个单元格位置  
39 -  
40 - for (int j = startcol; j < cellNum; j++) { //列循环开始  
41 - 43 + HSSFRow row = sheet.getRow(i); // 行
  44 + int cellNum = row.getLastCellNum(); // 每行的最后一个单元格位置
  45 + for (int j = startcol; j < cellNum; j++) { // 列循环开始
42 HSSFCell cell = row.getCell(Short.parseShort(j + "")); 46 HSSFCell cell = row.getCell(Short.parseShort(j + ""));
43 String cellValue = null; 47 String cellValue = null;
44 if (null != cell) { 48 if (null != cell) {
45 - switch (cell.getCellType()) { // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库  
46 - case 0:  
47 - cellValue = String.valueOf((int) cell.getNumericCellValue());  
48 - break;  
49 - case 1: 49 + int type = cell.getCellType();
  50 + if (type == 0) {
  51 + cellValue = String.valueOf((long) cell
  52 + .getNumericCellValue());
  53 + }
  54 + if (type == 1) {
50 cellValue = cell.getStringCellValue(); 55 cellValue = cell.getStringCellValue();
51 - break;  
52 - case 2: 56 + }
  57 + if (type == 2) {
53 cellValue = cell.getNumericCellValue() + ""; 58 cellValue = cell.getNumericCellValue() + "";
54 - // cellValue = String.valueOf(cell.getDateCellValue());  
55 - break;  
56 - case 3: 59 + }
  60 + if (type == 3) {
57 cellValue = ""; 61 cellValue = "";
58 - break;  
59 - case 4:  
60 - cellValue = String.valueOf(cell.getBooleanCellValue());  
61 - break;  
62 - case 5:  
63 - cellValue = String.valueOf(cell.getErrorCellValue());  
64 - break; 62 + }
  63 + if (type == 4) {
  64 + cellValue = String.valueOf(cell
  65 + .getBooleanCellValue());
  66 + }
  67 + if (type == 5) {
  68 + cellValue = String
  69 + .valueOf(cell.getErrorCellValue());
65 } 70 }
66 } else { 71 } else {
67 cellValue = ""; 72 cellValue = "";
68 } 73 }
69 -  
70 - varpd.put("var"+j, cellValue);  
71 - 74 + varpd.put("var" + j, cellValue);
  75 + System.out.println(cellValue);
72 } 76 }
73 varList.add(varpd); 77 varList.add(varpd);
74 } 78 }
@@ -76,7 +80,7 @@ public class ObjectExcelRead { @@ -76,7 +80,7 @@ public class ObjectExcelRead {
76 } catch (Exception e) { 80 } catch (Exception e) {
77 System.out.println(e); 81 System.out.println(e);
78 } 82 }
79 - 83 +
80 return varList; 84 return varList;
81 } 85 }
82 } 86 }