package com.ken.wms.common.util;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
* Excel -JavaBean 转换器
*
* @author Ken
* @since 2017/3/27.
*/
public class EJConvertor {
/**
* 默认配置文件名
*/
private static final String DEFAULT_CONFIG_FILE_NAME = "EJConvertorConfig.xml";
/**
* Entity 节点名称
*/
private static final String ENTITY_ELEMENT = "entity";
/**
* Property 节点名称
*/
private static final String PROPERTY_ELEMENT = "property";
/**
* Field 节点信息
*/
private static final String FIELD_ELEMENT = "field";
/**
* Value 节点信息
*/
private static final String VALUE_ELEMENT = "value";
/**
* class 属性
*/
private static final String CLASS_ATTRIBUTE = "class";
/**
* sheetName 属性
*/
private static final String SHEET_NAME_ATTRIBUTE = "sheetName";
/**
* boldHeading 属性
*/
private static final String BOLD_HEADING_ATTRIBUTE = "boldHeading";
/**
* JavaBean的映射信息
*/
private Map<String, MappingInfo> excelJavaBeanMap;
public EJConvertor() {
init(DEFAULT_CONFIG_FILE_NAME);
}
public EJConvertor(String filePath) {
init(filePath);
}
/**
* 初始化映射信息
*
* @param fileLocation 配置文件路径
*/
private void init(String fileLocation) {
try {
// 读取配置文件
File configFile = new ClassPathResource(fileLocation).getFile();
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = documentBuilder.parse(configFile);
// 解析配置文件
this.excelJavaBeanMap = parseMappingInfo(doc);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
/**
* 解析 Document root 下配置的所有 excel-javaBean 映射信息
*
* @param root Document 根节点
* @return 返回 excel-javaBean 映射信息
*/
private Map<String, MappingInfo> parseMappingInfo(Document root) {
// 获取 root 下的所有 entity 节点
NodeList entities = root.getElementsByTagName(ENTITY_ELEMENT);
// 创建承载 MappingInfo 信息的Map
Map<String, MappingInfo> mappingInfoMap = new HashMap<>(entities.getLength());
// 解析 entity 节点
for (int index = 0; index < entities.getLength(); index++) {
// 创建 mappingInfo
MappingInfo mappingInfo = new MappingInfo();
// 解析节点信息
Node entityNode = entities.item(index);
if (entityNode.getNodeType() == Node.ELEMENT_NODE) {
Element entityElement = (Element) entityNode;
parseEntityElement(entityElement, mappingInfo);
}
// 保存节点信息
mappingInfoMap.put(mappingInfo.getClassName(), mappingInfo);
}
return mappingInfoMap;
}
/**
* 解析 entity 节点信息
*
* @param entityElement entity 节点
* @param mappingInfo 本 entity 节点包含的映射信息
*/
private void parseEntityElement(Element entityElement, MappingInfo mappingInfo) {
// 解析 entity 的 class 属性
String className = entityElement.getAttribute(CLASS_ATTRIBUTE);
mappingInfo.setClassName(className);
// 解析 entity 的 sheetName 属性
if (entityElement.hasAttribute(SHEET_NAME_ATTRIBUTE))
mappingInfo.setSheetName(entityElement.getAttribute(SHEET_NAME_ATTRIBUTE));
// 解析 entity 的 boldHeading 属性
if (entityElement.hasAttribute(BOLD_HEADING_ATTRIBUTE)) {
String isBoldHeading = entityElement.getAttribute(BOLD_HEADING_ATTRIBUTE);
mappingInfo.setBoldHeading(isBoldHeading.equals("true"));
}
// 读取并解析 property 节点
NodeList properties = entityElement.getElementsByTagName(PROPERTY_ELEMENT);
for (int index = 0; index < properties.getLength(); index++) {
Node propertyNode = properties.item(index);
if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
Element propertyElement = (Element) propertyNode;
parsePropertyElement(propertyElement, mappingInfo);
}
}
}
/**
* 解析 property 节点信息
*
* @param propertyElement property 节点
* @param mappingInfo 承载映射信息
*/
private void parsePropertyElement(Element propertyElement, MappingInfo mappingInfo) {
NodeList infoNodes = propertyElement.getChildNodes();
String field = null;
String value = null;
for (int infoNode_index = 0; infoNode_index < infoNodes.getLength(); infoNode_index++) {
Node infoNode = infoNodes.item(infoNode_index);
if (infoNode.getNodeName().equals(FIELD_ELEMENT))
field = infoNode.getTextContent();
if (infoNode.getNodeName().equals(VALUE_ELEMENT))
value = infoNode.getTextContent();
}
// 添加到映射信息中
if (field != null && value != null) {
mappingInfo.addFieldValueMapping(field, value);
mappingInfo.addValueFieldMapping(value, field);
}
}
/**
* 讀取 Excel 文件中的内容 Excel 文件中的每一行代表了一个对象实例,而行中各列的属性值对应为对象中的各个属性值
* 读取时,需要指定读取目标对象的类型以获得相关的映射信息,并且要求该对象已在配置文件中注册
*
* @param javaBeanClass 目标对象的类型
* @param file 数据来源的 Excel 文件
* @return 包含若干个目标对象实例的 List
*/
public <T> List<T> excelReader(Class<T> javaBeanClass, File file) {
// 参数检查
if (file == null || javaBeanClass == null)
return null;
// 初始化存放读取结果的 List
List<T> javaBeans = new ArrayList<>();
// 获取类名和映射信息
String className = javaBeanClass.getName();
MappingInfo mappingInfo = excelJavaBeanMap.get(className);
if (mappingInfo == null)
return null;
// 读取 Excel 文件
try (Workbook workbook = new XSSFWorkbook(new FileInputStream(file))) {
Sheet dataSheet = workbook.getSheetAt(0);
Row row;
Cell cell;
Iterator<Row> rowIterator = dataSheet.iterator();
Iterator<Cell> cellIterator;
// 读取第一行表头信息
if (!rowIterator.hasNext())
return null;
String fieldName;
Field fieldInstance;
Class<?> fieldClass;
List<String> fieldNameList = new ArrayList<>();// 目标对象的 field 名称列表
List<Class<?>> fieldClassList = new ArrayList<>();// 目标对象 field 类型列表
row = rowIterator.next();
cellIterator = row.iterator();
while (cellItera
没有合适的资源?快使用搜索试试~ 我知道了~
ssm+jsp实现仓库管理系统源码+数据库+演示视频
共564个文件
class:185个
java:92个
xml:65个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 11 下载量 160 浏览量
2022-07-05
15:43:14
上传
评论 6
收藏 60.79MB ZIP 举报
温馨提示
使用Spring+SpringMVC+Mybatis+maven+tomcat实现仓库管理系统,数据库使用的是mysql,项目使用idea可以直接运行
资源推荐
资源详情
资源评论
收起资源包目录
ssm+jsp实现仓库管理系统源码+数据库+演示视频 (564个子文件)
access_log.2022-07-05 202B
EJConvertor.class 18KB
EJConvertor.class 18KB
StockRecordManageServiceImpl.class 13KB
StockRecordManageServiceImpl.class 13KB
StorageManageServiceImpl.class 12KB
StorageManageServiceImpl.class 12KB
SystemLogServiceImpl.class 10KB
SystemLogServiceImpl.class 10KB
StorageManageHandler.class 10KB
StorageManageHandler.class 10KB
RepositoryAdminManageServiceImpl.class 9KB
RepositoryAdminManageServiceImpl.class 9KB
AccountHandler.class 9KB
AccountHandler.class 9KB
RepositoryManageServiceImpl.class 9KB
RepositoryManageServiceImpl.class 9KB
GoodsManageServiceImpl.class 8KB
GoodsManageServiceImpl.class 8KB
RepositoryManageHandler.class 8KB
RepositoryManageHandler.class 8KB
CustomerManageServiceImpl.class 8KB
CustomerManageServiceImpl.class 8KB
SupplierManageServiceImpl.class 8KB
SupplierManageServiceImpl.class 8KB
RepositoryAdminManageHandler.class 8KB
RepositoryAdminManageHandler.class 8KB
CustomerManageHandler.class 8KB
CustomerManageHandler.class 8KB
SupplierManageHandler.class 8KB
SupplierManageHandler.class 8KB
GoodsManageHandler.class 7KB
GoodsManageHandler.class 7KB
UserInfoServiceImpl.class 7KB
UserInfoServiceImpl.class 7KB
StockRecordManageHandler.class 6KB
StockRecordManageHandler.class 6KB
KickoutSessionControlFilter.class 5KB
KickoutSessionControlFilter.class 5KB
SystemLogHandler.class 5KB
SystemLogHandler.class 5KB
UserAuthorizingRealm.class 4KB
UserAuthorizingRealm.class 4KB
ExtendFormAuthenticationFilter.class 3KB
ExtendFormAuthenticationFilter.class 3KB
AccountServiceImpl.class 3KB
AccountServiceImpl.class 3KB
FilterChainDefinitionMapBuilder.class 3KB
FilterChainDefinitionMapBuilder.class 3KB
CaptchaGenerator.class 3KB
CaptchaGenerator.class 3KB
StockOutDO.class 3KB
StockOutDO.class 3KB
StockInDO.class 3KB
StockInDO.class 3KB
StockRecordDTO.class 3KB
StockRecordDTO.class 3KB
UserInfoDTO.class 3KB
UserInfoDTO.class 3KB
UserOperationLogging.class 3KB
UserOperationLogging.class 3KB
EJConvertor$MappingInfo.class 3KB
EJConvertor$MappingInfo.class 3KB
FileSourceHandler.class 3KB
FileSourceHandler.class 3KB
Storage.class 2KB
Storage.class 2KB
ServiceLogging.class 2KB
ServiceLogging.class 2KB
RepositoryAdmin.class 2KB
RepositoryAdmin.class 2KB
UserOperationRecordDO.class 2KB
UserOperationRecordDO.class 2KB
Repository.class 2KB
Repository.class 2KB
UserOperationRecordDTO.class 2KB
UserOperationRecordDTO.class 2KB
AccessRecordDO.class 2KB
AccessRecordDO.class 2KB
AccessRecordDTO.class 2KB
AccessRecordDTO.class 2KB
Supplier.class 2KB
Customer.class 2KB
Supplier.class 2KB
Customer.class 2KB
StorageManageService.class 2KB
StorageManageService.class 2KB
Response.class 2KB
Response.class 2KB
Goods.class 2KB
Goods.class 2KB
SessionListener.class 2KB
SessionListener.class 2KB
UserInfoDO.class 2KB
UserInfoDO.class 2KB
GlobalExceptionHandler.class 2KB
GlobalExceptionHandler.class 2KB
RepositoryAdminManageService.class 2KB
RepositoryAdminManageService.class 2KB
SystemLogService.class 1KB
共 564 条
- 1
- 2
- 3
- 4
- 5
- 6
程序员小王java
- 粉丝: 4346
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页