com.github.rrsunhome.excelsql.parser.ExcelParser Maven / Gradle / Ivy
package com.github.rrsunhome.excelsql.parser;
import com.github.rrsunhome.excelsql.RuleParserConfig;
import com.github.rrsunhome.excelsql.SqlDefinition;
import com.github.rrsunhome.excelsql.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
/**
* @author : qijia.wang
* create at: 2020/3/31 下午3:43
*/
public class ExcelParser {
/**
* @param path 路径
* @param ruleParserConfig 解析规则
* @return sql定义的对象
* @throws Exception 解析异常
*/
public SqlDefinition parser(String path, RuleParserConfig ruleParserConfig) throws Exception {
return parser(new File(path), ruleParserConfig);
}
/**
* @param file 文件
* @param ruleParserConfig 解析规则
* @return sql定义的对象
* @throws Exception 解析异常
*/
public SqlDefinition parser(File file, RuleParserConfig ruleParserConfig) throws Exception {
return parser(new FileInputStream(file), ruleParserConfig);
}
/**
* @param is 文件
* @param ruleParserConfig 解析规则
* @return sql定义的对象
* @throws Exception 解析异常
*/
public SqlDefinition parser(InputStream is, RuleParserConfig ruleParserConfig) throws Exception {
List fieldMappings = ruleParserConfig.getFieldMappings();
RuleParserConfig.TableMapping tableMapping = ruleParserConfig.getTableMapping();
List> lists = null;
if (StringUtils.isEmpty(tableMapping.getSheetName())) {
lists = ExcelUtils.readExcel(is, tableMapping.getSheetIndex(), ruleParserConfig.getStartRowIndex());
} else {
lists = ExcelUtils.readExcel(is, tableMapping.getSheetName(), ruleParserConfig.getStartRowIndex());
}
SqlDefinition sqlDefinition = new SqlDefinition(tableMapping.getTableName());
processConfig(lists.get(0), fieldMappings);
lists.remove(0);
List rowDefinitions = sqlDefinition.getRowDefinitions();
for (int i = 0; i < lists.size(); i++) {
List