苦尽甘来 一个月学通JavaWeb(四十六 WMS系统)

阅读: 评论:0

苦尽甘来 一个月学通JavaWeb(四十六 WMS系统)

苦尽甘来 一个月学通JavaWeb(四十六 WMS系统)

夜光序言:

前尘如梦独醉里

世间总是多情痴

终年不遇便深埋

安得生死许相思

 

 

 

正文:

 

package com.ken.wmsmon.util;import figuration2.HierarchicalConfiguration;
import figuration2.XMLConfiguration;
import figuration2.builder.fluent.Configurations;
import ConfigurationException;
import ImmutableNode;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import flect.Method;
import java.sql.Date;
DecimalFormat;
import java.util.*;/*** @author Ken /  Yeguang / Genius Team*/
public class ExcelUtil {// 默认配置文件名private static final String DEFAULT_FILE_NAME = &#l";private XMLConfiguration xmlConfig;// 实体类与Excel的映射关系private Map<String, MappingInfo> excelMappingInfo;public ExcelUtil() {init(DEFAULT_FILE_NAME);}public ExcelUtil(String fileLocation) {init(fileLocation);}/*** 对 ExcelUtil 进行初始化 将扫描配置文件,加载配置的参数** @throws ConfigurationException*/private void init(String fileLocation) {// 创建对象的 excelMappingInfo 映射excelMappingInfo = new HashMap<>();Configurations configs = new Configurations();try {xmlConfig = l(fileLocation);} catch (ConfigurationException e) {e.printStackTrace();}if (xmlConfig == null) {return;}/** 扫描 XMl 并配置参数*/// 扫描 entity 节点List<Object> entities = List("entity[@class]");if (entities == null) {return;}int entityNum = entities.size();for (int i = 0; i < entityNum; i++) {MappingInfo mappingInfo = new MappingInfo();// 获得全限定类名String className = String("entity(" + i + ")[@class]");mappingInfo.setClassName(className);// 扫描 property 节点List<HierarchicalConfiguration<ImmutableNode>> properties = figurationsAt("entity(" + i + ").property");for (HierarchicalConfiguration<ImmutableNode> property : properties) {// 解析String field = String("field");String value = String("value");mappingInfo.addFieldsMap(field, value);mappingInfo.addValuesMap(value, field);}// 将 entity 添加到 excelMappingInfoexcelMappingInfo.put(className, mappingInfo);}}/*** 讀取 Excel 文件中的内容 Excel 文件中的每一行代表了一个对象实例,而行中各列的属性值对应为对象中的各个属性值* 读取时,需要指定读取目标对象的类型以获得相关的映射信息,并且要求该对象已在配置文件中注册** @param classType 目标对象的类型* @param file      数据来源的 Excel 文件* @return 包含若干个目标对象实例的 List*/public List<Object> excelReader(Class<? extends Object> classType, MultipartFile file) {if (file == null)return null;// 初始化存放读取结果的 ListList<Object> content = new ArrayList<>();// 获取类名和映射信息String className = Name();MappingInfo mappingInfo = (className);if (mappingInfo == null)return null;// 读取 Excel 文件try (Workbook workbook = new InputStream())) {Sheet dataSheet = SheetAt(0);Row row;Cell cell;Iterator<Row> rowIterator = dataSheet.iterator();Iterator<Cell> cellIterator;// 读取第一行表头信息if (!rowIterator.hasNext())return null;List<String> methodList = new ArrayList<>();// setter 方法列表List<Class<?>> fieldTypeList = new ArrayList<>();// 目标对象属性类型列表row = ();cellIterator = row.iterator();String field;while (cellIterator.hasNext()) {cell = ();field = (StringCellValue());Class<?> fieldType = DeclaredField(field).getType();fieldTypeList.ColumnIndex(), fieldType);methodList.ColumnIndex(), getSetterMethodName(field));}// 逐行读取表格内容,创建对象赋值并导入while (rowIterator.hasNext()) {row = ();cellIterator = row.iterator();Object elem = wInstance();// 读取单元格while (cellIterator.hasNext()) {cell = ();int columnIndex = ColumnIndex();Class<?> fieldType = (columnIndex);String methodName = (columnIndex);// 获取单元格的值,并设置对象中对应的属性Object value = getCellValue(fieldType, cell);if (value == null) continue;setField(elem, methodName, value);}// 放入结果content.add(elem);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchFieldException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return content;}/*** 将 List 中的元素对象写入到 Excel 中,其中每一个对象的一行,每一列的内容为对象的属性** @param classType 目标对象的类型* @param elems     数据来源的 List* @return*/public File excelWriter(Class<? extends Object> classType, List<?> elems) {if (classType == null || elems == null)return null;// 获取类名和映射信息String className = Name();MappingInfo mappingInfo = (className);if (mappingInfo == null)return null;File excel = null;try {// 创建临时文件excel = ateTempFile("excel", ".xslx");// 获取该 class 中定义的 field, 并将对应的信息保存到 List 中List<String> fieldList = new ArrayList<>();List<String> methodList = new ArrayList<>();List<String> valuesList = new ArrayList<>();Set<String> fields = mappingInfo.fieldsMap.keySet();if (fields == null)return null;for (String field : fields) {fieldList.add(field);methodList.add(getGetterMethodName(field));valuesList.add((field));}// 创建 workBook 对象Workbook workbook = new XSSFWorkbook();// 创建 sheet 对象Sheet sheet = ateSheet();int rowCount = 0;int cellCount;Row row;Cell cell;// 写入第一行表头row = ateRow(rowCount++);cellCount = 0;for (String value : valuesList) {cell = ateCell(cellCount);cell.setCellValue(value);cellCount++;}// 写入内容数据for (Object elem : elems) {row = ateRow(rowCount++);cellCount = 0;for (String methodName : methodList) {Object value = getField(elem, methodName);cell = ateCell(cellCount++);setCellValue(value, workbook, cell);}}// 将 workBook 写入到 tempFile 中FileOutputStream outputStream = new FileOutputStream(excel);workbook.write(outputStream);outputStream.flush();outputStream.close();workbook.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return excel;}/*** 该方法用于获取单元格 cell 中的值** @param fieldType 指定获取的值的类型* @param cell      单元格* @return 单元格中的值*/private Object getCellValue(Class<?> fieldType, Cell cell) {if (cell == null)return null;int cellType = CellType();Object value = null;if (cellType == Cell.CELL_TYPE_STRING) {if (fieldType.equals(String.class)) {value = StringCellValue();}} else if (cellType == Cell.CELL_TYPE_NUMERIC) {if (fieldType.equals(String.class)) {value = new DecimalFormat("0").NumericCellValue());} else if (fieldType.equals(Date.class)) {// && HSSFDateUtil.isCellDateFormatted(cell)value = new DateCellValue().getTime());} else if (fieldType.equals(Long.class)) {Double v = NumericCellValue();value = v.longValue();} else if (fieldType.equals(Integer.class)) {Double v = NumericCellValue();value = v.intValue();} else {value = NumericCellValue();}} else if (cellType == Cell.CELL_TYPE_BOOLEAN) {if (fieldType.equals(Boolean.class)) {value = BooleanCellValue();}} else if (cellType == Cell.CELL_TYPE_FORMULA) {} else if (cellType == Cell.CELL_TYPE_ERROR) {} else if (cellType == Cell.CELL_TYPE_BLANK) {}return value;}/*** 设置 Excel 单元格的值** @param value 值* @param cell  单元格*/private void setCellValue(Object value, Workbook workbook, Cell cell) {if (cell == null || value == null)return;Class<?> valueClassType = Class();if (valueClassType.equals(String.class)) {String v = (String) value;cell.setCellValue(v);} else if (valueClassType.equals(Integer.class)) {Integer v = (Integer) value;cell.setCellValue(v);} else if (valueClassType.equals(Long.class)) {Long v = (Long) value;cell.setCellValue(v);} else if (valueClassType.equals(Double.class)) {Double v = (Double) value;cell.setCellValue(v);} else if (valueClassType.equals(Boolean.class)) {Boolean v = (Boolean) value;cell.setCellValue(v);} else if (valueClassType.equals(Date.class)) {Date v = (Date) value;CellStyle cellStyle = ateCellStyle();CreationHelper creationHelper = CreationHelper();cellStyle.ateDataFormat().getFormat("yyyy/mm/dd"));cell.setCellValue(v);cell.setCellStyle(cellStyle);}}/*** 该方法用于设置对象中属性的值 通过调用目标对象属性对应的 setter 方法,因而要求目标对象必须设置 setter对象,否则赋值不成功** @param targetObject 目标对象* @param methodName   setter 方法名* @param field        方法参数的值* @throws Exception*/private void setField(Object targetObject, String methodName, Object field) throws Exception {// 获得 setter 方法实例Class<?> targetObjectType = Class();Class<?> fieldType = Class();Method setterMethod = Method(methodName, fieldType);// 调用方法setterMethod.invoke(targetObject, field);}/*** 获取目标对象中某个属性的值,通过调用目标对象属性对应的 getter 方法,因而要求目标对象必须设置 getter 对象,否则赋值不成功** @param targetObject 目标对象* @param methodName   getter 方法名* @return 返回该属性的值* @throws Exception*/private Object getField(Object targetObject, String methodName) throws Exception {// 获得 getter 方法实例Class<?> targetObjectType = Class();Method getterMethod = Method(methodName);// 调用方法return getterMethod.invoke(targetObject);}/*** 构造 setter 方法的方法名** @param field 字段名* @return*/private String getSetterMethodName(String field) {// 转换为首字母大写String name = placeFirst(field.substring(0, 1), field.substring(0, 1).toUpperCase());// 拼接 set 并返回return "set" + name;}/*** 构造 getter 方法的方法名** @param field 字段名* @return*/private String getGetterMethodName(String field) {// 转换为首字母大写String name = placeFirst(field.substring(0, 1), field.substring(0, 1).toUpperCase());// 拼接 get 并返回return "get" + name;}/*** 该对象代表了各个注册到 ExcelUtil 对象的映射信息 映射信息包括:注册对象的类型,对象属性与 Excel 列名的映射** @author Ken*/private class MappingInfo {private String className;private Map<String, String> fieldsMap = new HashMap<>();private Map<String, String> valuesMap = new HashMap<>();@SuppressWarnings("unused")public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public void addFieldsMap(String field, String value) {fieldsMap.put(field, value);}public void addValuesMap(String value, String field) {valuesMap.put(value, field);}}
}
package com.ken.wmsmon.util;import llections.map.HashedMap;import java.util.Map;/*** controller 返回的信息载体 response* @author ken /  Yeguang / Genius Team** Created by Genius Team*/
public class Response {public static final String RESPONSE_RESULT_SUCCESS = "success";public static final String RESPONSE_RESULT_ERROR = "error";// response 中可能使用的值private static final String RESPONSE_RESULT = "result";private static final String RESPONSE_MSG = "msg";private static final String RESPONSE_DATA = "data";private static final String RESPONSE_TOTAL = "total";// 存放响应中的信息private Map<String,Object> responseContent;// ConstructorResponse() {sponseContent = new HashedMap(10);}/*** 设置 response 的状态* @param result response 的状态,值为 success 或 error*/public void setResponseResult(String result){sponseContent.put(Response.RESPONSE_RESULT,result);}/*** 设置 response 的附加信息* @param msg response  的附加信息*/public void setResponseMsg(String msg){sponseContent.put(Response.RESPONSE_MSG,msg);}/*** 设置 response 中携带的数据* @param data response 中携带的数据*/public void setResponseData(Object data){sponseContent.put(Response.RESPONSE_DATA,data);}/*** 设置 response 中携带数据的数量,与 RESPONSE_DATA 配合使用* @param total 携带数据的数量*/public void setResponseTotal(long total){sponseContent.put(Response.RESPONSE_TOTAL,total);}/*** 设置 response 自定义信息* @param key 自定义信息的 key* @param value 自定义信息的值*/public void setCustomerInfo(String key, Object value){sponseContent.put(key,value);}/*** 生成 response* @return 代表 response 的一个 Map 对象*/public Map<String, Object> generateResponse(){sponseContent;}
}
package com.ken.wmsmon.util;import org.springframework.stereotype.Component;/*** Response Utils* Created by Ken /  Yeguang / Genius Team*/
@Component
public class ResponseUtil {/*** 生成一个 Response 对象* @return response 对象*/public Response newResponseInstance(){Response response = new Response();return response;}}

 

 

 

 

 

本文发布于:2024-02-01 10:28:34,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170675451635984.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:一个月   苦尽甘来   系统   JavaWeb   WMS
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23