本次已SSH框架举例
1.写个上传的控件
<DIV ID = “自营对话框” 风格= “ 显示:无; 文本对齐:左; ” > <形式 行动= ‘productsPriceView_modifySalePrice.do’ 方法= ‘POST’ 风格= ‘ 文本对齐:左’ ENCTYPE = ‘多部分/格式数据’ > 请上传附件(XLSX) <输入侧类型= ‘文件’ 名称= “文件名“ 大小= ”字符“ > <输入类型= ”提交“ 值= ”提交“ > </ FORM> </ DIV>
2.在动作里面
private String fileName ; 公共 JObject modifySalePrice()抛出异常{ //判断 文件是否存在 ,如果(文件名 == 空 || “”。equals(fileName)){ throw new RuntimeException(“请选择导入文件”); } 列表<String> msgs = new ArrayList <String>(); list <ProductsPrice> listProducts = productsPriceService .modifyProducts(new File(fileName), msgs); 返回新的 JResult(“导入成功!”); }
3.在服务里面
/ ** *产品价格导入 * @param excel * @param msgs * @return * @throws异常 * / public List <ProductsPrice> modifyProducts(File excel, List <String> msgs) throws Exception { 列表<ProductSalePriceModel1> models = new ArrayList <ProductSalePriceModel1>(); 尝试 { 模型= excelFunction .parse(EXCEL , ProductSalePriceModel1。类); } catch(Exception e){ throw new RuntimeException(“请使用系统给定模版”); } 如果(models!= null && models.size()< 1){ throw new Exception(“导入文件中没有数据,请检查”); // queD } else if(models.size()> 0){ ProductSalePriceModel1 products = models.get(0); Map <Integer , String> errorInfo = products.getErrorInfo(); if(!errorInfo.isEmpty()){ logger .info(errorInfo.toString()); 抛出新的异常(“请使用系统给定模版”); } }
4. ProductSalePriceModel1实体注意Excel里面尽量设置为文本格式,这个模型以String接收,不然数据转换的时候就出异常
public class ProductSalePriceModel1 extends BaseModel { / ** *产品id * * / protected String productsId ; public String getProductsId(){ return productsId ; } 公共无效setProductsId(字符串productsId){ 此。productsId = productsId ; } }
巴斯莫德尔实体
public class BaseModel { protected int origRowNum ; protected Map <String , Integer> title_cellNumMap ; protected String systemMsg ; protected boolean logicError ; protected Map <Integer , String> errorInfo = new HashMap <Integer , String>(); public int getOrigRowNum(){ return origRowNum ; } public String [] getOverwritableModelProperties(){ return title_cellNumMap == null?null:title_cellNumMap .keySet() .toArray(new String [ 0 ]); } public Integer getOrigCellNum(String title){ return title_cellNumMap == null?null:title_cellNumMap .get(title); } 公共无效setOrigRowNum(INT origRowNum){ 此。origRowNum = origRowNum ; } 公共无效setTitle_cellNumMap(地图<字符串,整数> title_cellNumMap){ 此。title_cellNumMap = title_cellNumMap ; } public String getSystemMsg(){ return systemMsg ; } 公共无效setSystemMsg(字符串systemMsg){ 此。systemMsg = systemMsg ; } public boolean isLogicError(){ return logicError ; } 公共无效setLogicError(布尔 logicError){ 此。logicError = logicError ; } public Map <Integer , String> getErrorInfo(){ return errorInfo ; } 公共无效setErrorInfo(地图<整数,字符串> errorInfo中){ 此。errorInfo中 = errorInfo中; } public Map <String , Integer> getTitle_cellNumMap(){ return title_cellNumMap ; } }6.转模型实体
模型= excelFunction .parse(EXCEL , ProductSalePriceModel1。类);
public < T extends BaseModel> List < T > parse(File excel , Class < T > clazz)throws Exception { FileInputStream fIn = new FileInputStream(excel); XSSFWorkbook xwb = new XSSFWorkbook(fIn); fIn.close(); 返回解析(xwb , clazz); }
public < T extends BaseModel> List < T > parse(XSSFWorkbook excel , Class < T > clazz)throws Exception { List < T > list = new ArrayList < T >(); XSSFSheet telSheet = excel.getSheet(SHEET); boolean gotTitle = false; Map <Integer , String> titleMap = null; 映射<String , Integer> cellNumMap = null; XSSFRow行; Ť型; for(int i = telSheet.getFirstRowNum(); 我<= telSheet.getLastRowNum(); 我++){ row = telSheet.getRow(i); //先解析标题行 if(!gotTitle){ if(!_isTitleRow(row)){ continue; } else { titleMap = _parseTitleRow(row); gotTitle = true; //未设置标题直接返回 if(titleMap.size()== 0){ return list ; } cellNumMap = _filp(titleMap); 继续; } } //解析数据行 if(_isDataRow(row)){ model = _parseDataRow(row , titleMap , cellNumMap , clazz); list.add(model); } else { break; } } 报道查看列表; }
private Map <Integer , String> _parseTitleRow(XSSFRow titleRow)throws Exception { Map <Integer , String> titleMap = new HashMap <Integer , String>(); XSSFCell细胞; 评论意见; for(int i = titleRow.getFirstCellNum(); 我<titleRow.getLastCellNum(); 我++){ cell = titleRow.getCell(i); if(cell.getStringCellValue()。equals(TE)){ break; } comment = cell.getCellComment(); if(comment!= null &&!comment.getString()。toString()。trim()。equals(“”)){ titleMap.put(i , comment.getString()。toString()。trim()); } } 返回 titleMap ; }
private Map <String , Integer> _filp(Map <Integer , String> map){ 映射<String , Integer> flip_map = new HashMap <String , Integer>(); 迭代器< 整数> it = map.keySet()。iterator(); 整数键; while(it.hasNext()){ key = it.next(); flip_map.put(map.get(key), key); } return flip_map ; }
private < T extends BaseModel> T _parseDataRow(XSSFRow dataRow , Map <Integer , String> titleMap , Map <String , Integer> cellNumMap , Class < T > clazz)throws Exception , IllegalAccessException { XSSFCell细胞; t model = clazz.newInstance(); model.setOrigRowNum(dataRow.getRowNum()); model.setTitle_cellNumMap(cellNumMap); 迭代器<整数> it = titleMap.keySet()。iterator(); int cellNum ; String titleName ; while(it.hasNext()){ cellNum = it.next(); cell = dataRow.getCell(cellNum); if(cell == null){ continue; } titleName = titleMap.get(cellNum); 尝试 { _reflectModel(model , titleName , cell); } catch(java.lang.IllegalStateException ie){ model.setSystemMsg “”类型转换错误,请检查相应单元格的数据类型“ + ”propertyName:“ + titleName); model.setLogicError(true); model.getErrorInfo()。put(cellNum ,“类型转换错误,请检查相应单元格的数据类型” + “propertyName:” + titleName); } catch(java.lang.NumberFormatException nfe){+ “propertyName:” + titleName); + “propertyName:” + titleName); model.setSystemMsg(“数据格式错误,请检查相应单元格输入的数据是否正确” + “propertyName:” + titleName); model.setLogicError(true); model.getErrorInfo()。put(cellNum ,“数据格式错误,请检查相应单元格输入的数据是否正确” + “propertyName:” + titleName); } catch(Exception e){ e.printStackTrace(); model.setSystemMsg(e.getMessage()+ “propertyName:” + titleName); model.setLogicError(true); model.getErrorInfo()。put(cellNum , e.getMessage()+ “propertyName:” + titleName); } } 退货模式; }
private void _reflectModel(Object model , String propertyName , XSSFCell cell)throws Exception { PropertyDescriptor pd = new PropertyDescriptor(propertyName , model.getClass()); 方法setMethod = pd.getWriteMethod(); setMethod.invoke(model ,new Object [] {getValue(cell , pd.getPropertyType())}); }
7.这样的Excel中里面的数据就转换成了模型实体了
8.Excel模板限制,每隔标题都有批注,批注就是模型里面的属性
<img src="<font><font class=" "="">https://img-blog.csdn.net/20170516110830501?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXhsZ3NsY2Q=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center“_xhe_src =”以上就是从中读取的数据