반사 읽기 해석 excel 템플릿에서 집합으로

5955 단어 codes
자세히 보기
# 호출
List list = readExcelForList(inputStream, fileName, ModelTest.class);

#excel 처리 도구 클래스
public class TestUtil {


.........


public  List readExcelForList(InputStream input, String fileName, Class cls) {
		List dataList = new ArrayList();
		//InputStream input = null;
		try {
			//input = new FileInputStream(filePath);
			Workbook wb = null;
			if (fileName.endsWith(".xls")) {
				wb = new HSSFWorkbook(input);
			} else {
				wb = new XSSFWorkbook(input);
			}
			Sheet sheet = wb.getSheetAt(0);
			
			//  
			Row row = sheet.getRow(0);
			String[] titles = new String[]{" "};
			int rowIndex = 1;
			int rowNum= sheet.getLastRowNum();
			
			for (int i = 1; i < rowNum + 1; i++) {
				dataList.add((T) getRowValueTemplate(sheet, i, titles, AssetsMutiltm.class));
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return dataList;
	}
	
	static DataFormatter formatter = new DataFormatter();
	
	private  T getRowValueTemplate(Sheet sheet, int rowIndex, String[] columnArr, Class cls) {
		Row row = sheet.getRow(rowIndex);
		T t = null;
		try {
			t = cls.newInstance();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		Method[] methods = cls.getMethods();
		for (Method m : methods) {
			ExcelAnnotationForMethod an = m.getAnnotation(ExcelAnnotationForMethod.class);
			if (null != an) {
				for (int i = 0; i < columnArr.length; i++) {
					if (an.columnName().equals(columnArr[i])) {
						String type = an.type();
						Cell cell = row.getCell(i);
						if (null == cell) {
							cell = row.createCell(i);
						}
						try {
							if ("string".equals(type)) {
								m.invoke(t, formatter.formatCellValue(cell));
							} else if ("date".equals(type)) {
								int cellType = cell.getCellType();
								if (cellType == Cell.CELL_TYPE_STRING) {
									String value = cell.toString().trim();
									m.invoke(t, (Object) null);
								} else if (cellType == Cell.CELL_TYPE_NUMERIC) {
									Date date = cell.getDateCellValue();
									m.invoke(t, date);
								}
								
							} else if ("int".equals(type)) {
								m.invoke(t, formatter.formatCellValue(cell));
							} else {
								m.invoke(t, getCellValueTemplate(row, i));
							}
							
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return t;
	}
	
	private Object getCellValueTemplate(Row row, int col) {
		if (row == null) {
			return "";
		}
		Cell cell = row.getCell(col);
		
		return getCellValueTemplate(cell);
	}
	
	private Object getCellValueTemplate(Cell cell) {
		if (cell == null) {
			return "";
		}else{
			Date date = null;
			String value = "";
			if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
				if(DateUtil.isCellDateFormatted(cell)){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
					date = cell.getDateCellValue();
					if (null != date) {
						value = sdf.format(date);
					}
					return date;
				} else {
					value = formatter.formatCellValue(cell);
				}
			} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
				value = formatter.formatCellValue(cell);
			} else {
				value = cell.toString().trim();
			}
			try {
				// This step is used to prevent Integer string being output with
				// '.0'.
				Float.parseFloat(value);
				value=value.replaceAll("\\.0$", "");
				value=value.replaceAll("\\.0+$", "");
				return value;
			} catch (NumberFormatException ex) {
				return value;
			}
		}

	}


.........



}

# 메모 클래스
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExcelAnnotationForMethod {
	String columnName ();
	String type() default "string";
}

#model
public class ModelTest{
    private Long id;
    private String source;

    private int rowIndex;


    public Long getId( ){
		    return id;
    }
    public void setId(Long id){
		    this.id=id;
    }

    public String getSource( ){
		    return source;
    }
    @ExcelAnnotationForMethod(columnName=" ")
    public void setSource(String source){
		    this.source=source;
    }

	public int getRowIndex() {
		return rowIndex;
	}
	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}
    }

좋은 웹페이지 즐겨찾기