浏览代码

修改util包到commons

ting.yin 8 年之前
父节点
当前提交
872c7558d5

+ 3 - 2
exam-work-api/pom.xml

@@ -45,8 +45,9 @@
             <version>2.8.0</version>
         </dependency>
 		<dependency>
-            <groupId>org.apache.poi</groupId>
-            <artifactId>poi-ooxml</artifactId>
+            <groupId>cn.com.qmth.examcloud.common</groupId>
+            <artifactId>common-uac</artifactId>
+             <version>${exam.cloud.version}</version>
         </dependency>
     </dependencies>
 

+ 18 - 8
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/api/ExamStudentApi.java

@@ -1,21 +1,31 @@
 package cn.com.qmth.examcloud.service.examwork.api;
 
+import io.swagger.annotations.ApiOperation;
+
 import java.io.IOException;
 import java.util.List;
 
-import cn.com.qmth.examcloud.service.examwork.dao.ExamStudentRepo;
-import cn.com.qmth.examcloud.service.examwork.entity.ExamStudent;
-import cn.com.qmth.examcloud.service.examwork.service.ExamStudentService;
-import cn.com.qmth.examcloud.service.examwork.util.ExcelError;
-import io.swagger.annotations.ApiOperation;
-
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
+import cn.com.qmth.examcloud.common.util.excel.ExcelError;
+import cn.com.qmth.examcloud.service.examwork.dao.ExamStudentRepo;
+import cn.com.qmth.examcloud.service.examwork.entity.ExamStudent;
+import cn.com.qmth.examcloud.service.examwork.service.ExamStudentService;
+
 /**
  * 考生服务API
  * Created by songyue on 17/1/13.
@@ -74,7 +84,7 @@ public class ExamStudentApi {
     }
     
     @ApiOperation(value="导入考试学生",notes = "导入")
-    @DeleteMapping("/exam_student/import")
+    @PostMapping("/exam_student/import")
     public ResponseEntity importExamStudent(@RequestParam Long examId,@RequestParam MultipartFile file){
     	try {
     		List<ExcelError> excelErrors = examStudentService.importExamStudent(examId,file.getInputStream());

+ 5 - 3
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/service/ExamStudentService.java

@@ -13,13 +13,13 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
+import cn.com.qmth.examcloud.common.util.excel.ExcelError;
+import cn.com.qmth.examcloud.common.util.excel.ExcelReader;
+import cn.com.qmth.examcloud.common.util.excel.ExcelReaderHandle;
 import cn.com.qmth.examcloud.service.examwork.dao.ExamRepo;
 import cn.com.qmth.examcloud.service.examwork.dao.ExamStudentRepo;
 import cn.com.qmth.examcloud.service.examwork.entity.Exam;
 import cn.com.qmth.examcloud.service.examwork.entity.ExamStudent;
-import cn.com.qmth.examcloud.service.examwork.util.ExcelError;
-import cn.com.qmth.examcloud.service.examwork.util.ExcelReader;
-import cn.com.qmth.examcloud.service.examwork.util.ExcelReaderHandle;
 
 /**
  * 考试学生服务类
@@ -122,10 +122,12 @@ public class ExamStudentService {
                 if (error == null) {
                 	examStudents.add(dto);
                 }
+                
                 return error;
             }
         });
         examStudentRepo.save(examStudents);
+        examStudents.removeAll(examStudents);
         return excelErrors;
 	}
 	/**

+ 0 - 57
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ColumnSetting.java

@@ -1,57 +0,0 @@
-package cn.com.qmth.examcloud.service.examwork.util;
-
-public class ColumnSetting implements Comparable<ColumnSetting> {
-
-    private String header;
-    private String fieldName;
-    private int width;
-    private int index;
-
-    public ColumnSetting(String header,String fieldName, int width, int index) {
-        this.header = header;
-        this.fieldName = fieldName;
-        this.width = width;
-        this.index = index;
-    }
-
-    public String getHeader() {
-        return header;
-    }
-
-    public void setHeader(String header) {
-        this.header = header;
-    }
-
-    public int getWidth() {
-        return width;
-    }
-
-    public void setWidth(int width) {
-        this.width = width;
-    }
-
-    public int getIndex() {
-        return index;
-    }
-
-    public void setIndex(int index) {
-        this.index = index;
-    }
-
-    public String getFieldName() {
-		return fieldName;
-	}
-
-	public void setFieldName(String fieldName) {
-		this.fieldName = fieldName;
-	}
-
-	@Override
-    public int compareTo(ColumnSetting columnSetting) {
-        if (index < columnSetting.getIndex())
-            return - 1 ;
-        if (index > columnSetting.getIndex())
-            return 1 ;
-        return 0 ;
-    }
-}

+ 0 - 48
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ExcelError.java

@@ -1,48 +0,0 @@
-package cn.com.qmth.examcloud.service.examwork.util;
-/**
- * 
- * @Description: excel导入错误
- * @author ting.yin
- * @date 2016年8月19日
- */
-public class ExcelError {
-
-	/**
-	 * 错误行数
-	 */
-	private int row;
-
-	/**
-	 * 错误类型
-	 */
-	private String excelErrorType;
-
-	public ExcelError() {
-
-	}
-
-	public ExcelError(int row, String excelErrorType) {
-		this.row = row;
-		this.excelErrorType = excelErrorType;
-	}
-	
-	public ExcelError(String excelErrorType) {
-		this.excelErrorType = excelErrorType;
-	}
-	public int getRow() {
-		return row;
-	}
-
-	public void setRow(int row) {
-		this.row = row;
-	}
-
-	public String getExcelErrorType() {
-		return excelErrorType;
-	}
-
-	public void setExcelErrorType(String excelErrorType) {
-		this.excelErrorType = excelErrorType;
-	}
-
-}

+ 0 - 31
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ExcelProperty.java

@@ -1,31 +0,0 @@
-package cn.com.qmth.examcloud.service.examwork.util;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-@Target(ElementType.FIELD)
-@Retention(RetentionPolicy.RUNTIME)
-public @interface ExcelProperty {
-	/**
-	 * 导出时列名
-	 */
-    String name() default "";
-    
-	/**
-	 * 导出时列宽
-	 */
-    int width() default 0;
-	/**
-	 * 排序
-	 */
-    int index();
-    /**
-     * 类型
-     * 0:导入(读excel)
-     * 1:导出(写excel)
-     * 2:导入&导出
-     */
-    int type() default 2;
-}

+ 0 - 89
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ExcelReader.java

@@ -1,89 +0,0 @@
-package cn.com.qmth.examcloud.service.examwork.util;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.ss.usermodel.*;
-
-/**
- * 
- * @Description: 读取excel
- * @author ting.yin
- * @date 2016年8月19日
- */
-public class ExcelReader extends ExcelUtils {
-
-	public ExcelReader(Class<?> dataClass) {
-		super(dataClass,true);
-	}
-
-	/**
-	 * 
-	 * @param inputStream
-	 *            输入流
-	 * @param handle
-	 *            单个对象处理器
-	 * @return 错误信息集合
-	 */
-	public List<ExcelError> reader(InputStream inputStream,
-			ExcelReaderHandle handle) {
-		List<ExcelError> excelErrors = new ArrayList<ExcelError>();
-		try {
-			Workbook wb = WorkbookFactory.create(inputStream);
-			Sheet sheet = wb.getSheetAt(0);
-			for (int i = 1; i <= sheet.getLastRowNum(); i++) {
-				Row row = sheet.getRow(i);
-				if (row == null) {
-					return excelErrors;
-				}
-				Object dto = getDataClass().newInstance();
-//				FieldAccess access = FieldAccess.get(getDataClass());
-				for (int j = 0; j < this.getColumnSettings().size(); j++) {
-					ColumnSetting columnSetting = this.getColumnSettings().get(j);
-					Cell cell = row.getCell(columnSetting.getIndex());
-					Object obj = convert(cell);
-					Field field = getDataClass().getDeclaredField(columnSetting.getFieldName());
-					field.setAccessible(true);
-					field.set(dto, obj);
-				}
-				ExcelError error = handle.handle(dto);
-				if (error != null) {
-					error.setRow(i+1);
-					excelErrors.add(error);
-				}
-			}
-		}catch (InvalidFormatException e) {
-				e.printStackTrace();
-		} catch (IOException e) {
-			e.printStackTrace();
-		} catch (Exception e) {
-			e.printStackTrace();
-		} finally {
-			try {
-				inputStream.close();
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-		}
-		return excelErrors;
-	}
-
-	private Object convert(Cell cell) {
-		if(cell!=null){
-			switch (cell.getCellType()) {
-			case Cell.CELL_TYPE_STRING:
-				return cell.getStringCellValue().toString();
-			case Cell.CELL_TYPE_BOOLEAN:
-				return String.valueOf(cell.getBooleanCellValue());
-			case Cell.CELL_TYPE_NUMERIC:
-				return Math.round(cell.getNumericCellValue());
-			}
-		}
-		
-		return null;
-	}
-}

+ 0 - 7
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ExcelReaderHandle.java

@@ -1,7 +0,0 @@
-package cn.com.qmth.examcloud.service.examwork.util;
-
-public interface ExcelReaderHandle {
-	
-	ExcelError handle(Object dto);
-
-}

+ 0 - 70
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ExcelUtils.java

@@ -1,70 +0,0 @@
-package cn.com.qmth.examcloud.service.examwork.util;
-
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Created by zhengmin on 2016/8/17.
- */
-@SuppressWarnings("rawtypes")
-public abstract class ExcelUtils {
-
-	private Class dataClass;
-    private List<ColumnSetting> columnSettings;
-    
-    public ExcelUtils(Class<?> dataClass , boolean isRead){
-        this.dataClass = dataClass;
-        this.columnSettings = getColumnSettings(dataClass,isRead);
-    }
-
-    public Class getDataClass() {
-        return dataClass;
-    }
-
-    public void setDataClass(Class dataClass) {
-        this.dataClass = dataClass;
-    }
-
-    public List<ColumnSetting> getColumnSettings() {
-        return columnSettings;
-    }
-
-    public void setColumnSettings(List<ColumnSetting> columnSettings) {
-        this.columnSettings = columnSettings;
-    }
-
-    /**
-     * 提取ExcelProperty注解类的字段信息
-     * @param dataClass 需要解析excel的数据类型
-     * @return
-     */
-    protected List<ColumnSetting> getColumnSettings(Class<?> dataClass, boolean isRead){
-		List<ColumnSetting> columnSettings = new ArrayList<>();
-		Field[] fileds = dataClass.getDeclaredFields();
-		for (Field field : fileds) {
-			ExcelProperty exportProperty = field.getAnnotation(ExcelProperty.class);
-			if (exportProperty != null) {
-				if (isRead) {
-					if (exportProperty.type() == 0 || exportProperty.type() == 2) {
-						ColumnSetting columnSetting = new ColumnSetting(
-								exportProperty.name(),field.getName(),exportProperty.width(),
-								exportProperty.index());
-						columnSettings.add(columnSetting);
-					}
-				}else{
-					if (exportProperty.type() == 1 || exportProperty.type() == 2) {
-						ColumnSetting columnSetting = new ColumnSetting(
-								exportProperty.name(), field.getName(),exportProperty.width(),
-								exportProperty.index());
-						columnSettings.add(columnSetting);
-					}
-				}
-			}
-		}
-		Collections.sort(columnSettings);
-		return columnSettings;
-    }
-    
-}

+ 0 - 187
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ExcelWriter.java

@@ -1,187 +0,0 @@
-package cn.com.qmth.examcloud.service.examwork.util;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.lang.reflect.Field;
-import java.text.SimpleDateFormat;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.poi.xssf.usermodel.*;
-
-/**
- * Created by dizhi on 2016/6/19.
- */
-public class ExcelWriter extends ExcelUtils{
-
-
-    public ExcelWriter(Class<?> dataClass){
-        super(dataClass,false);
-    }
-
-    /**
-     * 写入excel
-     * @param sheetName sheet名称
-     * @param dataset 数据集合
-     * @param out 输出流
-     */
-    public void write(String sheetName,Collection<?> dataset, OutputStream out) {
-        // 声明一个工作薄
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        // 生成一个表格
-        XSSFSheet sheet = workbook.createSheet(sheetName);
-
-        // 设置表格默认列宽度为15个字节
-        sheet.setDefaultColumnWidth((short) 15);
-        // 生成一个样式
-        XSSFCellStyle style = workbook.createCellStyle();
-        // 设置这些样式
-//        style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
-//        style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
-//        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
-//        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
-//        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
-//        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
-//        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
-        // 生成一个字体
-//        XSSFFont font = workbook.createFont();
-//        font.setColor(HSSFColor.VIOLET.index);
-//        font.setFontHeightInPoints((short) 12);
-//        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
-//        // 把字体应用到当前的样式
-//        style.setFont(font);
-//        // 生成并设置另一个样式
-//        XSSFCellStyle style2 = workbook.createCellStyle();
-//        style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
-//        style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
-//        style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
-//        style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
-//        style2.setBorderRight(XSSFCellStyle.BORDER_THIN);
-//        style2.setBorderTop(XSSFCellStyle.BORDER_THIN);
-//        style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
-//        style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
-//        // 生成另一个字体
-//        XSSFFont font2 = workbook.createFont();
-//        font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
-        // 把字体应用到当前的样式
-        //style2.setFont(font2);
-
-        // 声明一个画图的顶级管理器
-        //HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
-        // 定义注释的大小和位置,详见文档
-//        XSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
-//                0, 0, 0, (short) 4, 2, (short) 6, 5));
-//        // 设置注释内容
-//        comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
-//        // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
-//        comment.setAuthor("leno");
-
-        List<ColumnSetting> columnSettings = this.getColumnSettings();
-
-        // 产生表格标题行
-        XSSFRow row = sheet.createRow(0);
-        for (short i = 0; i < columnSettings.size(); i++) {
-            XSSFCell cell = row.createCell(i);
-            cell.setCellStyle(style);
-            XSSFRichTextString text = new XSSFRichTextString(columnSettings.get(i).getHeader());
-            cell.setCellValue(text);
-            if(columnSettings.get(i).getWidth() > 0){
-                sheet.setColumnWidth(i,columnSettings.get(i).getWidth()*256);
-            }
-        }
-
-        // 遍历集合数据,产生数据行
-        //Iterator<?> it = dataset.iterator();
-        int index = 0;
-        for(Object obj : dataset) {
-            index++;
-            row = sheet.createRow(index);
-            //T t = (T) it.next();
-            // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
-            for (short i = 0; i < columnSettings.size(); i++) {
-                XSSFCell cell = row.createCell(i);
-                //cell.setCellStyle(style2);
-                String fieldName = columnSettings.get(i).getFieldName();
-
-                try {
-                	Field field=this.getDataClass().getDeclaredField(fieldName);
-                	field.setAccessible(true);
-                    Object value = field.get(obj);
-                    // 判断值的类型后进行强制类型转换
-                    String textValue = null;
-                    // if (value instanceof Integer) {
-                    // int intValue = (Integer) value;
-                    // cell.setCellValue(intValue);
-                    // } else if (value instanceof Float) {
-                    // float fValue = (Float) value;
-                    // textValue = new HSSFRichTextString(
-                    // String.valueOf(fValue));
-                    // cell.setCellValue(textValue);
-                    // } else if (value instanceof Double) {
-                    // double dValue = (Double) value;
-                    // textValue = new HSSFRichTextString(
-                    // String.valueOf(dValue));
-                    // cell.setCellValue(textValue);
-                    // } else if (value instanceof Long) {
-                    // long longValue = (Long) value;
-                    // cell.setCellValue(longValue);
-                    // }
-                    if (value instanceof Boolean) {
-                        boolean bValue = (Boolean) value;
-                        textValue = "是";
-                        if (!bValue) {
-                            textValue = "否";
-                        }
-                    } else if (value instanceof Date) {
-                        Date date = (Date) value;
-                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-                        textValue = sdf.format(date);
-                    }
-                    else {
-                        // 其它数据类型都当作字符串简单处理
-                        textValue = String.valueOf(value);
-                    }
-                    // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
-                    if (textValue != null) {
-                        Pattern p = Pattern.compile("^//d+(//.//d+)?$");
-                        Matcher matcher = p.matcher(textValue);
-                        if (matcher.matches()) {
-                            // 是数字当作double处理
-                            cell.setCellValue(Double.parseDouble(textValue));
-                        } else {
-                            XSSFRichTextString richString = new XSSFRichTextString(
-                                    textValue);
-
-                            cell.setCellValue(richString);
-                        }
-                    }
-                } catch (NoSuchFieldException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                } catch (IllegalAccessException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                } catch (SecurityException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                } catch (IllegalArgumentException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                } finally {
-                    // 清理资源
-                }
-            }
-            
-        }
-        try {
-            workbook.write(out);
-        } catch (IOException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-
-    }
-}

+ 5 - 2
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/util/ExportUtils.java

@@ -1,10 +1,13 @@
 package cn.com.qmth.examcloud.service.examwork.util;
 
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletResponse;
 import java.net.URLEncoder;
 import java.util.Collection;
 
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+
+import cn.com.qmth.examcloud.common.util.excel.ExcelWriter;
+
 
 /*
  * excel导出工具

+ 2 - 2
exam-work-main/src/main/resources/application.properties

@@ -1,6 +1,6 @@
-spring.datasource.url=jdbc:mysql://localhost:3306/examcloud?useUnicode=true&characterEncoding=UTF-8
+spring.datasource.url=jdbc:mysql://localhost:3306/exam_cloud?useUnicode=true&characterEncoding=UTF-8
 spring.datasource.username=root
-spring.datasource.password=root
+spring.datasource.password=123456
 spring.datasource.validation-query=SELECT 1 FROM DUAL
 spring.datasource.test-on-borrow=true
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

+ 1 - 0
pom.xml

@@ -39,6 +39,7 @@
         <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
         <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
         <spring.boot.version>1.4.3.RELEASE</spring.boot.version>
+        <exam.cloud.version>0.1.0</exam.cloud.version>
     </properties>
 
     <dependencyManagement>